cftime_rs/datetimes/
all_leap.rs

1use crate::calendars::Calendar;
2use crate::datetimes::traits::{CalendarDatetime, IsLeap};
3use crate::timezone::Tz;
4use crate::utils::{get_timestamp_from_hms, get_timestamp_from_ymd, get_ymd_hms_from_timestamp};
5
6use super::traits::CalendarDatetimeCreator;
7
8pub struct AllLeapDatetime {
9    pub timestamp: i64,
10    pub nanoseconds: u32,
11    pub tz: Tz,
12    pub calendar: Calendar,
13}
14
15impl AllLeapDatetime {
16    pub fn new(timestamp: i64, nanoseconds: u32, tz: Tz) -> Self {
17        Self {
18            timestamp,
19            nanoseconds,
20            tz,
21            calendar: Calendar::AllLeap,
22        }
23    }
24}
25impl IsLeap for AllLeapDatetime {
26    fn is_leap(_year: i64) -> bool {
27        true
28    }
29}
30
31impl CalendarDatetime for AllLeapDatetime {
32    fn timestamp(&self) -> i64 {
33        self.timestamp
34    }
35    fn nanoseconds(&self) -> u32 {
36        self.nanoseconds
37    }
38    fn calendar(&self) -> Calendar {
39        self.calendar
40    }
41    fn timezone(&self) -> Tz {
42        self.tz
43    }
44    fn ymd_hms(&self) -> Result<(i64, u8, u8, u8, u8, u8), crate::errors::Error> {
45        Ok(get_ymd_hms_from_timestamp::<AllLeapDatetime>(
46            self.timestamp,
47        ))
48    }
49}
50impl CalendarDatetimeCreator for AllLeapDatetime {
51    fn from_timestamp(timestamp: i64, nanoseconds: u32) -> Self {
52        Self {
53            timestamp,
54            nanoseconds,
55            tz: Tz::new(0, 0).unwrap(),
56            calendar: Calendar::AllLeap,
57        }
58    }
59    fn from_ymd_hms(
60        year: i64,
61        month: u8,
62        day: u8,
63        hour: u8,
64        minute: u8,
65        second: f32,
66    ) -> Result<Self, crate::errors::Error> {
67        let (mut timestamp, nanoseconds) = get_timestamp_from_hms(hour, minute, second)?;
68        timestamp += get_timestamp_from_ymd::<AllLeapDatetime>(year, month, day)?;
69        Ok(Self {
70            timestamp,
71            nanoseconds,
72            tz: Tz::new(0, 0).unwrap(),
73            calendar: Calendar::AllLeap,
74        })
75    }
76}