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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
#[cfg(feature = "parse")]
mod tests {
use deep_time::{DateOrder, DateParseMode, Dt, ParseCfg, Scale};
#[cfg(feature = "tz")]
#[test]
fn roundtrip_gap_boundary_new_york() {
let our_input = "2023-03-12 02:00:00 America/New_York";
let expected_snapped = "2023-03-12 03:00:00 America/New_York";
// Parse the non-existent local time (should succeed via lenient gap handling)
let our_dt: Dt = Dt::from_str_parse(our_input, &None)
.expect("parse should succeed (lenient gap handling)");
// Verify internal representation (the snapped UTC instant)
assert_eq!(
our_dt.to_epoch(Dt::UNIX_EPOCH, Scale::UTC).to_sec(),
1678604400,
"internal unix timestamp should be the snapped UTC instant"
);
// Format back using the IANA zone
let fmt = "%Y-%m-%d %H:%M:%S %Q";
let output = our_dt
.to_str_with_tz(fmt, "America/New_York")
.expect("to_str_with_tz should succeed");
// === THE KEY REGRESSION ASSERT ===
assert_eq!(
output, expected_snapped,
"gap time should silently snap forward to the next valid local time (post-DST)"
);
// Bonus: verify the round-trip is stable (parse → format → parse → format)
let our_dt2: Dt =
Dt::from_str_parse(&output, &None).expect("second parse should also succeed");
let output2 = our_dt2
.to_str_with_tz(fmt, "America/New_York")
.expect("second format should succeed");
assert_eq!(output2, expected_snapped, "round-trip must be stable");
}
#[test]
fn print_stuff() {
// let x = Dt::from_ymd(1961, 1, 1, Scale::UTC);
// eprintln!("UTC lower 61 {:?}", x);
// let x = Dt::from_ymd(1972, 1, 1, Scale::UTC);
// eprintln!("UTC upper 72 {:?}", x);
// let utc_tp = Dt::from_ymd(1961, 1, 1, Scale::UTC);
// let sofa_tp = utc_tp.with_type(Scale::UTCSofa);
// let tai_tp = sofa_tp.to_tai();
// println!("TAI lower 61 {:?}", tai_tp);
// let utc_tp = Dt::from_ymd(1972, 1, 1, Scale::UTC);
// let tai_tp = utc_tp.to_tai();
// println!("TAI upper 72 {:?}", tai_tp);
// for &(year, month, day, _) in dates {
// // Create UTC Dt at 00:00:00 on the insertion date
// let utc_tp =
// Dt::from_ymd(year as i64, month as u8, day as u8, Scale::UTC);
// // Create TAI Dt at the same civil date (for comparison)
// let tai_tp =
// Dt::from_ymd(year as i64, month as u8, day as u8, Scale::TAI);
// println!(
// "{:04}-{:02}-{:02}: UTC sec = {}, TAI sec = {}",
// year,
// month,
// day,
// utc_tp.sec(),
// tai_tp.sec(),
// );
// }
// let unix_sec = -283996800i64;
// let tp = Dt::from_unix_sec(unix_sec);
// let tai = tp.to_type(Scale::TAI); // or tp.to_tai() if you have that method
// println!("TAI sec: {}", tai.sec());
// println!("TAI subsec: {}", tai.subsec());
// let unix_1972 = 63072000i64;
// let tp_1972 = Dt::from_unix_sec(unix_1972);
// let tai_1972 = tp_1972.to_type(Scale::TAI);
// eprintln!("1972-01-01 TAI sec (for cutoff): {}", tai_1972.sec());
// let x = Dt::from_ymd(1972, 1, 1, Scale::TAI).to_jd();
// eprintln!("{}", x);
// for e in SOFA_TAI_UTC_PRE_1972.iter() {
// // Create the UTC instant for this table row
// let utc_tp = Dt::from_ymd(e.yr as i64, e.mo, e.day, Scale::UTC);
// // Turn it into a UTCSofa Dt (preserves the numerical unix_sec)
// let sofa_tp = utc_tp.with_type(Scale::UTCSofa);
// // This calls your to_tai for UTCSofa, which does:
// // utc_to_tai(...) + add(sofa_offset)
// let tai_tp = sofa_tp.to_tai();
// println!(
// "{}-{}-{} : tai_jd: {}, tai_mjd: {}",
// e.yr,
// e.mo,
// e.day,
// tai_tp.to_jd(),
// tai_tp.to_mjd(),
// );
// }
}
// ─────────────────────────────────────────────────────────────────────────────
// Helpers
// ─────────────────────────────────────────────────────────────────────────────
fn assert_date(input: &str, expected_rfc3339: &str, opts: Option<ParseCfg>) {
let dt = Dt::from_str_parse(input.trim(), &opts)
.unwrap_or_else(|e| panic!("Failed to parse '{}': {}", input, e));
let actual = dt.to_str_rfc3339().unwrap();
assert_eq!(actual, expected_rfc3339, "Input: {}", input);
}
fn assert_millis(input: &str, expected_millis: i128, opts: Option<ParseCfg>) {
let millis = Dt::str_to_unix_ms(input, &opts)
.unwrap_or_else(|| panic!("Failed millis parse: {}", input));
assert_eq!(millis, expected_millis, "Input: {}", input);
}
fn assert_fails(input: &str, opts: Option<ParseCfg>) {
assert!(
Dt::from_str_parse(input, &opts).is_err(),
"Expected failure: {}",
input
);
}
// ─────────────────────────────────────────────────────────────────────────────
// Comprehensive suite
// ─────────────────────────────────────────────────────────────────────────────
fn generate_date_test_cases() -> Vec<(String, String, Option<ParseCfg>)> {
let mut cases = Vec::new();
// ================================================================
// 1. Core components – add new formats here for instant 10x coverage
// ================================================================
let date_only_bases = [
"2024-03-14",
"2024.03.14",
"2024/03/14",
"2024 03 14",
"20240314",
"14Mar24",
"14-Mar-24",
"14 Mar 2024",
"Mar 14 2024",
"March 14, 2024",
"14 March 2024",
"14-March-2024",
"14/Mar/2024",
"2024-W11-4",
"2024-074",
"2024/074",
"2024.074",
"240314",
// "202403",
];
let dt_separators = [" ", "T", "", ":"]; // ← ":" added (per request)
let time_variants = [
("", "T00:00:00Z"), // date-only
("15:30", "T15:30:00Z"),
("15:30:45", "T15:30:45Z"),
("15:30:45.123", "T15:30:45.123Z"),
("15:30:45.123456", "T15:30:45.123456Z"),
("15:30:45.123456789", "T15:30:45.123456789Z"),
("03:30:45 PM", "T15:30:45Z"),
("03:30:45.123456789 PM", "T15:30:45.123456789Z"),
];
let tz_variants = ["", "+0000", " +0000", "+00:00", " +00:00", "Z", "-0000"];
let prefixes = ["", " ", "Thu ", "Thu. ", "Thursday, ", "Thu, "];
let opts = ParseCfg {
order: DateOrder::YearFirst,
..Default::default()
};
// ================================================================
// 2. Generate massive combinatorial coverage
// ================================================================
for date in date_only_bases {
for prefix in prefixes {
for dt_sep in dt_separators {
for (time_in, time_expected) in time_variants {
for tz in tz_variants {
// === FIXED: Prevent invalid date-only + timezone (no time) ===
if time_in.is_empty()
&& !tz.is_empty()
&& (dt_sep.is_empty() || dt_sep == " ")
{
continue;
}
// === UPDATED: Prevent malformed date-only with T or : separator ===
// (now covers both "2024-03-14T" and "2024-03-14:")
if time_in.is_empty() && (dt_sep == "T" || dt_sep == ":") {
continue;
}
// === NEW: Prevent malformed 12-hour AM/PM + compact timezone suffix ===
// e.g. "03:30:45 PMZ", "PM+0000", "PM-0000", etc.
// (still allows valid spaced variants like "PM +0000")
if (time_in.contains("PM") || time_in.contains("AM"))
&& !tz.is_empty()
&& !tz.starts_with(' ')
{
continue;
}
// === Prevent compact NAMED date formats glued directly to time ===
// e.g. "14Mar2415:30", "14-Mar-2415:30", "March 14, 202415:30"
// (named = contains letters; numeric compacts like "2024031415:30" are untouched)
if dt_sep.is_empty()
&& !time_in.is_empty()
&& date.chars().any(|c| c.is_alphabetic())
{
continue;
}
// === NEW FIX: Prevent "Thu 2024 03 14:15:30" and similar bad cases ===
// Day name + purely numeric spaced date (YYYY MM DD) + time glued with ":"
// (or empty separator). These produce only "2 date digit groups" after the
// day name (2024 03) before the time starts, which violates the rule you
// described ("if there's only 2 date digit groups and a named then the
// named should be the month, not day"). We keep weekday prefixes only
// with named-month dates like "Thu Mar 14 2024".
if !prefix.trim().is_empty()
&& (prefix.contains("Thu") || prefix.contains("Thursday"))
&& date.contains(' ')
&& !date.chars().any(|c| c.is_alphabetic())
&& (dt_sep == ":" || dt_sep.is_empty())
&& !time_in.is_empty()
{
continue;
}
// === NEW FIX: Prevent "2024 03 14:15:30" (the specific invalid format you asked for) ===
// Purely numeric spaced date (YYYY MM DD) glued directly to time with ":"
// This is the exact case you flagged. It is now completely excluded from
// the generated test cases while leaving every other valid combination intact.
if date.contains(' ')
&& !date.chars().any(|c| c.is_alphabetic())
&& dt_sep == ":"
&& !time_in.is_empty()
{
continue;
}
// === NEW: Prevent julians from being produced with times that dont have a time connector ===
// e.g. "2024-07415:30" - should not be produced.
// Only blocks the empty (glued) case; T, space, and colon are still allowed.
if date.len() == 8
&& matches!(date.chars().nth(4), Some('-') | Some('/') | Some('.'))
&& date[5..].chars().all(|c| c.is_ascii_digit())
&& !time_in.is_empty()
&& dt_sep.is_empty()
{
continue;
}
// === Prevent all pure numeric YYYYMMDDHHMM* format tests ===
// (ONLY REMOVE THE PURE NUMERIC CASES — separators, connectors, T, :, ., space, etc. are OK!)
// This blocks only fully-glued digit-only cases like "202403141530" or "240314153045".
// Cases with any separator/connector (including the . in millis) are left untouched.
if dt_sep.is_empty()
&& !time_in.is_empty()
&& date.chars().all(|c| c.is_ascii_digit())
&& time_in.chars().all(|c| c.is_ascii_digit())
{
continue;
}
// === Prevent 6-digit pure numeric date (e.g. 240314) glued directly to time
// without any connector
// Bad examples that are now blocked:
// "24031415:30:00", "24031415:30:45", "24031415:30:45.123"
// Allowed (they contain a connector):
// "240314T15:30+0000", "240314 15:30", "240314:15:30:00", etc.
if date.len() == 6
&& date.chars().all(|c| c.is_ascii_digit())
&& dt_sep.is_empty()
&& !time_in.is_empty()
{
continue;
}
let input = format!("{}{}{}{}{}", prefix, date, dt_sep, time_in, tz)
.trim()
.to_string();
let expected = if time_in.is_empty() {
"2024-03-14T00:00:00Z".to_string()
} else {
format!("2024-03-14{}", time_expected)
};
cases.push((input, expected, Some(opts.clone())));
}
}
}
}
}
// ================================================================
// 3. Truly special / one-off cases (unchanged)
// ================================================================
let special_cases: Vec<(String, String, Option<ParseCfg>)> = vec![
(
"2024-W11".to_string(),
"2024-03-11T00:00:00Z".to_string(),
None,
),
(
"2024-074T15:30:45.123456789Z".to_string(),
"2024-03-14T15:30:45.123456789Z".to_string(),
None,
),
(
"20240314T153045Z".to_string(),
"2024-03-14T15:30:45Z".to_string(),
None,
),
(
"2024-03-14T15:30:45Z".to_string(),
"2024-03-14T15:30:45Z".to_string(),
None,
),
(
"Thu Mar 14 15:30:45 2024".to_string(),
"2024-03-14T15:30:45Z".to_string(),
None,
),
(
"14/Mar/2024:15:30:45 +0000".to_string(),
"2024-03-14T15:30:45Z".to_string(),
None,
),
// REMOVED pure-numeric YYYYMMDDHHMM* cases per user request:
// - "20240314153045"
// - "240314153045"
// (the .millis version below is kept because it contains a separator)
(
"20240314153045.123456789".to_string(),
"2024-03-14T15:30:45.123456789Z".to_string(),
None,
),
// Parse-mode / DateOrder / explicit format tests
(
"60400".to_string(),
"2024-03-31T00:00:00Z".to_string(),
Some(ParseCfg {
mode: DateParseMode::Scientific,
..Default::default()
}),
),
(
"05/06/2024".to_string(),
"2024-06-05T00:00:00Z".to_string(),
Some(ParseCfg {
order: DateOrder::DayFirst,
..Default::default()
}),
),
(
"14/03/2024 15:30".to_string(),
"2024-03-14T15:30:00Z".to_string(),
Some(ParseCfg {
parse: Some(vec!["%d/%m/%Y %H:%M".to_string()]),
..Default::default()
}),
),
("2024".to_string(), "2024-01-01T00:00:00Z".to_string(), None),
(
"-2024-03-14".to_string(),
"-2024-03-14T00:00:00Z".to_string(),
None,
),
(
"Dec 31 23:59:59".to_string(),
"2025-12-31T23:59:59Z".to_string(),
Some(ParseCfg {
ref_time: Some(Dt::from_ymdhms(2025, 12, 31, 23, 59, 59, 0)),
..Default::default()
}),
),
];
cases.extend(special_cases);
cases
}
#[test]
fn date_parser_roundtrip() {
let tp1 = Dt::from(5, 0, Scale::LTC);
let tp2 = Dt::from(5, 0, Scale::GPS);
let xp1 = tp1.to_str("%Y-%m-%dT%H:%M:%S%.f").unwrap();
let xp2 = tp2.to_str("%Y-%m-%dT%H:%M:%S%.f").unwrap();
let res_tp1 = Dt::from_str(&xp1, "%Y-%m-%dT%H:%M:%S%.f", true, true, false).unwrap();
let res_tp2 = Dt::from_str(&xp2, "%Y-%m-%dT%H:%M:%S%.f", true, true, false).unwrap();
assert!(tp1 == res_tp1);
assert!(tp2 == res_tp2);
}
#[test]
fn round_trip_fixed_offsets() {
for tp in [Dt::new(5, 0), Dt::new(5, 0)] {
let xp1 = tp
.to_str_with_offset("%Y-%m-%dT%H:%M:%S%.~f %:z", 3600)
.unwrap();
let tp2 = Dt::from_str_parse(&xp1, &None).unwrap();
let xp2 = tp2
.to_str_with_offset("%Y-%m-%dT%H:%M:%S%.~f %:z", 3600)
.unwrap();
let tp3 = Dt::from_str_parse(&xp2, &None).unwrap();
assert_eq!(tp, tp3);
}
}
#[test]
fn date_parser_comprehensive() {
let cases: Vec<(&str, &str, Option<ParseCfg>)> = vec![
(
"2024-03-14 03:30:45.123 PM",
"2024-03-14T15:30:45.123Z",
None,
),
("2024.03.14", "2024-03-14T00:00:00Z", None),
("The 14th of March, 2024", "2024-03-14T00:00:00Z", None),
("2024-03-14", "2024-03-14T00:00:00Z", None),
("2024/03/14", "2024-03-14T00:00:00Z", None),
("2024 03 14", "2024-03-14T00:00:00Z", None),
("2024.03.14 15:30:45", "2024-03-14T15:30:45Z", None),
("2024-03-14 15:30:45 +0000", "2024-03-14T15:30:45Z", None),
("2024-03-14 15:30:45 +00:00", "2024-03-14T15:30:45Z", None),
("2024-03-14T15:30:45 +00:00", "2024-03-14T15:30:45Z", None),
(
"2024.03.14 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-03-14T15:30:45.123456789 +00:00",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-03-14T15:30:45.123456789 +0000",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-03-14 15:30:45.123456789 +00:00",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-03-14 15:30:45.123456789 +0000",
"2024-03-14T15:30:45.123456789Z",
None,
),
("14Mar24", "2024-03-14T00:00:00Z", None),
("20240314", "2024-03-14T00:00:00Z", None),
("2024-W11", "2024-03-11T00:00:00Z", None),
("2024-W11", "2024-03-11T00:00:00Z", None),
("2024-074", "2024-03-14T00:00:00Z", None),
("2024/074", "2024-03-14T00:00:00Z", None),
("2024.074", "2024-03-14T00:00:00Z", None),
("14Mar2024", "2024-03-14T00:00:00Z", None),
("14-Mar-24", "2024-03-14T00:00:00Z", None),
(" 14-Mar-24", "2024-03-14T00:00:00Z", None),
// REMOVED pure-numeric YYYYMMDDHHMM* cases (no separators at all) per user request:
// - "2403141530"
// - "202403141530"
// - "20240314153045" (both occurrences)
// - "240314153045"
// (cases with separators/T/:/space/. remain)
("2024-W11-4", "2024-03-14T00:00:00Z", None),
("2024-W11-4", "2024-03-14T00:00:00Z", None),
("14 Mar 2024", "2024-03-14T00:00:00Z", None),
("14-Mar-2024", "2024-03-14T00:00:00Z", None),
("Mar 14 2024", "2024-03-14T00:00:00Z", None),
("2024 Mar 14", "2024-03-14T00:00:00Z", None),
("14 Mar 2024", "2024-03-14T00:00:00Z", None),
("14-Mar-2024", "2024-03-14T00:00:00Z", None),
("20240314 15:30", "2024-03-14T15:30:00Z", None),
("2024-074 15:30", "2024-03-14T15:30:00Z", None),
("2024.074 15:30", "2024-03-14T15:30:00Z", None),
("2024/074 15:30", "2024-03-14T15:30:00Z", None),
("20240314T153045", "2024-03-14T15:30:45Z", None),
("2024-03-14 15:30", "2024-03-14T15:30:00Z", None),
("2024-03-14T15:30", "2024-03-14T15:30:00Z", None),
("14-03-2024 15:30", "2024-03-14T15:30:00Z", None),
("14.03.2024 15:30", "2024-03-14T15:30:00Z", None),
("2024 03 14 15:30", "2024-03-14T15:30:00Z", None),
("2024-074T15:30:45", "2024-03-14T15:30:45Z", None),
("2024-074 15:30:45", "2024-03-14T15:30:45Z", None),
("2024/074T15:30:45", "2024-03-14T15:30:45Z", None),
("2024/074 15:30:45", "2024-03-14T15:30:45Z", None),
("2024.074T15:30:45", "2024-03-14T15:30:45Z", None),
("2024.074 15:30:45", "2024-03-14T15:30:45Z", None),
("2024 Mar 14 15:30", "2024-03-14T15:30:00Z", None),
("14 Mar 2024 15:30", "2024-03-14T15:30:00Z", None),
("2024 Mar 14 15:30", "2024-03-14T15:30:00Z", None),
("20240314 15:30:45", "2024-03-14T15:30:45Z", None),
("2024-03-1415:30:45", "2024-03-14T15:30:45Z", None),
("2024-03-14 15:30:45", "2024-03-14T15:30:45Z", None),
("2024-03-14T15:30:45", "2024-03-14T15:30:45Z", None),
("2024/03/14 15:30:45", "2024-03-14T15:30:45Z", None),
("2024 03 14 15:30:45", "2024-03-14T15:30:45Z", None),
("14-03-2024 15:30:45", "2024-03-14T15:30:45Z", None),
("14.03.2024 15:30:45", "2024-03-14T15:30:45Z", None),
("2024-W11-4 15:30:45", "2024-03-14T15:30:45Z", None),
("2024-W11-4 15:30:45", "2024-03-14T15:30:45Z", None),
("14/Mar/2024:15:30:45", "2024-03-14T15:30:45Z", None),
("14-Mar-2024 15:30:45", "2024-03-14T15:30:45Z", None),
("Mar 14 2024 15:30:45", "2024-03-14T15:30:45Z", None),
("14 Mar 2024 15:30:45", "2024-03-14T15:30:45Z", None),
("2024 Mar 14 15:30:45", "2024-03-14T15:30:45Z", None),
("2024 Mar 14 15:30:45", "2024-03-14T15:30:45Z", None),
("14 Mar 2024 15:30:45", "2024-03-14T15:30:45Z", None),
("14-Mar-2024 15:30:45", "2024-03-14T15:30:45Z", None),
("14 Mar, 2024 15:30:45", "2024-03-14T15:30:45Z", None),
("Mar 14, 2024 15:30:45", "2024-03-14T15:30:45Z", None),
("2024.03.14 15:30:45", "2024-03-14T15:30:45Z", None),
("2024-03-14 15:30:45 +0000", "2024-03-14T15:30:45Z", None),
("2024-03-14 15:30:45 +00:00", "2024-03-14T15:30:45Z", None),
("2024-03-14T15:30:45 +00:00", "2024-03-14T15:30:45Z", None),
(
"2024-03-14T15:30:45.123456789 +00:00",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-03-14T15:30:45.123456789 +0000",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-03-14 15:30:45.123456789 +00:00",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-03-14 15:30:45.123456789 +0000",
"2024-03-14T15:30:45.123456789Z",
None,
),
("2024-03-1415:30:45.123", "2024-03-14T15:30:45.123Z", None),
("14 Mar 2024 03:30:45 PM", "2024-03-14T15:30:45Z", None),
("Mar 14 2024 03:30:45 PM", "2024-03-14T15:30:45Z", None),
("Mar 14, 2024 03:30:45 PM", "2024-03-14T15:30:45Z", None),
("2024-03-14 15:30:45+0000", "2024-03-14T15:30:45Z", None),
("2024-03-14T15:30:45+0000", "2024-03-14T15:30:45Z", None),
("Thu Mar 14 15:30:45 2024", "2024-03-14T15:30:45Z", None),
("14 Mar, 2024 03:30:45 PM", "2024-03-14T15:30:45Z", None),
("2024-03-14T15:30:45+00:00", "2024-03-14T15:30:45Z", None),
(
"2024-03-1415:30:45.123456",
"2024-03-14T15:30:45.123456Z",
None,
),
("14/Mar/2024:15:30:45 +0000", "2024-03-14T15:30:45Z", None),
(
"Thu Mar 14 15:30:45 +00:00 2024",
"2024-03-14T15:30:45Z",
None,
),
("Mar 14, 2024 15:30:45 +0000", "2024-03-14T15:30:45Z", None),
("Mar 14, 2024 15:30:45 +00:00", "2024-03-14T15:30:45Z", None),
(
"Thu Mar 14 15:30:45 +0000 2024",
"2024-03-14T15:30:45Z",
None,
),
(
"Thu, 14 Mar 2024 15:30:45 +0000",
"2024-03-14T15:30:45Z",
None,
),
(
"Thu Mar 14 15:30:45 +00:00 2024",
"2024-03-14T15:30:45Z",
None,
),
("20240314T153045Z", "2024-03-14T15:30:45Z", None),
("2024-03-14T15:30:45Z", "2024-03-14T15:30:45Z", None),
("March 14, 2024 03:30:45 PM", "2024-03-14T15:30:45Z", None),
("March 14, 2024 15:30:45", "2024-03-14T15:30:45Z", None),
(
"March 14, 2024 03:30:45.123456789 PM",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"March 14, 2024 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
("March 14, 2024", "2024-03-14T00:00:00Z", None),
(
"March 14, 2024 15:30:45 +00:00",
"2024-03-14T15:30:45Z",
None,
),
(
"March 14, 2024 15:30:45 +0000",
"2024-03-14T15:30:45Z",
None,
),
(
"March 14 2024 15:30:45 +00:00",
"2024-03-14T15:30:45Z",
None,
),
(
"Mar 14, 2024 03:30:45.123456789 PM",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"Mar 14, 2024 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"Mar 14 2024 03:30:45.123456789 PM",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"Mar 14 2024 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
("Thursday, 14 March 2024", "2024-03-14T00:00:00Z", None),
("Thursday 14 March 2024", "2024-03-14T00:00:00Z", None),
("Thursday, 14 Mar 2024", "2024-03-14T00:00:00Z", None),
("Thursday 14 Mar 2024", "2024-03-14T00:00:00Z", None),
(
"Thursday, 14 Mar 2024 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"Thursday 14 Mar 2024 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"Thu Mar 14 15:30:45.123456789 2024",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"Thu Mar 14 15:30:45.123456789 +00:00 2024",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"Thu Mar 14 15:30:45.123456789 +0000 2024",
"2024-03-14T15:30:45.123456789Z",
None,
),
("14 March 2024", "2024-03-14T00:00:00Z", None),
("14 March, 2024", "2024-03-14T00:00:00Z", None),
("14 March 2024", "2024-03-14T00:00:00Z", None),
("14 March, 2024", "2024-03-14T00:00:00Z", None),
("14-March-2024", "2024-03-14T00:00:00Z", None),
("14 March 2024 15:30:45", "2024-03-14T15:30:45Z", None),
("14 March, 2024 15:30:45", "2024-03-14T15:30:45Z", None),
("14 March 2024 15:30:45", "2024-03-14T15:30:45Z", None),
("14-March-2024 15:30:45", "2024-03-14T15:30:45Z", None),
("Mar 14, 2024 15:30:45 +00:00", "2024-03-14T15:30:45Z", None),
(
"Mar 14, 2024 15:30:45.123456789 +00:00",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"Mar 14, 2024 15:30:45.123456789 +0000",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"14 Mar, 2024 15:30:45.123456789 +00:00",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"14 Mar, 2024 15:30:45.123456789 +0000",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"14 March 2024 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"14 March, 2024 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"14 March 2024 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"14-March-2024 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
("14 March 2024 03:30:45 PM", "2024-03-14T15:30:45Z", None),
("14 March, 2024 03:30:45 PM", "2024-03-14T15:30:45Z", None),
(
"14 March 2024 03:30:45.123456789 PM",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"14 March, 2024 03:30:45.123456789 PM",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"14 Mar 2024 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"14 Mar, 2024 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"14 Mar, 2024 03:30:45.123456789 PM",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"14 Mar 2024 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"14-Mar-2024 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"14/Mar/2024:15:30:45.123456789 +0000",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"20240314T153045.123456789Z",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-074T15:30:45.123456789Z", // here
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024.074T15:30:45.123456789Z",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024/074T15:30:45.123456789Z",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-03-14T15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-03-14T15:30:45.123456789+0000",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-03-14 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-03-1415:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024/03/14 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"20240314T153045.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"20240314153045.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"20240314 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024 March 14 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024 Mar 14 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
("2024 March 14 15:30:45", "2024-03-14T15:30:45Z", None),
("2024 March 14", "2024-03-14T00:00:00Z", None),
(
"2024 Mar 14 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024 03 14 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-074T15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-074 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-074T15:30:45.123456789+0000",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-074 15:30:45.123456789+0000",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-074T15:30:45.123456789+00:00",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024-074 15:30:45.123456789+00:00",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024.074T15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024.074 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024.074T15:30:45.123456789+0000",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024.074T15:30:45.123456789+00:00",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024/074T15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024/074 15:30:45.123456789",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024/074T15:30:45.123456789+0000",
"2024-03-14T15:30:45.123456789Z",
None,
),
(
"2024/074T15:30:45.123456789+00:00",
"2024-03-14T15:30:45.123456789Z",
None,
),
// Common formats
("2024-03-14", "2024-03-14T00:00:00Z", None),
("14 Mar 2024", "2024-03-14T00:00:00Z", None),
("15 Mar 2024 14:30", "2024-03-15T14:30:00Z", None),
("Mar 14, 2024", "2024-03-14T00:00:00Z", None),
("2024/03/14", "2024-03-14T00:00:00Z", None),
("14.03.2024", "2024-03-14T00:00:00Z", None),
("14/03.2024", "2024-03-14T00:00:00Z", None),
// Pure-numeric special cases (date-only or epoch — kept; only full datetime smashed cases were removed)
("240314", "2024-03-14T00:00:00Z", None),
("202403", "2024-03-01T00:00:00Z", None),
("2024073", "2024-03-13T00:00:00Z", None),
("24073", "2024-03-13T00:00:00Z", None),
("60400", "2024-03-31T00:00:00Z", None),
("2460000", "2023-02-25T00:00:00Z", None),
("1700000000", "2023-11-14T22:13:20Z", None),
("1700000000000", "2023-11-14T22:13:20Z", None),
// Parse modes
(
"60400",
"2024-03-31T00:00:00Z",
Some(ParseCfg {
parse: None,
mode: DateParseMode::Scientific,
order: DateOrder::default(),
..Default::default()
}),
),
(
"24073",
"2024-03-13T00:00:00Z",
Some(ParseCfg {
parse: None,
mode: DateParseMode::Legacy,
order: DateOrder::default(),
..Default::default()
}),
),
// DateOrder
(
"05/06/2024",
"2024-06-05T00:00:00Z",
Some(ParseCfg {
parse: None,
mode: DateParseMode::Scientific,
order: DateOrder::DayFirst,
..Default::default()
}),
),
(
"05/06/2024",
"2024-05-06T00:00:00Z",
Some(ParseCfg {
order: DateOrder::MonthFirst,
..Default::default()
}),
),
// Year-only
("2024", "2024-01-01T00:00:00Z", None),
("24", "2024-01-01T00:00:00Z", None),
("2024.074", "2024-03-14T00:00:00Z", None),
// Negative years — Jiff pads negative years to 6 digits (Temporal/ISO rule)
("-2024-03-14", "-2024-03-14T00:00:00Z", None),
("-2025/01/01", "-2025-01-01T00:00:00Z", None),
("-0001-01-01", "-0001-01-01T00:00:00Z", None),
// Syslog no-year
(
"Dec 31 23:59:59",
"2025-12-31T23:59:59Z",
Some(ParseCfg {
ref_time: Some(Dt::from_ymdhms(2025, 12, 31, 23, 59, 59, 0)),
..Default::default()
}),
),
// Explicit format
(
"14/03/2024 15:30",
"2024-03-14T15:30:00Z",
Some(ParseCfg {
parse: Some(vec!["%d/%m/%Y %H:%M".to_string()]),
..Default::default()
}),
),
(
"2026年4月5日",
"2026-04-05T00:00:00Z",
Some(ParseCfg {
order: DateOrder::YearFirst,
..Default::default()
}),
),
(
"߂߀߂߄-߀߄-߀߅",
"2024-04-05T00:00:00Z",
Some(ParseCfg {
order: DateOrder::YearFirst,
..Default::default()
}),
),
];
for (input, expected, opts) in cases {
assert_date(input, expected, opts);
}
let cases = generate_date_test_cases();
for (input, expected, opts) in cases {
assert_date(&input, &expected, opts);
}
}
fn generate_relative_date_test_cases() -> Vec<String> {
let mut cases: Vec<String> = Vec::new();
let core_phrases = ["now", "today", "tomorrow", "yesterday"];
cases.extend(core_phrases.iter().map(|&s| s.to_string()));
let numbers = [
"1", "2", "3", "5", "10", "42", "0.5", "1.5", "2,5", "3.75", "1_000",
];
let units = [
"sec", "second", "seconds", "min", "minute", "minutes", "hr", "hour", "hours", "day",
"days", "wk", "week", "weeks", "mo", "month", "months", "yr", "year", "years",
];
let past_suffixes = [" ago"];
let future_prefixes = ["in "];
for num in numbers {
for unit in units {
// past forms
for suffix in past_suffixes {
cases.push(format!("{} {}{}", num, unit, suffix));
}
for prefix in future_prefixes {
if !prefix.is_empty() || num.parse::<f64>().unwrap_or(0.0) != 1.0 {
cases.push(format!("{}{} {}", prefix, num, unit).trim().to_string());
}
}
}
}
let multi_unit_cases = [
"2 hours and 30 minutes ago",
"1 day 12 hours from now",
"3 weeks 4 days later",
"1day ,5hr ago",
"2hrs30min from now",
"45min 15sec later",
"1 day, 2 hours, and 30 minutes ago",
"the 1.5 days ago",
"in 2day 3hr 45min",
"1 week and 2 days from now",
"2 weeks 3 days 4 hours ago",
"in 1 week and 2 days",
"3 days 5 hours later",
];
cases.extend(multi_unit_cases.iter().map(|&s| s.to_string()));
cases
}
#[test]
fn relative_date_parser_comprehensive() {
let cases = generate_relative_date_test_cases();
let opts = Some(ParseCfg {
ref_time: Some(Dt::new(5_000_000, 0)),
..Default::default()
});
for input in cases {
let result = Dt::from_str_parse(input.trim(), &opts);
// eprintln!("Tried: {}, got result: {:?}", &input, result);
assert!(result.is_ok(), "Failed to parse relative date: '{}'", input);
}
}
#[test]
fn date_millis_and_errors() {
assert_millis("2024-03-14", 1710374400000, None);
assert_millis("1700000000000", 1700000000000, None);
assert_fails("not-a-date", None);
assert_fails("2024-13-01", None);
}
}