cftime_rs/datetimes/
no_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 NoLeapDatetime {
9    pub timestamp: i64,
10    pub nanoseconds: u32,
11    pub tz: Tz,
12    pub calendar: Calendar,
13}
14
15impl NoLeapDatetime {
16    pub fn new(timestamp: i64, nanoseconds: u32, tz: Tz) -> Self {
17        Self {
18            timestamp,
19            nanoseconds,
20            tz,
21            calendar: Calendar::NoLeap,
22        }
23    }
24}
25impl IsLeap for NoLeapDatetime {
26    fn is_leap(_year: i64) -> bool {
27        false
28    }
29}
30
31impl CalendarDatetime for NoLeapDatetime {
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::<NoLeapDatetime>(self.timestamp))
46    }
47}
48impl CalendarDatetimeCreator for NoLeapDatetime {
49    fn from_timestamp(timestamp: i64, nanoseconds: u32) -> Self {
50        Self {
51            timestamp,
52            nanoseconds,
53            tz: Tz::new(0, 0).unwrap(),
54            calendar: Calendar::NoLeap,
55        }
56    }
57    fn from_ymd_hms(
58        year: i64,
59        month: u8,
60        day: u8,
61        hour: u8,
62        minute: u8,
63        second: f32,
64    ) -> Result<Self, crate::errors::Error> {
65        let (mut timestamp, nanoseconds) = get_timestamp_from_hms(hour, minute, second)?;
66        timestamp += get_timestamp_from_ymd::<NoLeapDatetime>(year, month, day)?;
67        Ok(Self {
68            timestamp,
69            nanoseconds,
70            tz: Tz::new(0, 0).unwrap(),
71            calendar: Calendar::NoLeap,
72        })
73    }
74}