From f2c58b71914572b0fb7082cbeef2d639ec7fcd53 Mon Sep 17 00:00:00 2001 From: Botahamec Date: Mon, 27 Dec 2021 17:07:44 -0500 Subject: Added a super simple date structure --- src/date.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/date.rs (limited to 'src/date.rs') 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 + } +} -- cgit v1.2.3