diff options
| author | Botahamec <botahamec@outlook.com> | 2021-12-27 17:07:44 -0500 |
|---|---|---|
| committer | Botahamec <botahamec@outlook.com> | 2021-12-27 17:07:44 -0500 |
| commit | f2c58b71914572b0fb7082cbeef2d639ec7fcd53 (patch) | |
| tree | 3a9f265b7be310a270d2f4d8e4cfe416eb4589aa /src | |
| parent | 561ec37ecfdb0cca9f3f0bfcab93e923bb9727be (diff) | |
Added a super simple date structure
Diffstat (limited to 'src')
| -rw-r--r-- | src/date.rs | 51 | ||||
| -rw-r--r-- | src/lib.rs | 2 |
2 files changed, 53 insertions, 0 deletions
diff --git a/src/date.rs b/src/date.rs new file mode 100644 index 0000000..6cd5f48 --- /dev/null +++ b/src/date.rs @@ -0,0 +1,51 @@ +use crate::{Month, Year}; + +#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)] +pub struct Date { + year: Year, + month: Month, + day: u8, +} + +impl Date { + /// The earliest date which can be represented + pub const MIN: Self = + unsafe { Self::from_calendar_date_unchecked(Year::MIN, Month::January, 1) }; + + /// The latest date which can be represented + pub const MAX: Self = + unsafe { Self::from_calendar_date_unchecked(Year::MAX, Month::December, 31) }; + + // TODO validated from_calendar_date + + /// Creates a date without checking to make sure that it's valid. + /// + /// # Example + /// + /// ``` + /// use botic::Date; + /// + /// let y2k = unsafe { + /// Date::from_calendar_date_unchecked(Year::from(2000), Month::January, 1) + /// }; + /// ``` + /// + /// # Safety + /// + /// This function results in undefined behavior if the given date is not a real date + pub const unsafe fn from_calendar_date_unchecked(year: Year, month: Month, day: u8) -> Self { + Self { year, month, day } + } + + pub const fn year(self) -> Year { + self.year + } + + pub const fn month(self) -> Month { + self.month + } + + pub const fn day(self) -> u8 { + self.day + } +} @@ -1,9 +1,11 @@ #![doc = include_str!("../README.md")] +mod date; mod month; mod weekday; mod year; +pub use date::Date; pub use month::Month; pub use weekday::Weekday; pub use year::Year; |
