CFDatetime

Struct CFDatetime 

Source
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 :

Source

pub fn calendar(&self) -> Calendar

Returns the calendar

Source

pub fn timestamp(&self) -> i64

Returns the timestamp

Source

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.

Source

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

Source

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.

Source

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

Source

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

Source

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

Source

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

Source

pub fn hours(&self) -> Result<u8, Error>

Returns the hours of the date.

Source

pub fn minutes(&self) -> Result<u8, Error>

Returns the minutes of the date.

Source

pub fn seconds(&self) -> Result<u8, Error>

Returns the seconds of the date.

Source

pub fn nanoseconds(&self) -> u32

Returns the nanoseconds of the date.

Source

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

Source

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

Source§

type Output = Result<CFDatetime, Error>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &CFDuration) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<&CFDuration> for CFDatetime

Source§

type Output = Result<CFDatetime, Error>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: &CFDuration) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<CFDuration> for &CFDatetime

Source§

type Output = Result<CFDatetime, Error>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: CFDuration) -> Self::Output

Performs the + operation. Read more
Source§

impl Add<CFDuration> for CFDatetime

Source§

type Output = Result<CFDatetime, Error>

The resulting type after applying the + operator.
Source§

fn add(self, rhs: CFDuration) -> Self::Output

Performs the + operation. Read more
Source§

impl CFEncoder<f32> for CFDatetime

Source§

fn encode_cf(&self, units: &str, calendar: Calendar) -> Result<f32, Error>

Encodes the data into a specific format. Read more
Source§

impl CFEncoder<f64> for CFDatetime

Source§

fn encode_cf(&self, units: &str, calendar: Calendar) -> Result<f64, Error>

Encodes the data into a specific format. Read more
Source§

impl CFEncoder<i32> for CFDatetime

Source§

fn encode_cf(&self, units: &str, calendar: Calendar) -> Result<i32, Error>

Encodes the data into a specific format. Read more
Source§

impl CFEncoder<i64> for CFDatetime

Source§

fn encode_cf(&self, units: &str, calendar: Calendar) -> Result<i64, Error>

Encodes the data into a specific format. Read more
Source§

impl Display for CFDatetime

Display a CFDatetime with the following format : YYYY-MM-DD HH:MM:SS.SSS

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for CFDatetime

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Sub<&CFDatetime> for &CFDatetime

Source§

type Output = Result<CFDuration, Error>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &CFDatetime) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<&CFDatetime> for CFDatetime

Source§

type Output = Result<CFDuration, Error>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &CFDatetime) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<&CFDuration> for &CFDatetime

Source§

type Output = Result<CFDatetime, Error>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &CFDuration) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<&CFDuration> for CFDatetime

Source§

type Output = Result<CFDatetime, Error>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: &CFDuration) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<CFDatetime> for &CFDatetime

Source§

type Output = Result<CFDuration, Error>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: CFDatetime) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<CFDuration> for &CFDatetime

Source§

type Output = Result<CFDatetime, Error>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: CFDuration) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub<CFDuration> for CFDatetime

Source§

type Output = Result<CFDatetime, Error>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: CFDuration) -> Self::Output

Performs the - operation. Read more
Source§

impl Sub for CFDatetime

Source§

type Output = Result<CFDuration, Error>

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: CFDatetime) -> Self::Output

Performs the - operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Ungil for T
where T: Send,