oxedyne/fe2o3/fe2o3_datime/tests/moment.rs
15.2 KiB, 1 run
created by r1870400018:63091, which is this file's identity for as long as the history lasts, whatever it is later renamed to
download · who wrote it · its history
| 1 | //! The reader of a moment a person typed, checked against GNU date: what it |
| 2 | //! reads, in which zone, and what it refuses. |
| 3 | |
| 4 | mod common; |
| 5 | |
| 6 | use common::*; |
| 7 | |
| 8 | use oxedyne_fe2o3_core::prelude::*; |
| 9 | use oxedyne_fe2o3_datime::{ |
| 10 | parser::moment::{ |
| 11 | self, |
| 12 | Moment, |
| 13 | FORMS, |
| 14 | }, |
| 15 | time::CalClockZone, |
| 16 | }; |
| 17 | |
| 18 | const ZONES: [&str; 5] = [ |
| 19 | "Australia/Perth", |
| 20 | "America/New_York", |
| 21 | "Europe/London", |
| 22 | "Australia/Lord_Howe", |
| 23 | "Asia/Kathmandu", |
| 24 | ]; |
| 25 | |
| 26 | const MICROS: i64 = 1_000_000; |
| 27 | |
| 28 | // 2026-10-01 18:23:30.25 UTC, which is already the 2nd of October at Perth, Lord |
| 29 | // Howe and Kathmandu and still the 1st at New York and London. |
| 30 | const NOW: i64 = 1_790_879_010 * MICROS + 250_000; |
| 31 | |
| 32 | fn zones() -> Outcome<Vec<(&'static str, CalClockZone)>> { |
| 33 | let mut zones = Vec::new(); |
| 34 | for name in ZONES { |
| 35 | if let Some(zone) = res!(zone_from(ZONEINFO, name)) { |
| 36 | zones.push((name, zone)); |
| 37 | } |
| 38 | } |
| 39 | Ok(zones) |
| 40 | } |
| 41 | |
| 42 | // GNU date's reading of one string, in UTC seconds. |
| 43 | fn gnu(tz: &str, said: &str) -> Outcome<Option<i64>> { |
| 44 | let out = res!(date_batch(tz, None, &[said.to_string()], "+%s")); |
| 45 | Ok(out[0].as_deref().and_then(|s| s.parse().ok())) |
| 46 | } |
| 47 | |
| 48 | // The local date on which an instant falls, as GNU date writes it. |
| 49 | fn gnu_day(tz: &str, secs: i64) -> Outcome<String> { |
| 50 | let out = res!(date_batch(tz, None, &[fmt!("@{}", secs)], "+%F")); |
| 51 | Ok(res!(out[0].clone().ok_or_else(|| err!("GNU date refused @{}", secs; Invalid, Mismatch)))) |
| 52 | } |
| 53 | |
| 54 | // What a refusal says, or a note that there was none. |
| 55 | fn refusal(said: Outcome<i64>) -> String { |
| 56 | match said { |
| 57 | Ok(micros) => fmt!("no refusal: {}", micros), |
| 58 | Err(e) => fmt!("{}", e), |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | #[test] |
| 63 | fn a_full_stamp_with_no_offset_is_read_in_the_zone() -> Outcome<()> { |
| 64 | if skip_without_oracle() { return Ok(()); } |
| 65 | for (name, zone) in res!(zones()) { |
| 66 | for said in ["2026-06-15 17:30:15", "2026-06-15T17:30", "2026-01-15 00:00:00", "2026-12-31 23:59:59", |
| 67 | "2024-02-29 12:00"] |
| 68 | { |
| 69 | let want = res!(gnu(name, &said.replace('T', " "))).map(|s| s * MICROS); |
| 70 | assert_eq!(Some(res!(moment::read(said, NOW, &zone))), want, "{}: '{}'", name, said); |
| 71 | } |
| 72 | // The fraction rides along. |
| 73 | let a = res!(moment::read("2026-06-15 17:30:15", NOW, &zone)); |
| 74 | let b = res!(moment::read("2026-06-15 17:30:15.123456", NOW, &zone)); |
| 75 | let c = res!(moment::read("2026-06-15 17:30:15.5", NOW, &zone)); |
| 76 | assert_eq!((b - a, c - a), (123_456, 500_000), "{}", name); |
| 77 | } |
| 78 | Ok(()) |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn a_stamp_that_gives_its_offset_ignores_the_zone() -> Outcome<()> { |
| 83 | if skip_without_oracle() { return Ok(()); } |
| 84 | let cases = [ |
| 85 | "2026-06-15 17:30:15Z", |
| 86 | "2026-06-15 17:30:15 Z", |
| 87 | "2026-06-15T17:30:15Z", |
| 88 | "2026-06-15t17:30:15z", |
| 89 | "2026-06-15 17:30:15+08:00", |
| 90 | "2026-06-15 17:30:15 +08:00", |
| 91 | "2026-06-15T17:30:15 -04:00", |
| 92 | "2026-06-15 17:30:15 +0545", |
| 93 | "2026-06-15 17:30 -03:30", |
| 94 | "2026-11-01 01:30:00-04:00", |
| 95 | "2026-11-01 01:30:00-05:00", |
| 96 | "0001-01-01 00:00:00Z", |
| 97 | "9999-12-31 23:59:59Z", |
| 98 | "2000-02-29 12:00Z", |
| 99 | ]; |
| 100 | // The one answer must come from every zone alike, Perth to New York. |
| 101 | for said in cases { |
| 102 | let spaced = said.replace('T', " ").replace('t', " ").replace(" Z", "Z").replace(" z", "Z"); |
| 103 | let want = res!(gnu("UTC", &spaced.replace('z', "Z"))).map(|s| s * MICROS); |
| 104 | assert!(want.is_some(), "GNU date would not read '{}'", spaced); |
| 105 | for (name, zone) in res!(zones()) { |
| 106 | assert_eq!(Some(res!(moment::read(said, NOW, &zone))), want, "{}: '{}'", name, said); |
| 107 | } |
| 108 | } |
| 109 | Ok(()) |
| 110 | } |
| 111 | |
| 112 | #[test] |
| 113 | fn a_clock_time_is_today_unless_that_is_still_to_come() -> Outcome<()> { |
| 114 | if skip_without_oracle() { return Ok(()); } |
| 115 | for (name, zone) in res!(zones()) { |
| 116 | let day = res!(gnu_day(name, NOW.div_euclid(MICROS))); |
| 117 | let here_now = { |
| 118 | // The local time of day of NOW, as GNU writes it. |
| 119 | let out = res!(date_batch(name, None, &[fmt!("@{}", NOW.div_euclid(MICROS))], "+%H:%M:%S")); |
| 120 | out[0].clone().unwrap_or_default() |
| 121 | }; |
| 122 | for said in ["00:00", "03:15", "09:30:45", "12:00", "17:30", "23:59:59"] { |
| 123 | let today = res!(gnu(name, &fmt!("{} {}", day, said))).map(|s| s * MICROS); |
| 124 | let yesterday = res!(gnu(name, &fmt!("{} {} yesterday", day, said))).map(|s| s * MICROS); |
| 125 | let want = match today { |
| 126 | // The reading is later than now, so it is yesterday's. |
| 127 | Some(t) if t > NOW => yesterday, |
| 128 | other => other, |
| 129 | }; |
| 130 | assert_eq!(Some(res!(moment::read(said, NOW, &zone))), want, |
| 131 | "{}: '{}' with the time {} on {}", name, said, here_now, day); |
| 132 | } |
| 133 | // The very second of now is not later than it. |
| 134 | let now_clock = here_now.clone(); |
| 135 | let at_now = res!(moment::read(&now_clock, NOW - 250_000, &zone)); |
| 136 | assert_eq!(at_now, NOW - 250_000, "{}: now to the second", name); |
| 137 | } |
| 138 | Ok(()) |
| 139 | } |
| 140 | |
| 141 | #[test] |
| 142 | fn today_and_yesterday_alone_are_now_and_24_hours_ago() -> Outcome<()> { |
| 143 | for (name, zone) in res!(zones()) { |
| 144 | assert_eq!(res!(moment::read("today", NOW, &zone)), NOW, "{}", name); |
| 145 | assert_eq!(res!(moment::read("yesterday", NOW, &zone)), NOW - 86_400 * MICROS, "{}", name); |
| 146 | assert_eq!(res!(moment::read(" Yesterday ", NOW, &zone)), NOW - 86_400 * MICROS, "{}", name); |
| 147 | assert_eq!(res!(moment::read("TODAY", NOW, &zone)), NOW, "{}", name); |
| 148 | } |
| 149 | Ok(()) |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn today_and_yesterday_with_a_clock_take_the_local_date() -> Outcome<()> { |
| 154 | if skip_without_oracle() { return Ok(()); } |
| 155 | for (name, zone) in res!(zones()) { |
| 156 | let day = res!(gnu_day(name, NOW.div_euclid(MICROS))); |
| 157 | for clock in ["00:00", "05:30", "17:30:15", "23:59"] { |
| 158 | let today = res!(gnu(name, &fmt!("{} {}", day, clock))).map(|s| s * MICROS); |
| 159 | let yesterday = res!(gnu(name, &fmt!("{} {} yesterday", day, clock))).map(|s| s * MICROS); |
| 160 | // A time still to come today is read as it is: only a bare clock is moved back. |
| 161 | assert_eq!(Some(res!(moment::read(&fmt!("today {}", clock), NOW, &zone))), today, |
| 162 | "{}: today {}", name, clock); |
| 163 | assert_eq!(Some(res!(moment::read(&fmt!("yesterday {}", clock), NOW, &zone))), yesterday, |
| 164 | "{}: yesterday {}", name, clock); |
| 165 | } |
| 166 | } |
| 167 | Ok(()) |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn offsets_count_back_from_now() -> Outcome<()> { |
| 172 | if skip_without_oracle() { return Ok(()); } |
| 173 | // GNU date's relative items, which take a date to be relative to, are the |
| 174 | // oracle; in UTC a day is 24 hours, as it is here. |
| 175 | let now_text = res!(date_batch("UTC", None, &[fmt!("@{}", NOW.div_euclid(MICROS))], "+%F %T")); |
| 176 | let now_text = now_text[0].clone().unwrap_or_default(); |
| 177 | // GNU date's "ago" turns over only the item before it, so each item has its own. |
| 178 | let cases = [ |
| 179 | ("10m", "10 minutes ago"), |
| 180 | ("90s", "90 seconds ago"), |
| 181 | ("1h30m", "1 hour ago 30 minutes ago"), |
| 182 | ("2d", "2 days ago"), |
| 183 | ("1w", "1 week ago"), |
| 184 | ("1w2d3h4m5s", "1 week ago 2 days ago 3 hours ago 4 minutes ago 5 seconds ago"), |
| 185 | ("0s", "0 seconds ago"), |
| 186 | ("30m1h", "30 minutes ago 1 hour ago"), |
| 187 | ]; |
| 188 | for (said, relative) in cases { |
| 189 | let want = res!(gnu("UTC", &fmt!("{}Z {}", now_text, relative))).map(|s| s * MICROS + 250_000); |
| 190 | assert!(want.is_some(), "GNU date would not read '{}'", relative); |
| 191 | for (name, zone) in res!(zones()) { |
| 192 | assert_eq!(Some(res!(moment::read(said, NOW, &zone))), want, "{}: '{}'", name, said); |
| 193 | } |
| 194 | } |
| 195 | Ok(()) |
| 196 | } |
| 197 | |
| 198 | #[test] |
| 199 | fn a_wall_time_in_a_spring_gap_is_refused_naming_the_gap() -> Outcome<()> { |
| 200 | let ny = match res!(zone_from(ZONEINFO, "America/New_York")) { Some(z) => z, None => return Ok(()) }; |
| 201 | // 2026-03-08 03:00 is the first moment after the gap. |
| 202 | assert_eq!(res!(moment::read("2026-03-08 03:00", NOW, &ny)), 1_772_953_200 * MICROS); |
| 203 | let text = refusal(moment::read("2026-03-08 02:30", NOW, &ny)); |
| 204 | for part in ["2026-03-08 02:30", "does not exist", "America/New_York", "from 02:00 to 03:00", "-05:00 to -04:00"] { |
| 205 | assert!(text.contains(part), "the refusal '{}' lacks '{}'", text, part); |
| 206 | } |
| 207 | // On the day itself, 08:00 EDT, today's 02:30 is the gap. |
| 208 | let on_the_day = 1_772_971_200 * MICROS; |
| 209 | for said in ["02:30", "today 02:30"] { |
| 210 | let text = refusal(moment::read(said, on_the_day, &ny)); |
| 211 | assert!(text.contains("2026-03-08 02:30") && text.contains("does not exist"), "'{}': {}", said, text); |
| 212 | } |
| 213 | // Yesterday's 02:30 is fine, and so is the day after, when 02:30 has passed. |
| 214 | assert!(moment::read("yesterday 02:30", on_the_day, &ny).is_ok()); |
| 215 | assert!(moment::read("02:30", 1_773_055_200 * MICROS, &ny).is_ok()); |
| 216 | // Just after midnight the next day, today's 02:30 is still to come, so the |
| 217 | // yesterday that stands in for it is the gap day, which is refused as well. |
| 218 | let early = 1_773_032_400 * MICROS; // 2026-03-09 01:00 EDT |
| 219 | let text = refusal(moment::read("02:30", early, &ny)); |
| 220 | assert!(text.contains("2026-03-08 02:30") && text.contains("does not exist"), "{}", text); |
| 221 | // Half an hour gaps are named as well: Lord Howe skips 02:00 to 02:30. |
| 222 | if let Some(lh) = res!(zone_from(ZONEINFO, "Australia/Lord_Howe")) { |
| 223 | let text = refusal(moment::read("2026-10-04 02:15", NOW, &lh)); |
| 224 | for part in ["2026-10-04 02:15", "does not exist", "from 02:00 to 02:30", "+10:30 to +11:00"] { |
| 225 | assert!(text.contains(part), "the refusal '{}' lacks '{}'", text, part); |
| 226 | } |
| 227 | } |
| 228 | Ok(()) |
| 229 | } |
| 230 | |
| 231 | #[test] |
| 232 | fn a_wall_time_in_an_autumn_fold_takes_the_earlier_and_says_so() -> Outcome<()> { |
| 233 | if skip_without_oracle() { return Ok(()); } |
| 234 | let ny = match res!(zone_from(ZONEINFO, "America/New_York")) { Some(z) => z, None => return Ok(()) }; |
| 235 | let earlier = res!(gnu("UTC", "2026-11-01 01:30:00-04:00")).unwrap_or(0) * MICROS; |
| 236 | let later = res!(gnu("UTC", "2026-11-01 01:30:00-05:00")).unwrap_or(0) * MICROS; |
| 237 | assert_eq!(later - earlier, 3600 * MICROS); |
| 238 | let got = res!(moment::read_noted("2026-11-01 01:30", NOW, &ny)); |
| 239 | assert_eq!(got, Moment { micros: earlier, fold: true }); |
| 240 | // Either occurrence can be asked for by its offset, and then it is not a fold. |
| 241 | let got = res!(moment::read_noted("2026-11-01 01:30 -05:00", NOW, &ny)); |
| 242 | assert_eq!(got, Moment { micros: later, fold: false }); |
| 243 | // Neighbours are not folds. |
| 244 | for said in ["2026-11-01 00:59:59", "2026-11-01 02:00", "2026-10-31 01:30"] { |
| 245 | assert!(!res!(moment::read_noted(said, NOW, &ny)).fold, "'{}'", said); |
| 246 | } |
| 247 | // A bare clock time in the fold, when the first occurrence has passed and when |
| 248 | // it has not. Now is 01:10 EST (the second 01:10), then 01:10 EDT (the first). |
| 249 | let at_0110_est = 1_793_513_400 * MICROS; |
| 250 | let at_0110_edt = 1_793_509_800 * MICROS; |
| 251 | assert_eq!(res!(moment::read_noted("01:30", at_0110_est, &ny)), Moment { micros: earlier, fold: true }); |
| 252 | // The first 01:30 has not come yet, so yesterday's is meant. |
| 253 | let got = res!(moment::read_noted("01:30", at_0110_edt, &ny)); |
| 254 | assert_eq!(got, Moment { micros: earlier - 86_400 * MICROS, fold: false }); |
| 255 | Ok(()) |
| 256 | } |
| 257 | |
| 258 | #[test] |
| 259 | fn the_reader_is_strict() -> Outcome<()> { |
| 260 | let zone = CalClockZone::utc(); |
| 261 | let refused = [ |
| 262 | "", " ", "foo", "10", "3", "0", "10M", "10x", "1h30", "h", "m", "1h 30m", "-10m", "+10m", "1.5h", |
| 263 | "25:00", "24:00", "12:60", "12:30:60", "9", "9:5", "12:30.5", "12:30:15.1234567", "12:30:15.", |
| 264 | "2026-6-15 17:30", "2026-10-01", "2026-10-01T", "2026-02-30 10:00", "2026-13-01 10:00", "2026-00-10 10:00", |
| 265 | "2026-10-00 10:00", "2100-02-29 10:00", "26-10-01 10:00", "2026/10/01 10:00", "2026-10-01 25:00", |
| 266 | "2026-10-01 10:00 Zulu", "2026-10-01 10:00 +24:00", "2026-10-01 10:00 +08:60", "2026-10-01 10:00Z Z", |
| 267 | "2026-10-01 10:00 +08:00 +08:00", "2026-10-01 10:00 +8", "2026-10-01 10:00 extra words here", |
| 268 | "today 17", "today foo", "yesterday 25:00", "today 17:30 extra", "yesterday yesterday", |
| 269 | "tomorrow", "last friday", "17:30 yesterday", "0000-01-01 00:00Z", |
| 270 | "99999999999999999999s", "9999999999999w", "1h9999999999999999999m", |
| 271 | ]; |
| 272 | for said in refused { |
| 273 | assert!(moment::read(said, NOW, &zone).is_err(), "'{}' was read", said); |
| 274 | } |
| 275 | Ok(()) |
| 276 | } |
| 277 | |
| 278 | #[test] |
| 279 | fn a_refusal_names_the_words_and_the_reason() -> Outcome<()> { |
| 280 | let utc = CalClockZone::utc(); |
| 281 | let text = refusal(moment::read("last friday", NOW, &utc)); |
| 282 | assert!(text.contains("'last friday' is not a time that can be read"), "{}", text); |
| 283 | // A reading that has the shape of a time and a wrong number says which. |
| 284 | let text = refusal(moment::read("25:00", NOW, &utc)); |
| 285 | assert!(text.contains("hour") && text.contains("25"), "{}", text); |
| 286 | let text = refusal(moment::read("12:61", NOW, &utc)); |
| 287 | assert!(text.contains("minute") && text.contains("61"), "{}", text); |
| 288 | let text = refusal(moment::read("2026-02-30 10:00", NOW, &utc)); |
| 289 | assert!(text.contains("2026-02-30"), "{}", text); |
| 290 | // And what it reads is listed for the caller, each form once and in the words |
| 291 | // a person types. |
| 292 | assert_eq!(FORMS.len(), 5); |
| 293 | for (form, meaning) in FORMS { |
| 294 | assert!(!form.is_empty() && !meaning.is_empty()); |
| 295 | } |
| 296 | Ok(()) |
| 297 | } |
| 298 | |
| 299 | #[test] |
| 300 | fn show_matches_gnu_date_and_reads_back() -> Outcome<()> { |
| 301 | if skip_without_oracle() { return Ok(()); } |
| 302 | // A spread of instants, with both sides of each 2026 change in each zone. |
| 303 | let mut instants: Vec<i64> = vec![0, 1, 951_782_400, 1_790_879_010, 1_772_953_199, 1_772_953_200, |
| 304 | 1_793_509_199, 1_793_509_200, 1_793_512_799, 1_793_512_800, 4_102_444_799, 253_402_214_399, -1, -86_400]; |
| 305 | let mut seed: u64 = 0x2545_f491_4f6c_dd1d; |
| 306 | for _ in 0..300 { |
| 307 | seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1_442_695_040_888_963_407); |
| 308 | instants.push((seed >> 33) as i64 % 4_000_000_000); |
| 309 | } |
| 310 | let lines: Vec<String> = instants.iter().map(|t| fmt!("@{}", t)).collect(); |
| 311 | for (name, zone) in res!(zones()) { |
| 312 | let said = res!(date_batch(name, None, &lines, "+%F %T %:z")); |
| 313 | for (t, want) in instants.iter().zip(said) { |
| 314 | let shown = res!(moment::show(t * MICROS + 999_999, &zone)); |
| 315 | assert_eq!(Some(shown.as_str()), want.as_deref(), "{} at {}", name, t); |
| 316 | // What is shown is what is read, to the second, and it names its own offset. |
| 317 | assert_eq!(res!(moment::read(&shown, NOW, &zone)), t * MICROS, "{}: '{}'", name, shown); |
| 318 | } |
| 319 | } |
| 320 | let said = res!(date_batch("UTC", None, &lines, "+%F %T")); |
| 321 | for (t, want) in instants.iter().zip(said) { |
| 322 | let shown = res!(moment::show_utc(*t * MICROS)); |
| 323 | assert_eq!(Some(shown.trim_end_matches('Z')), want.as_deref(), "UTC at {}", t); |
| 324 | assert_eq!(res!(moment::read(&shown, NOW, &CalClockZone::utc())), t * MICROS, "'{}'", shown); |
| 325 | } |
| 326 | Ok(()) |
| 327 | } |
| 328 | |
| 329 | #[test] |
| 330 | fn random_wall_times_match_gnu_date_in_every_zone() -> Outcome<()> { |
| 331 | if skip_without_oracle() { return Ok(()); } |
| 332 | // Local times from 1971 to 2099, over both hemispheres' rules, and over the |
| 333 | // leap days that the Julian day arithmetic could get wrong. |
| 334 | let mut locals: Vec<i64> = Vec::new(); |
| 335 | let mut seed: u64 = 0x9e37_79b9_7f4a_7c15; |
| 336 | for _ in 0..4000 { |
| 337 | seed = seed.wrapping_mul(6_364_136_223_846_793_005).wrapping_add(1_442_695_040_888_963_407); |
| 338 | locals.push(((seed >> 33) as i64 % 4_000_000_000) + 31_536_000); |
| 339 | } |
| 340 | let mut wall = res!(walls(&locals)); |
| 341 | // 29 February and the days about it, in a leap year, a century leap year and a |
| 342 | // century that is not one. |
| 343 | for day in ["2024-02-28", "2024-02-29", "2024-03-01", "2000-02-29", "2096-02-29", "2100-02-28", "2100-03-01"] { |
| 344 | wall.push(fmt!("{} 12:00:00", day)); |
| 345 | } |
| 346 | for (name, zone) in res!(zones()) { |
| 347 | let said = res!(date_batch(name, None, &wall, "+%s")); |
| 348 | for (w, g) in wall.iter().zip(said) { |
| 349 | let g: Option<i64> = g.and_then(|s| s.parse().ok()); |
| 350 | // The stamp's own text, which is what a person would type. |
| 351 | let got = moment::read_noted(w, NOW, &zone); |
| 352 | match (got, g) { |
| 353 | (Ok(m), Some(g)) if !m.fold => assert_eq!(m.micros, g * MICROS, "{}: {}", name, w), |
| 354 | (Ok(m), Some(g)) => { |
| 355 | // In a fold GNU date takes either occurrence, an hour or so apart. |
| 356 | let gap = (m.micros - g * MICROS).abs(); |
| 357 | assert!(gap <= 86_400 * MICROS && gap % (1800 * MICROS) == 0, "{}: {} fold {} vs {}", name, w, m.micros, g); |
| 358 | }, |
| 359 | (Err(_), None) => (), // a skipped time is refused by both |
| 360 | (got, g) => panic!("{}: {} -> {:?} but GNU date says {:?}", name, w, got, g), |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | Ok(()) |
| 365 | } |