BlueGit

ict rusty-teams

Files

rusty-teams / crates / core / src / time_zone.rs

time_zone.rs

time_zone.rs 8.7 KiB Download Raw
1use chrono::{DateTime, Datelike, Duration, FixedOffset, NaiveDate, TimeZone, Utc, Weekday};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4enum Dst {
5 None,
6 Europe,
7 NorthAmerica,
8 Australia,
9 NewZealand,
10}
11
12const ZONES: &[(&str, i32, Dst)] = &[
13 ("UTC", 0, Dst::None),
14 ("Dateline Standard Time", -720, Dst::None),
15 ("UTC-11", -660, Dst::None),
16 ("Hawaiian Standard Time", -600, Dst::None),
17 ("Alaskan Standard Time", -540, Dst::NorthAmerica),
18 ("Pacific Standard Time", -480, Dst::NorthAmerica),
19 ("US Mountain Standard Time", -420, Dst::None),
20 ("Mountain Standard Time", -420, Dst::NorthAmerica),
21 ("Central Standard Time", -360, Dst::NorthAmerica),
22 ("Central America Standard Time", -360, Dst::None),
23 ("Canada Central Standard Time", -360, Dst::None),
24 ("Mexico Standard Time", -360, Dst::None),
25 ("Eastern Standard Time", -300, Dst::NorthAmerica),
26 ("US Eastern Standard Time", -300, Dst::NorthAmerica),
27 ("SA Pacific Standard Time", -300, Dst::None),
28 ("Atlantic Standard Time", -240, Dst::NorthAmerica),
29 ("Newfoundland Standard Time", -210, Dst::NorthAmerica),
30 ("E. South America Standard Time", -180, Dst::None),
31 ("Argentina Standard Time", -180, Dst::None),
32 ("GMT Standard Time", 0, Dst::Europe),
33 ("Greenwich Standard Time", 0, Dst::None),
34 ("W. Europe Standard Time", 60, Dst::Europe),
35 ("Central Europe Standard Time", 60, Dst::Europe),
36 ("Romance Standard Time", 60, Dst::Europe),
37 ("Central European Standard Time", 60, Dst::Europe),
38 ("W. Central Africa Standard Time", 60, Dst::None),
39 ("South Africa Standard Time", 120, Dst::None),
40 ("E. Europe Standard Time", 120, Dst::Europe),
41 ("GTB Standard Time", 120, Dst::Europe),
42 ("FLE Standard Time", 120, Dst::Europe),
43 ("Turkey Standard Time", 180, Dst::None),
44 ("Arab Standard Time", 180, Dst::None),
45 ("Russian Standard Time", 180, Dst::None),
46 ("E. Africa Standard Time", 180, Dst::None),
47 ("Iran Standard Time", 210, Dst::None),
48 ("Arabian Standard Time", 240, Dst::None),
49 ("Pakistan Standard Time", 300, Dst::None),
50 ("West Asia Standard Time", 300, Dst::None),
51 ("India Standard Time", 330, Dst::None),
52 ("Sri Lanka Standard Time", 330, Dst::None),
53 ("Nepal Standard Time", 345, Dst::None),
54 ("Central Asia Standard Time", 360, Dst::None),
55 ("Bangladesh Standard Time", 360, Dst::None),
56 ("SE Asia Standard Time", 420, Dst::None),
57 ("China Standard Time", 480, Dst::None),
58 ("Singapore Standard Time", 480, Dst::None),
59 ("W. Australia Standard Time", 480, Dst::None),
60 ("Taipei Standard Time", 480, Dst::None),
61 ("Tokyo Standard Time", 540, Dst::None),
62 ("Korea Standard Time", 540, Dst::None),
63 ("AUS Central Standard Time", 570, Dst::None),
64 ("Cen. Australia Standard Time", 570, Dst::Australia),
65 ("E. Australia Standard Time", 600, Dst::None),
66 ("AUS Eastern Standard Time", 600, Dst::Australia),
67 ("Tasmania Standard Time", 600, Dst::Australia),
68 ("New Zealand Standard Time", 720, Dst::NewZealand),
69];
70
71/// `None` for a zone outside the table.
72pub fn local_time(zone_name: &str, now: DateTime<Utc>) -> Option<DateTime<FixedOffset>> {
73 let (_, standard_minutes, dst) = ZONES
74 .iter()
75 .find(|(name, _, _)| name.eq_ignore_ascii_case(zone_name.trim()))?;
76 let saving = if is_daylight_time(*dst, *standard_minutes, now) {
77 60
78 } else {
79 0
80 };
81 let offset = FixedOffset::east_opt((standard_minutes + saving) * 60)?;
82 Some(now.with_timezone(&offset))
83}
84
85fn is_daylight_time(rule: Dst, standard_minutes: i32, now: DateTime<Utc>) -> bool {
86 let year = now.year();
87 let utc = |date: NaiveDate, local_standard_minutes: i32| {
88 Utc.from_utc_datetime(
89 &date
90 .and_hms_opt(0, 0, 0)
91 .expect("midnight exists")
92 .checked_add_signed(Duration::minutes(i64::from(
93 local_standard_minutes - standard_minutes,
94 )))
95 .expect("transition is in range"),
96 )
97 };
98 let sunday = |month: u32, nth: Option<u32>| match nth {
99 Some(nth) => nth_sunday(year, month, nth),
100 None => last_sunday(year, month),
101 };
102 match rule {
103 Dst::None => false,
104 Dst::Europe => {
105 utc(sunday(3, None), 60 + standard_minutes) <= now
106 && now < utc(sunday(10, None), 60 + standard_minutes)
107 }
108 Dst::NorthAmerica => {
109 utc(sunday(3, Some(2)), 120) <= now && now < utc(sunday(11, Some(1)), 60)
110 }
111 Dst::Australia => {
112 utc(sunday(10, Some(1)), 120) <= now || now < utc(sunday(4, Some(1)), 120)
113 }
114 Dst::NewZealand => utc(sunday(9, None), 120) <= now || now < utc(sunday(4, Some(1)), 120),
115 }
116}
117
118fn nth_sunday(year: i32, month: u32, nth: u32) -> NaiveDate {
119 let first = NaiveDate::from_ymd_opt(year, month, 1).expect("month is valid");
120 let to_sunday = (7 - first.weekday().num_days_from_sunday()) % 7;
121 first + Duration::days(i64::from(to_sunday + 7 * (nth - 1)))
122}
123
124fn last_sunday(year: i32, month: u32) -> NaiveDate {
125 let (next_year, next_month) = if month == 12 {
126 (year + 1, 1)
127 } else {
128 (year, month + 1)
129 };
130 let mut day = NaiveDate::from_ymd_opt(next_year, next_month, 1).expect("month is valid")
131 - Duration::days(1);
132 while day.weekday() != Weekday::Sun {
133 day -= Duration::days(1);
134 }
135 day
136}
137
138#[cfg(test)]
139mod tests {
140 use chrono::TimeZone;
141
142 use super::*;
143
144 fn clock(
145 zone: &str,
146 year: i32,
147 month: u32,
148 day: u32,
149 hour: u32,
150 minute: u32,
151 ) -> Option<String> {
152 let now = Utc
153 .with_ymd_and_hms(year, month, day, hour, minute, 0)
154 .unwrap();
155 local_time(zone, now).map(|time| time.format("%H:%M %z").to_string())
156 }
157
158 #[test]
159 fn europe_switches_on_the_last_sunday_at_one_utc() {
160 let zone = "W. Europe Standard Time";
161 assert_eq!(
162 clock(zone, 2026, 1, 15, 12, 0).as_deref(),
163 Some("13:00 +0100")
164 );
165 assert_eq!(
166 clock(zone, 2026, 3, 29, 0, 59).as_deref(),
167 Some("01:59 +0100")
168 );
169 assert_eq!(
170 clock(zone, 2026, 3, 29, 1, 0).as_deref(),
171 Some("03:00 +0200")
172 );
173 assert_eq!(
174 clock(zone, 2026, 10, 9, 12, 0).as_deref(),
175 Some("14:00 +0200")
176 );
177 assert_eq!(
178 clock(zone, 2026, 10, 25, 0, 59).as_deref(),
179 Some("02:59 +0200")
180 );
181 assert_eq!(
182 clock(zone, 2026, 10, 25, 1, 0).as_deref(),
183 Some("02:00 +0100")
184 );
185 }
186
187 #[test]
188 fn gmt_and_eastern_europe_follow_the_same_utc_instant() {
189 assert_eq!(
190 clock("GMT Standard Time", 2026, 7, 1, 12, 0).as_deref(),
191 Some("13:00 +0100")
192 );
193 assert_eq!(
194 clock("GTB Standard Time", 2026, 3, 29, 1, 0).as_deref(),
195 Some("04:00 +0300")
196 );
197 }
198
199 #[test]
200 fn north_america_switches_on_the_second_sunday_of_march() {
201 let zone = "Pacific Standard Time";
202 assert_eq!(
203 clock(zone, 2026, 3, 8, 9, 59).as_deref(),
204 Some("01:59 -0800")
205 );
206 assert_eq!(
207 clock(zone, 2026, 3, 8, 10, 0).as_deref(),
208 Some("03:00 -0700")
209 );
210 assert_eq!(
211 clock(zone, 2026, 11, 1, 8, 59).as_deref(),
212 Some("01:59 -0700")
213 );
214 assert_eq!(
215 clock(zone, 2026, 11, 1, 9, 0).as_deref(),
216 Some("01:00 -0800")
217 );
218 }
219
220 #[test]
221 fn southern_zones_are_daylight_time_over_the_new_year() {
222 assert_eq!(
223 clock("AUS Eastern Standard Time", 2026, 1, 15, 0, 0).as_deref(),
224 Some("11:00 +1100")
225 );
226 assert_eq!(
227 clock("AUS Eastern Standard Time", 2026, 7, 15, 0, 0).as_deref(),
228 Some("10:00 +1000")
229 );
230 assert_eq!(
231 clock("New Zealand Standard Time", 2026, 12, 1, 0, 0).as_deref(),
232 Some("13:00 +1300")
233 );
234 assert_eq!(
235 clock("New Zealand Standard Time", 2026, 6, 1, 0, 0).as_deref(),
236 Some("12:00 +1200")
237 );
238 }
239
240 #[test]
241 fn zones_without_daylight_time_keep_their_offset() {
242 assert_eq!(
243 clock("India Standard Time", 2026, 7, 1, 12, 0).as_deref(),
244 Some("17:30 +0530")
245 );
246 assert_eq!(
247 clock("UTC", 2026, 7, 1, 12, 0).as_deref(),
248 Some("12:00 +0000")
249 );
250 }
251
252 #[test]
253 fn unknown_zones_have_no_clock() {
254 assert_eq!(clock("Custom Zone", 2026, 7, 1, 12, 0), None);
255 }
256
257 #[test]
258 fn zone_names_ignore_case() {
259 assert!(clock("w. europe standard time", 2026, 7, 1, 12, 0).is_some());
260 }
261}