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