automapper_validation/eval/
timezone.rs1use chrono::{Datelike, NaiveDate};
7
8use super::evaluator::ConditionResult;
9
10pub fn parse_dtm303(value: &str) -> Option<i64> {
16 let s = value.trim();
17 if s.len() < 12 {
18 return None;
19 }
20 let year: i32 = s.get(0..4).and_then(|v| v.parse().ok())?;
21 let month: u32 = s.get(4..6).and_then(|v| v.parse().ok())?;
22 let day: u32 = s.get(6..8).and_then(|v| v.parse().ok())?;
23 let hour: u32 = s.get(8..10).and_then(|v| v.parse().ok())?;
24 let minute: u32 = s.get(10..12).and_then(|v| v.parse().ok())?;
25 let dt = NaiveDate::from_ymd_opt(year, month, day)?.and_hms_opt(hour, minute, 0)?;
26 Some(dt.and_utc().timestamp() / 60)
27}
28
29pub fn dtm_start_utc_min(value: &str, format: &str) -> Option<i64> {
35 match format {
36 "303" => parse_dtm303(value),
37 "102" => {
38 let s = value.trim();
39 if s.len() != 8 {
40 return None;
41 }
42 let utc_midnight = parse_dtm303(&format!("{s}0000"))?;
43 let mesz_midnight = utc_midnight - 120;
46 let mesz = chrono::DateTime::from_timestamp(mesz_midnight * 60, 0)?
47 .format("%Y%m%d%H%M")
48 .to_string();
49 match is_mesz_utc(&mesz) {
50 ConditionResult::True => Some(mesz_midnight),
51 ConditionResult::False => Some(utc_midnight - 60),
52 ConditionResult::Unknown => None,
53 }
54 }
55 _ => None,
56 }
57}
58
59pub fn dtm_after(value: &str, format: &str, reference_303: &str) -> ConditionResult {
67 if !matches!(format, "303" | "102") {
68 return ConditionResult::Unknown;
69 }
70 let Some(reference) = parse_dtm303(reference_303) else {
71 return ConditionResult::Unknown;
72 };
73 match dtm_start_utc_min(value, format) {
74 Some(v) => ConditionResult::from(v > reference),
75 None => ConditionResult::False,
76 }
77}
78
79pub fn dtm_not_after(value: &str, format: Option<&str>, reference_303: &str) -> ConditionResult {
90 let Some(reference) = parse_dtm303(reference_303) else {
91 return ConditionResult::Unknown;
92 };
93 match format {
94 Some(format @ ("303" | "102")) => match dtm_start_utc_min(value, format) {
95 Some(v) => ConditionResult::from(v <= reference),
96 None => ConditionResult::False,
97 },
98 Some("304") => {
99 let seconds = value.get(12..14).and_then(|s| s.parse::<u32>().ok());
100 match (parse_dtm303(value), seconds) {
101 (Some(v), Some(seconds)) => {
102 ConditionResult::from(v < reference || (v == reference && seconds == 0))
103 }
104 _ => ConditionResult::False,
105 }
106 }
107 _ => ConditionResult::from(value <= reference_303),
108 }
109}
110
111pub fn dst_transitions_between(a_utc_min: i64, b_utc_min: i64) -> i32 {
123 let (lo, hi) = if a_utc_min <= b_utc_min {
124 (a_utc_min, b_utc_min)
125 } else {
126 (b_utc_min, a_utc_min)
127 };
128 if lo == hi {
129 return 0;
130 }
131 let lo_year = year_of_epoch_min(lo);
132 let hi_year = year_of_epoch_min(hi);
133 let mut delta: i32 = 0;
134 for y in lo_year..=hi_year {
135 let spring_forward = dst_instant_min(y, 3);
136 if lo <= spring_forward && spring_forward < hi {
137 delta += 60;
138 }
139 let fall_back = dst_instant_min(y, 10);
140 if lo <= fall_back && fall_back < hi {
141 delta -= 60;
142 }
143 }
144 delta
145}
146
147fn dst_instant_min(year: i32, month: u32) -> i64 {
148 let date = last_sunday_of_month(year, month);
149 date.and_hms_opt(1, 0, 0).unwrap().and_utc().timestamp() / 60
150}
151
152fn year_of_epoch_min(epoch_min: i64) -> i32 {
153 chrono::DateTime::from_timestamp(epoch_min * 60, 0)
154 .map(|dt| dt.date_naive().year())
155 .unwrap_or(1970)
156}
157
158pub fn is_mesz_utc(dtm_value: &str) -> ConditionResult {
163 let s = dtm_value.trim();
164 if s.len() < 8 {
165 return ConditionResult::Unknown;
166 }
167
168 let year: i32 = match s[0..4].parse() {
169 Ok(v) => v,
170 Err(_) => return ConditionResult::Unknown,
171 };
172 let month: u32 = match s[4..6].parse() {
173 Ok(v) => v,
174 Err(_) => return ConditionResult::Unknown,
175 };
176 let day: u32 = match s[6..8].parse() {
177 Ok(v) => v,
178 Err(_) => return ConditionResult::Unknown,
179 };
180 let hour: u32 = if s.len() >= 10 {
181 match s[8..10].parse() {
182 Ok(v) => v,
183 Err(_) => return ConditionResult::Unknown,
184 }
185 } else {
186 0
187 };
188 let minute: u32 = if s.len() >= 12 {
189 match s[10..12].parse() {
190 Ok(v) => v,
191 Err(_) => return ConditionResult::Unknown,
192 }
193 } else {
194 0
195 };
196
197 let Some(dt) =
198 NaiveDate::from_ymd_opt(year, month, day).and_then(|d| d.and_hms_opt(hour, minute, 0))
199 else {
200 return ConditionResult::Unknown;
201 };
202
203 let mesz_start = last_sunday_of_month(year, 3).and_hms_opt(1, 0, 0).unwrap();
204 let mesz_end = last_sunday_of_month(year, 10).and_hms_opt(1, 0, 0).unwrap();
205
206 ConditionResult::from(dt >= mesz_start && dt < mesz_end)
207}
208
209pub fn is_mez_utc(dtm_value: &str) -> ConditionResult {
213 match is_mesz_utc(dtm_value) {
214 ConditionResult::True => ConditionResult::False,
215 ConditionResult::False => ConditionResult::True,
216 ConditionResult::Unknown => ConditionResult::Unknown,
217 }
218}
219
220fn last_sunday_of_month(year: i32, month: u32) -> NaiveDate {
222 let last_day = NaiveDate::from_ymd_opt(year, month + 1, 1)
224 .unwrap_or_else(|| NaiveDate::from_ymd_opt(year + 1, 1, 1).unwrap())
225 .pred_opt()
226 .unwrap();
227 let days_since_sunday = last_day.weekday().num_days_from_sunday();
228 last_day - chrono::Duration::days(days_since_sunday as i64)
229}
230
231#[cfg(test)]
232mod tests {
233
234 #[test]
235 fn not_after_compares_each_format_as_what_it_means() {
236 let reference = "202411152300+00"; let not_after = |v: &str, f: Option<&str>| dtm_not_after(v, f, reference);
238 assert_eq!(
239 not_after("20241115230000+00", Some("304")),
240 ConditionResult::True
241 );
242 assert_eq!(
243 not_after("20241115230001+00", Some("304")),
244 ConditionResult::False
245 );
246 assert_eq!(
247 not_after("202411152300+00", Some("303")),
248 ConditionResult::True
249 );
250 assert_eq!(
251 not_after("202411152301+00", Some("303")),
252 ConditionResult::False
253 );
254 assert_eq!(not_after("20241116", Some("102")), ConditionResult::True);
256 assert_eq!(not_after("20241115", Some("102")), ConditionResult::True);
257 assert_eq!(not_after("20241117", Some("102")), ConditionResult::False);
258 assert_eq!(not_after("202411", Some("610")), ConditionResult::True);
259 assert_eq!(not_after("202412", Some("610")), ConditionResult::False);
260 assert_eq!(not_after("1", Some("102")), ConditionResult::False);
261 }
262
263 use super::*;
264
265 #[test]
266 fn test_known_mesz_date() {
267 assert_eq!(is_mesz_utc("202607151200"), ConditionResult::True);
268 }
269
270 #[test]
271 fn test_known_mez_date() {
272 assert_eq!(is_mesz_utc("202601151200"), ConditionResult::False);
273 }
274
275 #[test]
276 fn test_march_transition_2026() {
277 assert_eq!(is_mesz_utc("202603290059"), ConditionResult::False);
279 assert_eq!(is_mesz_utc("202603290100"), ConditionResult::True);
280 }
281
282 #[test]
283 fn test_october_transition_2026() {
284 assert_eq!(is_mesz_utc("202610250059"), ConditionResult::True);
286 assert_eq!(is_mesz_utc("202610250100"), ConditionResult::False);
287 }
288
289 #[test]
290 fn test_short_input() {
291 assert_eq!(is_mesz_utc("2026"), ConditionResult::Unknown);
292 assert_eq!(is_mesz_utc(""), ConditionResult::Unknown);
293 assert_eq!(is_mesz_utc("20260715"), ConditionResult::True);
294 assert_eq!(is_mesz_utc("20260115"), ConditionResult::False);
295 }
296
297 #[test]
298 fn test_invalid_input_returns_unknown() {
299 assert_eq!(is_mesz_utc("abcdefghijkl"), ConditionResult::Unknown);
300 assert_eq!(is_mesz_utc("202613151200"), ConditionResult::Unknown);
301 assert_eq!(is_mesz_utc("202601321200"), ConditionResult::Unknown);
302 }
303
304 #[test]
305 fn test_is_mez_complements_is_mesz() {
306 assert_eq!(is_mez_utc("202607151200"), ConditionResult::False);
307 assert_eq!(is_mez_utc("202601151200"), ConditionResult::True);
308 assert_eq!(is_mez_utc("short"), ConditionResult::Unknown);
309 }
310
311 #[test]
312 fn test_value_with_timezone_suffix() {
313 assert_eq!(is_mesz_utc("202607151200UTC"), ConditionResult::True);
314 assert_eq!(is_mesz_utc("202601151200303"), ConditionResult::False);
315 }
316
317 #[test]
318 fn test_last_sunday_of_march_2026() {
319 assert_eq!(
320 last_sunday_of_month(2026, 3),
321 NaiveDate::from_ymd_opt(2026, 3, 29).unwrap()
322 );
323 }
324
325 #[test]
326 fn test_last_sunday_of_october_2026() {
327 assert_eq!(
328 last_sunday_of_month(2026, 10),
329 NaiveDate::from_ymd_opt(2026, 10, 25).unwrap()
330 );
331 }
332
333 #[test]
334 fn test_parse_dtm303_valid() {
335 let m = parse_dtm303("202601151200").unwrap();
337 let expected = NaiveDate::from_ymd_opt(2026, 1, 15)
339 .unwrap()
340 .and_hms_opt(12, 0, 0)
341 .unwrap()
342 .and_utc()
343 .timestamp()
344 / 60;
345 assert_eq!(m, expected);
346 }
347
348 #[test]
349 fn test_parse_dtm303_ignores_tz_suffix() {
350 let a = parse_dtm303("202603290100").unwrap();
351 let b = parse_dtm303("202603290100+00").unwrap();
352 let c = parse_dtm303("202603290100UTC").unwrap();
353 assert_eq!(a, b);
354 assert_eq!(a, c);
355 }
356
357 #[test]
358 fn test_parse_dtm303_rejects_short() {
359 assert!(parse_dtm303("20260115").is_none());
360 assert!(parse_dtm303("").is_none());
361 }
362
363 #[test]
364 fn test_parse_dtm303_rejects_invalid() {
365 assert!(parse_dtm303("2026ZZ151200").is_none());
366 assert!(parse_dtm303("202613151200").is_none());
367 }
368
369 #[test]
370 fn test_dst_no_transition_same_day() {
371 let a = parse_dtm303("202601150800").unwrap();
372 let b = parse_dtm303("202601151200").unwrap();
373 assert_eq!(dst_transitions_between(a, b), 0);
374 }
375
376 #[test]
377 fn test_dst_spring_forward_2026() {
378 let before = parse_dtm303("202603290030").unwrap();
380 let after = parse_dtm303("202603290130").unwrap();
381 assert_eq!(dst_transitions_between(before, after), 60);
382 assert_eq!(dst_transitions_between(after, before), 60);
384 }
385
386 #[test]
387 fn test_dst_fall_back_2026() {
388 let before = parse_dtm303("202610250030").unwrap();
390 let after = parse_dtm303("202610250130").unwrap();
391 assert_eq!(dst_transitions_between(before, after), -60);
392 assert_eq!(dst_transitions_between(after, before), -60);
393 }
394
395 #[test]
396 fn test_dst_transition_at_exact_boundary() {
397 let at_boundary = parse_dtm303("202603290100").unwrap();
399 assert_eq!(dst_transitions_between(at_boundary, at_boundary), 0);
400 let after = parse_dtm303("202603290200").unwrap();
402 assert_eq!(dst_transitions_between(at_boundary, after), 60);
403 }
404
405 #[test]
406 fn test_dst_both_transitions_cancel() {
407 let winter_a = parse_dtm303("202601010000").unwrap();
409 let winter_b = parse_dtm303("202612310000").unwrap();
410 assert_eq!(dst_transitions_between(winter_a, winter_b), 0);
411 }
412
413 #[test]
414 fn test_different_years() {
415 assert_eq!(
416 last_sunday_of_month(2025, 3),
417 NaiveDate::from_ymd_opt(2025, 3, 30).unwrap()
418 );
419 assert_eq!(
420 last_sunday_of_month(2025, 10),
421 NaiveDate::from_ymd_opt(2025, 10, 26).unwrap()
422 );
423 assert_eq!(
424 last_sunday_of_month(2024, 3),
425 NaiveDate::from_ymd_opt(2024, 3, 31).unwrap()
426 );
427 assert_eq!(
428 last_sunday_of_month(2024, 10),
429 NaiveDate::from_ymd_opt(2024, 10, 27).unwrap()
430 );
431 }
432}