pub struct Date(/* private fields */);
Expand description
Our data uses imprecise dates in the “YYYY-MM-DD” format, with no timezone or time data.
Dates are stored as a packed u32
with 23 bits in use:
(YYYY << YEAR_SHIFT) | (MM << MONTH_SHIFT) | (DD << DAY_SHIFT).
YEAR_SHIFT > MONTH_SHIFT > DAY_SHIFT, so that dates are properly ordered.
Implementations§
Source§impl Date
impl Date
Sourcepub const fn from_parts(year: u32, month: u32, day: u32) -> Date
pub const fn from_parts(year: u32, month: u32, day: u32) -> Date
Creates a Date object from parts.
FIXME: Using this constructor bypasses error checks.
§Examples
let date = Date::from_parts(1988, 02, 16);
assert_eq!(date.year(), 1988);
assert_eq!(date.month(), 2);
assert_eq!(date.day(), 16);
Sourcepub const fn year(self) -> u32
pub const fn year(self) -> u32
Returns the year as an integer.
§Examples
let date = "1988-02-16".parse::<Date>().unwrap();
assert_eq!(date.year(), 1988);
Sourcepub const fn month(self) -> u32
pub const fn month(self) -> u32
Returns the month as an integer.
§Examples
let date = "1988-02-16".parse::<Date>().unwrap();
assert_eq!(date.month(), 2);
Sourcepub const fn day(self) -> u32
pub const fn day(self) -> u32
Returns the day as an integer.
§Examples
let date = "1988-02-16".parse::<Date>().unwrap();
assert_eq!(date.day(), 16);
Sourcepub const fn monthday(self) -> u32
pub const fn monthday(self) -> u32
Returns the month and day as a combined integer.
This is useful mostly for age calculations, where the monthday()
corresponds to an exact day in the given year.
§Examples
let date = "1988-02-16".parse::<Date>().unwrap();
assert_eq!(date.monthday(), 0216);
Sourcepub fn is_valid(self) -> bool
pub fn is_valid(self) -> bool
Determines whether a date exists in the Gregorian calendar.
§Examples
let date = "2000-02-29".parse::<Date>().unwrap();
assert_eq!(date.is_valid(), true);
let date = "2018-04-31".parse::<Date>().unwrap();
assert_eq!(date.is_valid(), false);
Sourcepub fn age_on(self, date: Date) -> Result<Age, &'static str>
pub fn age_on(self, date: Date) -> Result<Age, &'static str>
Calculates the Age of a lifter on a given date,
where self
is the lifter’s BirthDate.
§Failures
Fails if the lifter was not yet born by the given date.
Fails if the lifter would be more than 256 years old.
§Examples
let birthdate = "1991-12-16".parse::<Date>().unwrap();
let meetdate = "2018-11-03".parse::<Date>().unwrap();
assert_eq!(birthdate.age_on(meetdate), Ok(Age::Exact(26)));
Sourcepub fn count_days(self) -> u32
pub fn count_days(self) -> u32
Counts the number of days for the Date in the Common Era (0001-01-01
).
This is used to provide a reference point to facilitate date math.
§Examples
let first_day = "0001-01-01".parse::<Date>().unwrap();
assert_eq!(first_day.count_days(), 1); // The count is inclusive.
let special_day = "1982-06-11".parse::<Date>().unwrap();
assert_eq!(special_day.count_days(), 723_707);
The absolute day count is also used to enable Date subtraction:
let sixth = "2019-04-06".parse::<Date>().unwrap();
let fifth = "2019-04-05".parse::<Date>().unwrap();
assert_eq!(sixth - fifth, 1);