Skip to main content

Date

Struct Date 

Source
pub struct Date(/* private fields */);
Expand description

Date is a type that represents the MS-DOS date.

This is a packed 16-bit unsigned integer value.

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

Implementations§

Source§

impl Date

Source

pub const MIN: Self

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

This is “1980-01-01”.

§Examples
assert_eq!(Date::MIN, Date::from_date(date!(1980-01-01)).unwrap());
Source

pub const MAX: Self

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

This is “2107-12-31”.

§Examples
assert_eq!(Date::MAX, Date::from_date(date!(2107-12-31)).unwrap());
Source§

impl Date

Source

pub fn new(date: u16) -> Option<Self>

Creates a new Date with the given MS-DOS date.

Returns None if the given MS-DOS date is not a valid MS-DOS date.

§Examples
assert_eq!(Date::new(0b0000_0000_0010_0001), Some(Date::MIN));
assert_eq!(Date::new(0b1111_1111_1001_1111), Some(Date::MAX));

// The Day field is 0.
assert_eq!(Date::new(0b0000_0000_0010_0000), None);
Source

pub const unsafe fn new_unchecked(date: u16) -> Self

Creates a new Date with the given MS-DOS date.

§Safety

The given MS-DOS date must be a valid MS-DOS date.

Source

pub fn from_date(date: Date) -> Result<Self, DateRangeError>

Creates a new Date with the given time::Date.

§Errors

Returns Err if date is an invalid as the MS-DOS date.

§Examples
assert_eq!(Date::from_date(date!(1980-01-01)), Ok(Date::MIN));
assert_eq!(Date::from_date(date!(2107-12-31)), Ok(Date::MAX));

// Before `1980-01-01`.
assert!(Date::from_date(date!(1979-12-31)).is_err());
// After `2107-12-31`.
assert!(Date::from_date(date!(2108-01-01)).is_err());
Source

pub fn is_valid(self) -> bool

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

Source

pub const fn to_raw(self) -> u16

Returns the MS-DOS date of this Date as the underlying u16 value.

§Examples
assert_eq!(Date::MIN.to_raw(), 0b0000_0000_0010_0001);
assert_eq!(Date::MAX.to_raw(), 0b1111_1111_1001_1111);
Source

pub const fn year(self) -> u16

Gets the year of this Date.

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

pub fn month(self) -> Month

Gets the month of this Date.

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

pub fn day(self) -> u8

Gets the day of this Date.

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

Trait Implementations§

Source§

impl Clone for Date

Source§

fn clone(&self) -> Date

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Date

Source§

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

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

impl Default for Date

Source§

fn default() -> Self

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

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

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

impl Display for Date

Source§

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

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

§Examples
assert_eq!(format!("{}", Date::MIN), "1980-01-01");
assert_eq!(format!("{}", Date::MAX), "2107-12-31");
Source§

impl From<Date> for Date

Source§

fn from(date: Date) -> Self

Converts a Date to a time::Date.

§Examples
assert_eq!(time::Date::from(Date::MIN), date!(1980-01-01));
assert_eq!(time::Date::from(Date::MAX), date!(2107-12-31));
Source§

impl From<Date> for Date

Available on crate feature jiff only.
Source§

fn from(date: Date) -> Self

Converts a Date to a civil::Date.

§Examples
assert_eq!(civil::Date::from(Date::MIN), civil::date(1980, 1, 1));
assert_eq!(civil::Date::from(Date::MAX), civil::date(2107, 12, 31));
Source§

impl From<Date> for NaiveDate

Available on crate feature chrono only.
Source§

fn from(date: Date) -> Self

Converts a Date to a NaiveDate.

§Examples
assert_eq!(
    NaiveDate::from(Date::MIN),
    "1980-01-01".parse::<NaiveDate>().unwrap()
);
assert_eq!(
    NaiveDate::from(Date::MAX),
    "2107-12-31".parse::<NaiveDate>().unwrap()
);
Source§

impl Hash for Date

Source§

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

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

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 Date

Source§

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

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

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

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

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

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

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

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

impl PartialEq for Date

Source§

fn eq(&self, other: &Date) -> 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 PartialOrd for Date

Source§

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

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

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

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

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 · Source§

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

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

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<Date> for Date

Source§

fn try_from(date: Date) -> Result<Self, Self::Error>

Converts a time::Date to a Date.

§Errors

Returns Err if date is out of range for the MS-DOS date.

§Examples
assert_eq!(Date::try_from(date!(1980-01-01)), Ok(Date::MIN));
assert_eq!(Date::try_from(date!(2107-12-31)), Ok(Date::MAX));

// Before `1980-01-01`.
assert!(Date::try_from(date!(1979-12-31)).is_err());
// After `2107-12-31`.
assert!(Date::try_from(date!(2108-01-01)).is_err());
Source§

type Error = DateRangeError

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

impl TryFrom<Date> for Date

Available on crate feature jiff only.
Source§

fn try_from(date: Date) -> Result<Self, Self::Error>

Converts a civil::Date to a Date.

§Errors

Returns Err if date is out of range for the MS-DOS date.

§Examples
assert_eq!(Date::try_from(civil::date(1980, 1, 1)), Ok(Date::MIN));
assert_eq!(Date::try_from(civil::date(2107, 12, 31)), Ok(Date::MAX));

// Before `1980-01-01`.
assert!(Date::try_from(civil::date(1979, 12, 31)).is_err());
// After `2107-12-31`.
assert!(Date::try_from(civil::date(2108, 1, 1)).is_err());
Source§

type Error = DateRangeError

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

impl TryFrom<NaiveDate> for Date

Available on crate feature chrono only.
Source§

fn try_from(date: NaiveDate) -> Result<Self, Self::Error>

Converts a NaiveDate to a Date.

§Errors

Returns Err if date is out of range for the MS-DOS date.

§Examples
assert_eq!(
    Date::try_from("1980-01-01".parse::<NaiveDate>().unwrap()),
    Ok(Date::MIN)
);
assert_eq!(
    Date::try_from("2107-12-31".parse::<NaiveDate>().unwrap()),
    Ok(Date::MAX)
);

// Before `1980-01-01`.
assert!(Date::try_from("1979-12-31".parse::<NaiveDate>().unwrap()).is_err());
// After `2107-12-31`.
assert!(Date::try_from("2108-01-01".parse::<NaiveDate>().unwrap()).is_err());
Source§

type Error = DateRangeError

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

impl Copy for Date

Source§

impl Eq for Date

Source§

impl StructuralPartialEq for Date

Auto Trait Implementations§

§

impl Freeze for Date

§

impl RefUnwindSafe for Date

§

impl Send for Date

§

impl Sync for Date

§

impl Unpin for Date

§

impl UnwindSafe for Date

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> CloneToUninit for T
where T: Clone,

Source§

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
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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

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

Uses borrowed data to replace owned data, usually by cloning. Read more
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.