utc_cron_expr/calendar/impl.rs
1use super::*;
2
3/// Construction and conversion methods for [`UtcDateTime`].
4impl UtcDateTime {
5 /// Decomposes Unix epoch seconds into a [`UtcDateTime`].
6 ///
7 /// # Arguments
8 ///
9 /// - `i64` - Signed Unix timestamp in seconds.
10 ///
11 /// # Returns
12 ///
13 /// - `Self` - Decomposed UTC civil date and time.
14 #[inline(always)]
15 pub const fn from_unix_secs(secs: i64) -> Self {
16 let days: i64 = secs.div_euclid(86_400);
17 let sod: u32 = secs.rem_euclid(86_400) as u32;
18 let (year, month, day): (i32, u8, u8) = civil_from_days(days);
19 let weekday: u8 = weekday_from_days(days);
20 let hour: u8 = (sod / 3600) as u8;
21 let minute: u8 = ((sod % 3600) / 60) as u8;
22 let second: u8 = (sod % 60) as u8;
23 Self {
24 year,
25 month,
26 day,
27 weekday,
28 hour,
29 minute,
30 second,
31 }
32 }
33
34 /// Converts this [`UtcDateTime`] back to Unix epoch seconds.
35 ///
36 /// # Returns
37 ///
38 /// - `i64` - Signed Unix timestamp in seconds.
39 #[inline(always)]
40 pub const fn to_unix_secs(self) -> i64 {
41 let Self {
42 year,
43 month,
44 day,
45 hour,
46 minute,
47 second,
48 ..
49 } = self;
50 let days: i64 = days_from_civil(year, month, day);
51 days * 86_400 + (hour as i64) * 3600 + (minute as i64) * 60 + (second as i64)
52 }
53
54 /// Returns the full Gregorian year.
55 ///
56 /// # Returns
57 ///
58 /// - `i32` - Year.
59 #[inline(always)]
60 pub const fn get_year(self) -> i32 {
61 let Self { year, .. } = self;
62 year
63 }
64
65 /// Returns the month (`1..=12`).
66 ///
67 /// # Returns
68 ///
69 /// - `u8` - Month.
70 #[inline(always)]
71 pub const fn get_month(self) -> u8 {
72 let Self { month, .. } = self;
73 month
74 }
75
76 /// Returns the day of month (`1..=31`).
77 ///
78 /// # Returns
79 ///
80 /// - `u8` - Day of month.
81 #[inline(always)]
82 pub const fn get_day(self) -> u8 {
83 let Self { day, .. } = self;
84 day
85 }
86
87 /// Returns the weekday (`0 = Sunday ..= 6 = Saturday`).
88 ///
89 /// # Returns
90 ///
91 /// - `u8` - Weekday.
92 #[inline(always)]
93 pub const fn get_weekday(self) -> u8 {
94 let Self { weekday, .. } = self;
95 weekday
96 }
97
98 /// Returns the hour (`0..=23`).
99 ///
100 /// # Returns
101 ///
102 /// - `u8` - Hour.
103 #[inline(always)]
104 pub const fn get_hour(self) -> u8 {
105 let Self { hour, .. } = self;
106 hour
107 }
108
109 /// Returns the minute (`0..=59`).
110 ///
111 /// # Returns
112 ///
113 /// - `u8` - Minute.
114 #[inline(always)]
115 pub const fn get_minute(self) -> u8 {
116 let Self { minute, .. } = self;
117 minute
118 }
119
120 /// Returns the second (`0..=59`).
121 ///
122 /// # Returns
123 ///
124 /// - `u8` - Second.
125 #[inline(always)]
126 pub const fn get_second(self) -> u8 {
127 let Self { second, .. } = self;
128 second
129 }
130}
131
132/// Formats [`UtcDateTime`] as an ISO-8601 UTC timestamp (`YYYY-MM-DDTHH:MM:SSZ`).
133impl fmt::Display for UtcDateTime {
134 /// Writes the UTC timestamp to the formatter.
135 ///
136 /// # Arguments
137 ///
138 /// - `&mut fmt::Formatter<'_>` - Output formatter.
139 ///
140 /// # Returns
141 ///
142 /// - `fmt::Result` - Formatting result.
143 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
144 write!(
145 f,
146 "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}Z",
147 self.get_year(),
148 self.get_month(),
149 self.get_day(),
150 self.get_hour(),
151 self.get_minute(),
152 self.get_second(),
153 )
154 }
155}