pub struct CFDatetime { /* private fields */ }Expand description
Represents a calendar CF datetime.
Internally it uses the timestamp in seconds representation With this implementation it is faster to add a duration to a CFDatetime However if we ask for a representation of the date calling the format method it will calculate the year, month, day, hour, minute, second and nanoseconds from timestamp which can make print a bit slow
§Examples
§Creating a datetime
let cf_datetime = CFDatetime::from_ymd_hms(1970, 1, 1, 0, 0, 0, Calendar::Standard).unwrap();
// Computation of the timestamp
assert_eq!(cf_datetime.timestamp(), 0);
// Idempotence
assert_eq!(cf_datetime.ymd_hms().unwrap(), (1970, 1, 1, 0, 0, 0));§Duration between two datetimes
let cf_datetime_1 = CFDatetime::from_ymd_hms(1970, 1, 1, 0, 0, 0, Calendar::Standard).unwrap();
let cf_datetime_2 = CFDatetime::from_ymd_hms(1970, 1, 2, 0, 0, 0, Calendar::Standard).unwrap();
let duration = cf_datetime_2 - cf_datetime_1;
assert_eq!(duration.num_days, 1);Implementations§
Source§impl CFDatetime
Immplementation of the CF convention specifications :
impl CFDatetime
Immplementation of the CF convention specifications :
Sourcepub fn ymd(&self) -> Result<(i64, u8, u8), Error>
pub fn ymd(&self) -> Result<(i64, u8, u8), Error>
Returns the year, month, and day of the date.
§Returns
A Result containing a tuple containing the hour, minute, second as (i64, u8, u8).
or an error of type crate::errors::Error::InvalidDate if the date cannot be computed from the timestamp.
Sourcepub fn hms(&self) -> Result<(u8, u8, u8), Error>
pub fn hms(&self) -> Result<(u8, u8, u8), Error>
Returns the hour, minute, second of the date.
§Returns
A Result containing a tuple containing the hour, minute, second as (i64, u8, u8).
or an error of type crate::errors::Error::InvalidDate if the date cannot be computed from the timestamp.
hms needs to first compute the date to see if the date is impossible
Sourcepub fn ymd_hms(&self) -> Result<(i64, u8, u8, u8, u8, u8), Error>
pub fn ymd_hms(&self) -> Result<(i64, u8, u8, u8, u8, u8), Error>
Returns the year, month, day, hour, minute, second of the date.
§Returns
A Result containing a tuple containing the year, month, day, hour, minute, second as
(i64, u8, u8, u8, u8, u8) or an error of type crate::errors::Error::InvalidDate if
the date cannot be computed from the timestamp.
Sourcepub fn from_ymd_hms(
year: i64,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: f32,
calendar: Calendar,
) -> Result<Self, Error>
pub fn from_ymd_hms( year: i64, month: u8, day: u8, hour: u8, minute: u8, second: f32, calendar: Calendar, ) -> Result<Self, Error>
Creates a new CFDatetime from the given year, month, day, hour, minute, second, and calendar.
§Returns
A Result containing a new CFDatetime or an error of type crate::errors::Error::InvalidDate if
the date is not valid in the calendar
Sourcepub fn from_hms(
hour: u8,
minute: u8,
second: f32,
calendar: Calendar,
) -> Result<Self, Error>
pub fn from_hms( hour: u8, minute: u8, second: f32, calendar: Calendar, ) -> Result<Self, Error>
Creates a new CFDatetime from the given hour, minute, second, and calendar. It sets the year, month, day to 1970, 1, 1
§Returns
A Result containing a new CFDatetime or an error of type crate::errors::Error::InvalidDate if
the date is not valid in the calendar
Sourcepub fn from_ymd(
year: i64,
month: u8,
day: u8,
calendar: Calendar,
) -> Result<Self, Error>
pub fn from_ymd( year: i64, month: u8, day: u8, calendar: Calendar, ) -> Result<Self, Error>
Creates a new CFDatetime from the given year, month, day and calendar. It sets the hour, minute, second to 1970, 1, 1
§Returns
A Result containing a new CFDatetime or an error of type crate::errors::Error::InvalidDate if
the date is not valid in the calendar
Sourcepub fn from_timestamp(
timestamp: i64,
nanoseconds: u32,
calendar: Calendar,
) -> Result<Self, Error>
pub fn from_timestamp( timestamp: i64, nanoseconds: u32, calendar: Calendar, ) -> Result<Self, Error>
Creates a new CFDatetime from a given timestamp and calendar atrting from the epoch
§Returns
A Result containing a new CFDatetime or an error of type crate::errors::Error::InvalidDate if
the date is not valid in the calendar
Sourcepub fn nanoseconds(&self) -> u32
pub fn nanoseconds(&self) -> u32
Returns the nanoseconds of the date.
Sourcepub fn change_calendar(&self, calendar: Calendar) -> Result<Self, Error>
pub fn change_calendar(&self, calendar: Calendar) -> Result<Self, Error>
Change the calendar of the CFDatetime.
It get the year, month, day, hour, minute, second and nanoseconds by calling the Self::ymd_hms method and then call the Self::from_ymd_hms method with the new calendar. This can be considered as safe
§Returns
A Result containing a new CFDatetime or an error of type crate::errors::Error::InvalidDate if
the date is not valid in the calendar
Sourcepub fn change_calendar_from_timestamp(
&self,
calendar: Calendar,
) -> Result<Self, Error>
pub fn change_calendar_from_timestamp( &self, calendar: Calendar, ) -> Result<Self, Error>
Change the calendar of the CFDatetime using the timestamp
It get the year, month, day, hour, minute, second and nanoseconds by calling the Self::timestamp method and then call the Self::from_timestamp method with the new calendar.
Be aware that there is highly chance that the two dates do not correspond. However their distances from epoch are the same.
§Returns
A Result containing a new CFDatetime or an error of type crate::errors::Error::InvalidDate if
the date is not valid in the calendar
Trait Implementations§
Source§impl Add<&CFDuration> for &CFDatetime
impl Add<&CFDuration> for &CFDatetime
Source§impl Add<&CFDuration> for CFDatetime
impl Add<&CFDuration> for CFDatetime
Source§impl Add<CFDuration> for &CFDatetime
impl Add<CFDuration> for &CFDatetime
Source§impl Add<CFDuration> for CFDatetime
impl Add<CFDuration> for CFDatetime
Source§impl CFEncoder<f32> for CFDatetime
impl CFEncoder<f32> for CFDatetime
Source§impl CFEncoder<f64> for CFDatetime
impl CFEncoder<f64> for CFDatetime
Source§impl CFEncoder<i32> for CFDatetime
impl CFEncoder<i32> for CFDatetime
Source§impl CFEncoder<i64> for CFDatetime
impl CFEncoder<i64> for CFDatetime
Source§impl Display for CFDatetime
Display a CFDatetime with the following format : YYYY-MM-DD HH:MM:SS.SSS
impl Display for CFDatetime
Display a CFDatetime with the following format : YYYY-MM-DD HH:MM:SS.SSS