1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::calendars::Calendar;
use crate::datetimes::traits::{CalendarDatetime, IsLeap};
use crate::timezone::Tz;
use crate::utils::{get_timestamp_from_hms, get_timestamp_from_ymd, get_ymd_hms_from_timestamp};

use super::traits::CalendarDatetimeCreator;

pub struct AllLeapDatetime {
    pub timestamp: i64,
    pub nanoseconds: u32,
    pub tz: Tz,
    pub calendar: Calendar,
}

impl AllLeapDatetime {
    pub fn new(timestamp: i64, nanoseconds: u32, tz: Tz) -> Self {
        Self {
            timestamp,
            nanoseconds,
            tz,
            calendar: Calendar::AllLeap,
        }
    }
}
impl IsLeap for AllLeapDatetime {
    fn is_leap(_year: i64) -> bool {
        true
    }
}

impl CalendarDatetime for AllLeapDatetime {
    fn timestamp(&self) -> i64 {
        self.timestamp
    }
    fn nanoseconds(&self) -> u32 {
        self.nanoseconds
    }
    fn calendar(&self) -> Calendar {
        self.calendar
    }
    fn timezone(&self) -> Tz {
        self.tz
    }
    fn ymd_hms(&self) -> Result<(i64, u8, u8, u8, u8, u8), crate::errors::Error> {
        Ok(get_ymd_hms_from_timestamp::<AllLeapDatetime>(
            self.timestamp,
        ))
    }
}
impl CalendarDatetimeCreator for AllLeapDatetime {
    fn from_timestamp(timestamp: i64, nanoseconds: u32) -> Self {
        Self {
            timestamp,
            nanoseconds,
            tz: Tz::new(0, 0).unwrap(),
            calendar: Calendar::AllLeap,
        }
    }
    fn from_ymd_hms(
        year: i64,
        month: u8,
        day: u8,
        hour: u8,
        minute: u8,
        second: f32,
    ) -> Result<Self, crate::errors::Error> {
        let (mut timestamp, nanoseconds) = get_timestamp_from_hms(hour, minute, second)?;
        timestamp += get_timestamp_from_ymd::<AllLeapDatetime>(year, month, day)?;
        Ok(Self {
            timestamp,
            nanoseconds,
            tz: Tz::new(0, 0).unwrap(),
            calendar: Calendar::AllLeap,
        })
    }
}