pub struct Date { /* private fields */ }Expand description
A calendar date (year, month, day) in the proleptic Gregorian calendar, with no time of day and no time zone.
The year is limited to 0..=9999, so the ISO 8601 form YYYY-MM-DD always has four year
digits. Dates compare chronologically. Being a plain calendar date, it cannot express “now”:
to get today’s date, convert a unix time with Date::from_unix_days (the current time is
an input, never read behind your back).
§Examples
use helpers4::date::Date;
let date = Date::new(2026, 9, 23)?;
assert_eq!(date.to_string(), "2026-09-23");
assert_eq!(date.add_days(10), Some(Date::new(2026, 10, 3)?));
assert_eq!("2024-02-29".parse::<Date>(), Date::new(2024, 2, 29));Implementations§
Source§impl Date
impl Date
Sourcepub fn new(year: i32, month: u8, day: u8) -> Result<Self, DateError>
pub fn new(year: i32, month: u8, day: u8) -> Result<Self, DateError>
Builds a date, checking that it exists.
§Arguments
year- The year,0..=9999.month- The month,1..=12.day- The day of the month,1to the length of that month.
§Errors
DateError::InvalidYear, DateError::InvalidMonth or DateError::InvalidDay when
the date does not exist (such as February 30th).
Sourcepub fn parse(text: &str) -> Result<Self, DateError>
pub fn parse(text: &str) -> Result<Self, DateError>
Parses the ISO 8601 form YYYY-MM-DD.
Exactly four year digits, two month digits and two day digits, separated by hyphens: no spaces, signs or time of day.
§Arguments
text- The text to parse.
§Errors
DateError::InvalidFormat when the text does not have that shape, or the errors of
Date::new when it has the shape but names a date that does not exist.
Sourcepub fn is_leap_year(self) -> bool
pub fn is_leap_year(self) -> bool
Sourcepub fn ordinal(self) -> u16
pub fn ordinal(self) -> u16
The day of the year: 1 for January 1st, 365 (or 366) for December 31st.
§Returns
The ordinal day, 1..=366.
Sourcepub fn unix_days(self) -> i64
pub fn unix_days(self) -> i64
The number of days since 1970-01-01 (negative before it).
§Returns
The day count, the same one Date::from_unix_days reads back.