DateTime

Struct DateTime 

Source
pub struct DateTime { /* private fields */ }
Expand description

DateTime is a type that combines a Date and a Time and represents MS-DOS date and time.

These are packed 16-bit unsigned integer values that specify the date and time an MS-DOS file was last written to, and are used as timestamps such as FAT or ZIP file format.

The resolution of MS-DOS date and time is 2 seconds.

See the format specification for Kaitai Struct for more details on the structure of MS-DOS date and time.

Implementations§

Source§

impl DateTime

Source

pub const MIN: Self

The smallest value that can be represented by MS-DOS date and time.

This is “1980-01-01 00:00:00”.

§Examples
assert_eq!(
    DateTime::MIN,
    DateTime::from_date_time(date!(1980-01-01), Time::MIDNIGHT).unwrap()
);
Source

pub const MAX: Self

The largest value that can be represented by MS-DOS date and time.

This is “2107-12-31 23:59:58”.

§Examples
assert_eq!(
    DateTime::MAX,
    DateTime::from_date_time(date!(2107-12-31), time!(23:59:58)).unwrap()
);
Source§

impl DateTime

Source

pub const fn new(date: Date, time: Time) -> Self

Creates a new DateTime with the given Date and Time.

§Examples
assert_eq!(DateTime::new(Date::MIN, Time::MIN), DateTime::MIN);
assert_eq!(DateTime::new(Date::MAX, Time::MAX), DateTime::MAX);
Source

pub fn from_date_time( date: Date, time: Time, ) -> Result<Self, DateTimeRangeError>

Creates a new DateTime with the given time::Date and time::Time.

The resolution of MS-DOS date and time is 2 seconds. So this method rounds towards zero, truncating any fractional part of the exact result of dividing seconds by 2.

§Errors

Returns Err if date or time are invalid as MS-DOS date and time.

§Examples
assert_eq!(
    DateTime::from_date_time(date!(1980-01-01), Time::MIDNIGHT),
    Ok(DateTime::MIN)
);
assert_eq!(
    DateTime::from_date_time(date!(2107-12-31), time!(23:59:58)),
    Ok(DateTime::MAX)
);

// Before `1980-01-01 00:00:00`.
assert!(DateTime::from_date_time(date!(1979-12-31), time!(23:59:59)).is_err());
// After `2107-12-31 23:59:59`.
assert!(DateTime::from_date_time(date!(2108-01-01), Time::MIDNIGHT).is_err());
Source

pub fn is_valid(self) -> bool

Returns true if self is valid MS-DOS date and time, and false otherwise.

§Examples
assert_eq!(DateTime::MIN.is_valid(), true);
assert_eq!(DateTime::MAX.is_valid(), true);

assert_eq!(
    DateTime::new(unsafe { Date::new_unchecked(u16::MAX) }, unsafe {
        Time::new_unchecked(u16::MAX)
    })
    .is_valid(),
    false
);
Source

pub const fn date(self) -> Date

Gets the Date of this DateTime.

§Examples
assert_eq!(DateTime::MIN.date(), Date::MIN);
assert_eq!(DateTime::MAX.date(), Date::MAX);
Source

pub const fn time(self) -> Time

Gets the Time of this DateTime.

§Examples
assert_eq!(DateTime::MIN.time(), Time::MIN);
assert_eq!(DateTime::MAX.time(), Time::MAX);
Source

pub const fn year(self) -> u16

Gets the year of this DateTime.

§Examples
assert_eq!(DateTime::MIN.year(), 1980);
assert_eq!(DateTime::MAX.year(), 2107);
Source

pub fn month(self) -> Month

Gets the month of this DateTime.

§Examples
assert_eq!(DateTime::MIN.month(), Month::January);
assert_eq!(DateTime::MAX.month(), Month::December);
Source

pub fn day(self) -> u8

Gets the day of this DateTime.

§Examples
assert_eq!(DateTime::MIN.day(), 1);
assert_eq!(DateTime::MAX.day(), 31);
Source

pub fn hour(self) -> u8

Gets the hour of this DateTime.

§Examples
assert_eq!(DateTime::MIN.hour(), 0);
assert_eq!(DateTime::MAX.hour(), 23);
Source

pub fn minute(self) -> u8

Gets the minute of this DateTime.

§Examples
assert_eq!(DateTime::MIN.minute(), 0);
assert_eq!(DateTime::MAX.minute(), 59);
Source

pub fn second(self) -> u8

Gets the second of this DateTime.

§Examples
assert_eq!(DateTime::MIN.second(), 0);
assert_eq!(DateTime::MAX.second(), 58);

Trait Implementations§

Source§

impl Clone for DateTime

Source§

fn clone(&self) -> DateTime

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DateTime

Source§

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

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

impl Default for DateTime

Source§

fn default() -> Self

Returns the default value of “1980-01-01 00:00:00”.

Equivalent to DateTime::MIN except that it is not callable in const contexts.

§Examples
assert_eq!(DateTime::default(), DateTime::MIN);
Source§

impl Display for DateTime

Source§

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

Shows the value of this DateTime in the well-known RFC 3339 format.

§Examples
assert_eq!(format!("{}", DateTime::MIN), "1980-01-01 00:00:00");
assert_eq!(format!("{}", DateTime::MAX), "2107-12-31 23:59:58");
Source§

impl From<DateTime> for DateTime

Available on crate feature jiff only.
Source§

fn from(dt: DateTime) -> Self

Converts a DateTime to a civil::DateTime.

§Examples
assert_eq!(
    civil::DateTime::from(DateTime::MIN),
    civil::date(1980, 1, 1).at(0, 0, 0, 0)
);
assert_eq!(
    civil::DateTime::from(DateTime::MAX),
    civil::date(2107, 12, 31).at(23, 59, 58, 0)
);
Source§

impl From<DateTime> for NaiveDateTime

Available on crate feature chrono only.
Source§

fn from(dt: DateTime) -> Self

Converts a DateTime to a NaiveDateTime.

§Examples
assert_eq!(
    NaiveDateTime::from(DateTime::MIN),
    "1980-01-01T00:00:00".parse::<NaiveDateTime>().unwrap()
);
assert_eq!(
    NaiveDateTime::from(DateTime::MAX),
    "2107-12-31T23:59:58".parse::<NaiveDateTime>().unwrap()
);
Source§

impl From<DateTime> for PrimitiveDateTime

Source§

fn from(dt: DateTime) -> Self

Converts a DateTime to a PrimitiveDateTime.

§Examples
assert_eq!(
    PrimitiveDateTime::from(DateTime::MIN),
    datetime!(1980-01-01 00:00:00)
);
assert_eq!(
    PrimitiveDateTime::from(DateTime::MAX),
    datetime!(2107-12-31 23:59:58)
);
Source§

impl Hash for DateTime

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for DateTime

Source§

fn cmp(&self, other: &DateTime) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for DateTime

Source§

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

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

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 PartialOrd for DateTime

Source§

fn partial_cmp(&self, other: &DateTime) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl TryFrom<DateTime> for DateTime

Available on crate feature jiff only.
Source§

fn try_from(dt: DateTime) -> Result<Self, Self::Error>

Converts a civil::DateTime to a DateTime.

The resolution of MS-DOS date and time is 2 seconds. So this method rounds towards zero, truncating any fractional part of the exact result of dividing seconds by 2.

§Errors

Returns Err if dt is out of range for MS-DOS date and time.

§Examples
assert_eq!(
    DateTime::try_from(civil::date(1980, 1, 1).at(0, 0, 0, 0)),
    Ok(DateTime::MIN)
);
assert_eq!(
    DateTime::try_from(civil::date(2107, 12, 31).at(23, 59, 58, 0)),
    Ok(DateTime::MAX)
);

// Before `1980-01-01 00:00:00`.
assert!(DateTime::try_from(civil::date(1979, 12, 31).at(23, 59, 59, 0)).is_err());
// After `2107-12-31 23:59:59`.
assert!(DateTime::try_from(civil::date(2108, 1, 1).at(0, 0, 0, 0)).is_err());
Source§

type Error = DateTimeRangeError

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

impl TryFrom<NaiveDateTime> for DateTime

Available on crate feature chrono only.
Source§

fn try_from(dt: NaiveDateTime) -> Result<Self, Self::Error>

Converts a NaiveDateTime to a DateTime.

The resolution of MS-DOS date and time is 2 seconds. So this method rounds towards zero, truncating any fractional part of the exact result of dividing seconds by 2.

§Errors

Returns Err if dt is out of range for MS-DOS date and time.

§Examples
assert_eq!(
    DateTime::try_from("1980-01-01T00:00:00".parse::<NaiveDateTime>().unwrap()),
    Ok(DateTime::MIN)
);
assert_eq!(
    DateTime::try_from("2107-12-31T23:59:58".parse::<NaiveDateTime>().unwrap()),
    Ok(DateTime::MAX)
);

// Before `1980-01-01 00:00:00`.
assert!(DateTime::try_from("1979-12-31T23:59:59".parse::<NaiveDateTime>().unwrap()).is_err());
// After `2107-12-31 23:59:59`.
assert!(DateTime::try_from("2108-01-01T00:00:00".parse::<NaiveDateTime>().unwrap()).is_err());
Source§

type Error = DateTimeRangeError

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

impl TryFrom<PrimitiveDateTime> for DateTime

Source§

fn try_from(dt: PrimitiveDateTime) -> Result<Self, Self::Error>

Converts a PrimitiveDateTime to a DateTime.

The resolution of MS-DOS date and time is 2 seconds. So this method rounds towards zero, truncating any fractional part of the exact result of dividing seconds by 2.

§Errors

Returns Err if dt is out of range for MS-DOS date and time.

§Examples
assert_eq!(
    DateTime::try_from(datetime!(1980-01-01 00:00:00)),
    Ok(DateTime::MIN)
);
assert_eq!(
    DateTime::try_from(datetime!(2107-12-31 23:59:58)),
    Ok(DateTime::MAX)
);

// Before `1980-01-01 00:00:00`.
assert!(DateTime::try_from(datetime!(1979-12-31 23:59:59)).is_err());
// After `2107-12-31 23:59:59`.
assert!(DateTime::try_from(datetime!(2108-01-01 00:00:00)).is_err());
Source§

type Error = DateTimeRangeError

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

impl Copy for DateTime

Source§

impl Eq for DateTime

Source§

impl StructuralPartialEq for DateTime

Auto Trait Implementations§

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

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

§

fn to_string(&self) -> String

Converts the given value to a String. Read more
§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.