kelora 1.5.0

A command-line log analysis tool with embedded Rhai scripting
Documentation
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
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
use chrono::{DateTime, Datelike, Duration, NaiveDateTime, TimeZone, Timelike, Utc};
use chrono_tz::Tz;
use rhai::{Engine, EvalAltResult, Position};
use std::cell::RefCell;
use std::fmt;
use std::str::FromStr;

/// Wrapper for chrono::DateTime to provide Rhai integration
#[derive(Debug, Clone)]
pub struct DateTimeWrapper {
    pub inner: DateTime<Tz>,
}

impl DateTimeWrapper {
    pub fn new(dt: DateTime<Tz>) -> Self {
        Self { inner: dt }
    }

    pub fn from_utc(dt: DateTime<Utc>) -> Self {
        Self {
            inner: dt.with_timezone(&Tz::UTC),
        }
    }
}

impl fmt::Display for DateTimeWrapper {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.inner.to_rfc3339())
    }
}

/// Wrapper for chrono::Duration to provide Rhai integration
#[derive(Debug, Clone)]
pub struct DurationWrapper {
    pub inner: Duration,
}

impl DurationWrapper {
    pub fn new(dur: Duration) -> Self {
        // Ensure durations are always non-negative as per spec
        Self { inner: dur.abs() }
    }

    pub fn from_seconds(secs: i64) -> Self {
        Self::new(Duration::seconds(secs))
    }

    pub fn from_minutes(mins: i64) -> Self {
        Self::new(Duration::minutes(mins))
    }

    pub fn from_hours(hours: i64) -> Self {
        Self::new(Duration::hours(hours))
    }

    pub fn from_days(days: i64) -> Self {
        Self::new(Duration::days(days))
    }

    pub fn from_milliseconds(ms: i64) -> Self {
        Self::new(Duration::milliseconds(ms))
    }

    pub fn from_nanoseconds(ns: i64) -> Self {
        Self::new(Duration::nanoseconds(ns))
    }
}

impl fmt::Display for DurationWrapper {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let total_seconds = self.inner.num_seconds();
        if total_seconds < 60 {
            write!(f, "{}s", total_seconds)
        } else if total_seconds < 3600 {
            let minutes = total_seconds / 60;
            let seconds = total_seconds % 60;
            if seconds == 0 {
                write!(f, "{}m", minutes)
            } else {
                write!(f, "{}m {}s", minutes, seconds)
            }
        } else if total_seconds < 86400 {
            let hours = total_seconds / 3600;
            let remaining = total_seconds % 3600;
            let minutes = remaining / 60;
            if minutes == 0 {
                write!(f, "{}h", hours)
            } else {
                write!(f, "{}h {}m", hours, minutes)
            }
        } else {
            let days = total_seconds / 86400;
            let remaining = total_seconds % 86400;
            let hours = remaining / 3600;
            if hours == 0 {
                write!(f, "{}d", days)
            } else {
                write!(f, "{}d {}h", days, hours)
            }
        }
    }
}

// Thread-local adaptive parser for Rhai timestamp parsing
thread_local! {
    static RHAI_TS_PARSER: RefCell<crate::timestamp::AdaptiveTsParser> =
        RefCell::new(crate::timestamp::AdaptiveTsParser::new());
}

/// Convert a string into a `DateTimeWrapper` using optional format and timezone hints.
pub fn to_datetime(
    s: &str,
    format: Option<&str>,
    tz: Option<&str>,
) -> Result<DateTimeWrapper, Box<EvalAltResult>> {
    // Default timezone
    let default_tz = if let Some(tz_str) = tz {
        tz_str.parse::<Tz>().map_err(|e| {
            Box::new(EvalAltResult::ErrorRuntime(
                format!("Invalid timezone '{}': {}", tz_str, e).into(),
                Position::NONE,
            ))
        })?
    } else {
        Tz::UTC
    };

    // Try explicit format first
    if let Some(fmt) = format {
        if let Ok(naive_dt) = NaiveDateTime::parse_from_str(s, fmt) {
            return Ok(DateTimeWrapper::new(
                default_tz.from_utc_datetime(&naive_dt),
            ));
        }
        // Also try with timezone-aware parsing for explicit format
        if let Ok(dt) = DateTime::parse_from_str(s, fmt) {
            return Ok(DateTimeWrapper::new(dt.with_timezone(&default_tz)));
        }

        // If explicit format was provided but failed, return error immediately
        // Don't fall back to adaptive parsing
        return Err(Box::new(EvalAltResult::ErrorRuntime(
            format!("Failed to parse '{}' with format '{}'", s, fmt).into(),
            Position::NONE,
        )));
    }

    // For auto-parsing (no explicit format), use the adaptive parser
    // Rhai scripts use UTC interpretation for consistency
    let parsed_utc = RHAI_TS_PARSER.with(|parser| {
        parser
            .borrow_mut()
            .parse_ts_with_config(s, None, Some("UTC"))
    });

    if let Some(utc_dt) = parsed_utc {
        // Convert to the requested timezone
        let tz_dt = if default_tz == Tz::UTC {
            utc_dt.with_timezone(&Tz::UTC)
        } else {
            utc_dt.with_timezone(&default_tz)
        };
        return Ok(DateTimeWrapper::new(tz_dt));
    }

    Err(Box::new(EvalAltResult::ErrorRuntime(
        format!("Unable to parse timestamp: '{}'", s).into(),
        Position::NONE,
    )))
}

/// Convert a string like "1h 30m" or "2d" into a `DurationWrapper`.
pub fn to_duration(s: &str) -> Result<DurationWrapper, Box<EvalAltResult>> {
    let mut total_duration = Duration::zero();
    let mut current_number = String::new();
    let mut current_unit = String::new();
    let mut found_unit = false;

    fn push_duration(
        total: &mut Duration,
        number: &str,
        unit: &str,
    ) -> Result<(), Box<EvalAltResult>> {
        if number.is_empty() || unit.is_empty() {
            return Err(Box::new(EvalAltResult::ErrorRuntime(
                "Incomplete duration segment".into(),
                Position::NONE,
            )));
        }

        let value: f64 = number.parse().map_err(|_| {
            Box::new(EvalAltResult::ErrorRuntime(
                format!("Invalid number in duration: '{}'", number).into(),
                Position::NONE,
            ))
        })?;

        let unit_norm = unit.to_lowercase();
        let nanos_per_unit: f64 = match unit_norm.as_str() {
            "ns" | "nsec" | "nsecs" | "nanosecond" | "nanoseconds" => 1.0,
            "us" | "µs" | "usec" | "usecs" | "microsecond" | "microseconds" => 1_000.0,
            "ms" | "msec" | "msecs" | "millisecond" | "milliseconds" => 1_000_000.0,
            "s" | "sec" | "secs" | "second" | "seconds" => 1_000_000_000.0,
            "m" | "min" | "mins" | "minute" | "minutes" => 60.0 * 1_000_000_000.0,
            "h" | "hr" | "hrs" | "hour" | "hours" => 3_600.0 * 1_000_000_000.0,
            "d" | "day" | "days" => 86_400.0 * 1_000_000_000.0,
            "w" | "week" | "weeks" => 604_800.0 * 1_000_000_000.0,
            _ => {
                return Err(Box::new(EvalAltResult::ErrorRuntime(
                    format!("Unknown duration unit: '{}'", unit).into(),
                    Position::NONE,
                )))
            }
        };

        let nanos = (value * nanos_per_unit).round();
        if !nanos.is_finite() {
            return Err(Box::new(EvalAltResult::ErrorRuntime(
                "Duration value out of range".into(),
                Position::NONE,
            )));
        }

        let nanos_i128 = nanos as i128;
        if nanos_i128 > i64::MAX as i128 || nanos_i128 < i64::MIN as i128 {
            return Err(Box::new(EvalAltResult::ErrorRuntime(
                "Duration value out of range".into(),
                Position::NONE,
            )));
        }

        *total += Duration::nanoseconds(nanos_i128 as i64);
        Ok(())
    }

    let mut chars = s.chars().peekable();
    let mut number_has_decimal = false;
    while let Some(ch) = chars.next() {
        if ch.is_ascii_whitespace() {
            if !current_unit.is_empty() {
                push_duration(&mut total_duration, &current_number, &current_unit)?;
                current_number.clear();
                current_unit.clear();
                number_has_decimal = false;
                found_unit = true;
            }
            continue;
        }

        if ch.is_ascii_digit() || ch == '.' {
            if ch == '.' {
                if number_has_decimal {
                    return Err(Box::new(EvalAltResult::ErrorRuntime(
                        "Multiple decimal points in duration number".into(),
                        Position::NONE,
                    )));
                }
                if current_number.is_empty() {
                    return Err(Box::new(EvalAltResult::ErrorRuntime(
                        "Duration numbers cannot start with a decimal point".into(),
                        Position::NONE,
                    )));
                }
                number_has_decimal = true;
            }

            if !current_unit.is_empty() {
                push_duration(&mut total_duration, &current_number, &current_unit)?;
                current_number.clear();
                current_unit.clear();
                number_has_decimal = ch == '.';
                found_unit = true;
                if ch == '.' {
                    return Err(Box::new(EvalAltResult::ErrorRuntime(
                        "Duration numbers cannot start with a decimal point".into(),
                        Position::NONE,
                    )));
                }
            }

            current_number.push(ch);
        } else if ch.is_ascii_alphabetic() || ch == 'µ' {
            if current_number.is_empty() {
                return Err(Box::new(EvalAltResult::ErrorRuntime(
                    "Duration unit must follow a number".into(),
                    Position::NONE,
                )));
            }
            current_unit.push(ch);

            if let Some(next) = chars.peek() {
                if next.is_ascii_whitespace() {
                    continue;
                }
                if next.is_ascii_digit() || *next == '.' {
                    push_duration(&mut total_duration, &current_number, &current_unit)?;
                    current_number.clear();
                    current_unit.clear();
                    number_has_decimal = false;
                    found_unit = true;
                }
            }
        } else {
            return Err(Box::new(EvalAltResult::ErrorRuntime(
                format!("Invalid character in duration: '{}'", ch).into(),
                Position::NONE,
            )));
        }
    }

    if !current_unit.is_empty() {
        push_duration(&mut total_duration, &current_number, &current_unit)?;
        found_unit = true;
    }

    if !found_unit {
        return Err(Box::new(EvalAltResult::ErrorRuntime(
            format!("Unable to parse duration: '{}'", s).into(),
            Position::NONE,
        )));
    }

    Ok(DurationWrapper::new(total_duration))
}

/// Floor-divide: always round toward negative infinity.
/// Rust's `/` truncates toward zero, which is wrong for negative timestamps.
fn floor_nanos(timestamp_nanos: i64, interval_nanos: i64) -> i64 {
    let d = timestamp_nanos / interval_nanos;
    let r = timestamp_nanos % interval_nanos;
    // If remainder is negative, we truncated toward zero — adjust down by one interval
    let floored_d = if r < 0 { d - 1 } else { d };
    floored_d * interval_nanos
}

fn parse_positive_interval_nanos(interval: &str) -> Result<i64, Box<EvalAltResult>> {
    let duration = to_duration(interval)?;
    let nanos = duration.inner.num_nanoseconds().ok_or_else(|| {
        EvalAltResult::ErrorRuntime("Duration out of range".into(), Position::NONE)
    })?;
    if nanos <= 0 {
        return Err(Box::new(EvalAltResult::ErrorRuntime(
            "Interval must be positive".into(),
            Position::NONE,
        )));
    }
    Ok(nanos)
}

/// Round a datetime down to the nearest interval
pub fn round_to(
    dt: &mut DateTimeWrapper,
    interval: &str,
) -> Result<DateTimeWrapper, Box<EvalAltResult>> {
    let interval_nanos = parse_positive_interval_nanos(interval)?;
    let timestamp_nanos = dt.inner.timestamp_nanos_opt().ok_or_else(|| {
        EvalAltResult::ErrorRuntime("Timestamp out of range".into(), Position::NONE)
    })?;

    let floored = floor_nanos(timestamp_nanos, interval_nanos);
    let rounded_dt = Utc
        .timestamp_nanos(floored)
        .with_timezone(&dt.inner.timezone());

    Ok(DateTimeWrapper::new(rounded_dt))
}

/// Round a datetime up to the next interval boundary (ceiling)
///
/// If the timestamp is already exactly on a boundary, it stays unchanged.
/// Otherwise it advances to the next boundary.
pub fn ceil_to(
    dt: &mut DateTimeWrapper,
    interval: &str,
) -> Result<DateTimeWrapper, Box<EvalAltResult>> {
    let interval_nanos = parse_positive_interval_nanos(interval)?;
    let timestamp_nanos = dt.inner.timestamp_nanos_opt().ok_or_else(|| {
        EvalAltResult::ErrorRuntime("Timestamp out of range".into(), Position::NONE)
    })?;

    let floored = floor_nanos(timestamp_nanos, interval_nanos);
    let ceiled = if floored == timestamp_nanos {
        floored
    } else {
        floored + interval_nanos
    };

    let ceiled_dt = Utc
        .timestamp_nanos(ceiled)
        .with_timezone(&dt.inner.timezone());

    Ok(DateTimeWrapper::new(ceiled_dt))
}

/// Register all datetime functions with the Rhai engine
pub fn register_functions(engine: &mut Engine) {
    // Parsing functions
    engine.register_fn(
        "to_datetime",
        |s: &str| -> Result<DateTimeWrapper, Box<EvalAltResult>> { to_datetime(s, None, None) },
    );

    engine.register_fn(
        "to_datetime",
        |s: &str, format: &str| -> Result<DateTimeWrapper, Box<EvalAltResult>> {
            to_datetime(s, Some(format), None)
        },
    );

    engine.register_fn(
        "to_datetime",
        |s: &str, format: &str, tz: &str| -> Result<DateTimeWrapper, Box<EvalAltResult>> {
            to_datetime(s, Some(format), Some(tz))
        },
    );

    engine.register_fn("to_duration", to_duration);

    // Current time helper
    engine.register_fn("now", || DateTimeWrapper::from_utc(Utc::now()));

    // Duration creation functions
    engine.register_fn("duration_from_seconds", DurationWrapper::from_seconds);
    engine.register_fn("duration_from_minutes", DurationWrapper::from_minutes);
    engine.register_fn("duration_from_hours", DurationWrapper::from_hours);
    engine.register_fn("duration_from_days", DurationWrapper::from_days);
    engine.register_fn(
        "duration_from_milliseconds",
        DurationWrapper::from_milliseconds,
    );
    engine.register_fn(
        "duration_from_nanoseconds",
        DurationWrapper::from_nanoseconds,
    );

    // Humanize milliseconds to readable duration format
    engine.register_fn("humanize_duration", |ms: i64| -> String {
        DurationWrapper::from_milliseconds(ms).to_string()
    });

    // Register the custom types
    engine
        .register_type::<DateTimeWrapper>()
        .register_type::<DurationWrapper>();

    // DateTime methods
    engine.register_fn("to_utc", |dt: &mut DateTimeWrapper| {
        DateTimeWrapper::new(dt.inner.with_timezone(&Tz::UTC))
    });

    engine.register_fn("to_local", |dt: &mut DateTimeWrapper| {
        let local_tz = chrono_tz::Tz::from_str("UTC").unwrap(); // This should be system local timezone
        DateTimeWrapper::new(dt.inner.with_timezone(&local_tz))
    });

    engine.register_fn(
        "to_timezone",
        |dt: &mut DateTimeWrapper, tz: &str| -> Result<DateTimeWrapper, Box<EvalAltResult>> {
            let timezone = tz.parse::<Tz>().map_err(|e| {
                Box::new(EvalAltResult::ErrorRuntime(
                    format!("Invalid timezone '{}': {}", tz, e).into(),
                    Position::NONE,
                ))
            })?;
            Ok(DateTimeWrapper::new(dt.inner.with_timezone(&timezone)))
        },
    );

    engine.register_fn("to_iso", |dt: &mut DateTimeWrapper| -> String {
        dt.inner.to_rfc3339()
    });

    engine.register_fn("format", |dt: &mut DateTimeWrapper, fmt: &str| -> String {
        dt.inner.format(fmt).to_string()
    });

    engine.register_fn("year", |dt: &mut DateTimeWrapper| dt.inner.year() as i64);
    engine.register_fn("month", |dt: &mut DateTimeWrapper| dt.inner.month() as i64);
    engine.register_fn("day", |dt: &mut DateTimeWrapper| dt.inner.day() as i64);
    engine.register_fn("hour", |dt: &mut DateTimeWrapper| dt.inner.hour() as i64);
    engine.register_fn("minute", |dt: &mut DateTimeWrapper| {
        dt.inner.minute() as i64
    });
    engine.register_fn("second", |dt: &mut DateTimeWrapper| {
        dt.inner.second() as i64
    });
    engine.register_fn("ts_nanos", |dt: &mut DateTimeWrapper| {
        dt.inner.timestamp_nanos_opt().unwrap_or(0)
    });
    engine.register_fn("timezone_name", |dt: &mut DateTimeWrapper| {
        dt.inner.timezone().to_string()
    });

    // Time bucketing
    engine.register_fn("round_to", round_to);
    engine.register_fn("ceil_to", ceil_to);

    // Duration methods
    engine.register_fn("as_seconds", |dur: &mut DurationWrapper| {
        dur.inner.num_seconds()
    });
    engine.register_fn("as_milliseconds", |dur: &mut DurationWrapper| {
        dur.inner.num_milliseconds()
    });
    engine.register_fn("as_nanoseconds", |dur: &mut DurationWrapper| {
        dur.inner.num_nanoseconds().unwrap_or(0)
    });
    engine.register_fn("as_minutes", |dur: &mut DurationWrapper| {
        dur.inner.num_minutes()
    });
    engine.register_fn("as_hours", |dur: &mut DurationWrapper| {
        dur.inner.num_hours()
    });
    engine.register_fn("as_days", |dur: &mut DurationWrapper| dur.inner.num_days());

    // Duration string conversion - enables to_string() and print() formatting
    engine.register_fn("to_string", |dur: &mut DurationWrapper| -> String {
        dur.to_string()
    });
    engine.register_fn("to_debug", |dur: &mut DurationWrapper| -> String {
        dur.to_string()
    });

    // DateTime arithmetic
    engine.register_fn("+", |dt: DateTimeWrapper, dur: DurationWrapper| {
        DateTimeWrapper::new(dt.inner + dur.inner)
    });

    engine.register_fn("-", |dt: DateTimeWrapper, dur: DurationWrapper| {
        DateTimeWrapper::new(dt.inner - dur.inner)
    });

    engine.register_fn("-", |dt1: DateTimeWrapper, dt2: DateTimeWrapper| {
        DurationWrapper::new((dt1.inner - dt2.inner).abs())
    });

    // Duration arithmetic
    engine.register_fn("+", |dur1: DurationWrapper, dur2: DurationWrapper| {
        DurationWrapper::new(dur1.inner + dur2.inner)
    });

    engine.register_fn("-", |dur1: DurationWrapper, dur2: DurationWrapper| {
        DurationWrapper::new((dur1.inner - dur2.inner).abs())
    });

    engine.register_fn("*", |dur: DurationWrapper, n: i64| {
        DurationWrapper::new(dur.inner * n as i32)
    });

    engine.register_fn("/", |dur: DurationWrapper, n: i64| {
        DurationWrapper::new(dur.inner / n as i32)
    });

    // Duration comparison
    engine.register_fn("==", |dur1: DurationWrapper, dur2: DurationWrapper| {
        dur1.inner == dur2.inner
    });
    engine.register_fn("!=", |dur1: DurationWrapper, dur2: DurationWrapper| {
        dur1.inner != dur2.inner
    });
    engine.register_fn(">", |dur1: DurationWrapper, dur2: DurationWrapper| {
        dur1.inner > dur2.inner
    });
    engine.register_fn("<", |dur1: DurationWrapper, dur2: DurationWrapper| {
        dur1.inner < dur2.inner
    });
    engine.register_fn(">=", |dur1: DurationWrapper, dur2: DurationWrapper| {
        dur1.inner >= dur2.inner
    });
    engine.register_fn("<=", |dur1: DurationWrapper, dur2: DurationWrapper| {
        dur1.inner <= dur2.inner
    });

    // DateTime comparison
    engine.register_fn("==", |dt1: DateTimeWrapper, dt2: DateTimeWrapper| {
        dt1.inner == dt2.inner
    });
    engine.register_fn("!=", |dt1: DateTimeWrapper, dt2: DateTimeWrapper| {
        dt1.inner != dt2.inner
    });
    engine.register_fn(">", |dt1: DateTimeWrapper, dt2: DateTimeWrapper| {
        dt1.inner > dt2.inner
    });
    engine.register_fn("<", |dt1: DateTimeWrapper, dt2: DateTimeWrapper| {
        dt1.inner < dt2.inner
    });
    engine.register_fn(">=", |dt1: DateTimeWrapper, dt2: DateTimeWrapper| {
        dt1.inner >= dt2.inner
    });
    engine.register_fn("<=", |dt1: DateTimeWrapper, dt2: DateTimeWrapper| {
        dt1.inner <= dt2.inner
    });
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_duration_wrapper_always_positive() {
        // Test that negative durations become positive
        let negative_dur = Duration::seconds(-100);
        let wrapper = DurationWrapper::new(negative_dur);
        assert_eq!(wrapper.inner.num_seconds(), 100);
    }

    #[test]
    fn test_duration_display_formatting() {
        // Test various duration display formats
        assert_eq!(DurationWrapper::from_seconds(30).to_string(), "30s");
        assert_eq!(DurationWrapper::from_seconds(60).to_string(), "1m");
        assert_eq!(DurationWrapper::from_seconds(90).to_string(), "1m 30s");
        assert_eq!(DurationWrapper::from_seconds(3600).to_string(), "1h");
        assert_eq!(DurationWrapper::from_seconds(3660).to_string(), "1h 1m");
        assert_eq!(DurationWrapper::from_seconds(86400).to_string(), "1d");
        assert_eq!(DurationWrapper::from_seconds(90000).to_string(), "1d 1h");
    }

    #[test]
    fn test_to_datetime_edge_cases() {
        // Test empty string
        assert!(to_datetime("", None, None).is_err());

        // Test invalid formats
        assert!(to_datetime("not-a-date", None, None).is_err());
        assert!(to_datetime("2023-13-01T12:00:00Z", None, None).is_err()); // Invalid month
        assert!(to_datetime("2023-02-30T12:00:00Z", None, None).is_err()); // Invalid day

        // Test valid edge cases
        assert!(to_datetime("2023-01-01T00:00:00Z", None, None).is_ok());
        assert!(to_datetime("2023-12-31T23:59:59Z", None, None).is_ok());
    }

    #[test]
    fn test_to_datetime_with_explicit_format() {
        // Test custom format parsing
        let result = to_datetime("2023/07/04 12:34:56", Some("%Y/%m/%d %H:%M:%S"), None);
        assert!(result.is_ok());
        let dt = result.unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 7);
        assert_eq!(dt.inner.day(), 4);

        // Test invalid format with explicit format
        assert!(to_datetime("2023-07-04", Some("%Y/%m/%d"), None).is_err());
    }

    #[test]
    fn test_to_datetime_with_timezone() {
        // Test parsing with valid timezone
        let result = to_datetime(
            "2023-07-04 12:34:56",
            Some("%Y-%m-%d %H:%M:%S"),
            Some("UTC"),
        );
        assert!(result.is_ok());

        // Test parsing with invalid timezone
        let result = to_datetime(
            "2023-07-04 12:34:56",
            Some("%Y-%m-%d %H:%M:%S"),
            Some("INVALID"),
        );
        assert!(result.is_err());
    }

    #[test]
    fn test_to_duration_edge_cases() {
        // Test empty string
        assert!(to_duration("").is_err());

        // Test invalid characters
        assert!(to_duration("1x").is_err());
        assert!(to_duration("1h@30m").is_err());

        // Test invalid numbers
        assert!(to_duration("ah").is_err());

        // Test zero duration
        assert!(to_duration("0s").is_ok());

        // Test complex valid durations
        assert!(to_duration("1d 2h 3m 4s").is_ok());
        assert!(to_duration("100h").is_ok());
    }

    #[test]
    fn test_to_duration_various_formats() {
        // Test single units
        let dur_s = to_duration("30s").unwrap();
        assert_eq!(dur_s.inner.num_seconds(), 30);

        let dur_m = to_duration("5m").unwrap();
        assert_eq!(dur_m.inner.num_minutes(), 5);

        let dur_h = to_duration("2h").unwrap();
        assert_eq!(dur_h.inner.num_hours(), 2);

        let dur_d = to_duration("3d").unwrap();
        assert_eq!(dur_d.inner.num_days(), 3);

        let dur_ms = to_duration("250ms").unwrap();
        assert_eq!(dur_ms.inner.num_milliseconds(), 250);

        let dur_us = to_duration("500us").unwrap();
        assert_eq!(dur_us.inner.num_microseconds().unwrap(), 500);

        let dur_ns = to_duration("750ns").unwrap();
        assert_eq!(dur_ns.inner.num_nanoseconds().unwrap(), 750);

        // Test mixed units
        let dur_mixed = to_duration("1h 30m").unwrap();
        assert_eq!(dur_mixed.inner.num_minutes(), 90);

        // Test with extra spaces
        let dur_spaced = to_duration("  1h   30m  ").unwrap();
        assert_eq!(dur_spaced.inner.num_minutes(), 90);

        // Test compact format without spaces
        let dur_compact = to_duration("1m30s").unwrap();
        assert_eq!(dur_compact.inner.num_seconds(), 90);

        // Test millisecond subsequence with additional unit
        let dur_combo = to_duration("2s500ms").unwrap();
        assert_eq!(dur_combo.inner.num_milliseconds(), 2500);

        // Test fractional values
        let dur_fractional = to_duration("1.5s").unwrap();
        assert_eq!(dur_fractional.inner.num_milliseconds(), 1500);

        let dur_fractional_ms = to_duration("0.25ms").unwrap();
        assert_eq!(dur_fractional_ms.inner.num_nanoseconds().unwrap(), 250_000);

        let dur_fractional_minutes = to_duration("1.25m").unwrap();
        assert_eq!(dur_fractional_minutes.inner.num_seconds(), 75);
    }

    #[test]
    fn test_duration_arithmetic_non_negative() {
        let dur1 = DurationWrapper::from_hours(2);
        let dur2 = DurationWrapper::from_hours(3);

        // Subtraction that would normally be negative becomes positive
        let result = DurationWrapper::new((dur1.inner - dur2.inner).abs());
        assert_eq!(result.inner.num_hours(), 1);
    }

    #[test]
    fn test_datetime_wrapper_display() {
        let dt = to_datetime("2023-07-04T12:34:56Z", None, None).unwrap();
        let display_str = dt.to_string();
        assert!(display_str.contains("2023-07-04"));
        assert!(display_str.contains("12:34:56"));
    }

    #[test]
    fn test_datetime_component_access() {
        let dt = to_datetime("2023-07-04T12:34:56Z", None, None).unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 7);
        assert_eq!(dt.inner.day(), 4);
        assert_eq!(dt.inner.hour(), 12);
        assert_eq!(dt.inner.minute(), 34);
        assert_eq!(dt.inner.second(), 56);
    }

    #[test]
    fn test_duration_conversions() {
        let dur = DurationWrapper::from_hours(2);
        assert_eq!(dur.inner.num_hours(), 2);
        assert_eq!(dur.inner.num_minutes(), 120);
        assert_eq!(dur.inner.num_seconds(), 7200);

        let dur_ms = DurationWrapper::from_milliseconds(5000);
        assert_eq!(dur_ms.inner.num_seconds(), 5);
    }

    #[test]
    fn test_rfc3339_and_rfc2822_parsing() {
        // RFC3339
        let rfc3339_result = to_datetime("2023-07-04T12:34:56+00:00", None, None);
        assert!(rfc3339_result.is_ok());

        // RFC2822
        let rfc2822_result = to_datetime("Tue, 04 Jul 2023 12:34:56 +0000", None, None);
        assert!(rfc2822_result.is_ok());
    }

    #[test]
    fn test_standard_format_parsing() {
        // Apache log format
        let apache_result = to_datetime("04/Jul/2023:12:34:56 +0000", None, None);
        assert!(apache_result.is_ok());

        // Common log format
        let common_result = to_datetime("2023-07-04 12:34:56", None, None);
        assert!(common_result.is_ok());

        // ISO 8601 variants
        let iso_result = to_datetime("2023-07-04T12:34:56.123Z", None, None);
        assert!(iso_result.is_ok());
    }

    #[test]
    fn test_large_duration_values() {
        // Test very large durations
        let large_dur = DurationWrapper::from_days(365);
        assert_eq!(large_dur.inner.num_days(), 365);

        // Test nanosecond precision
        let nano_dur = DurationWrapper::from_nanoseconds(1_000_000_000);
        assert_eq!(nano_dur.inner.num_seconds(), 1);
    }

    #[test]
    fn test_boundary_conditions() {
        // Test leap year
        let leap_year_result = to_datetime("2024-02-29T12:00:00Z", None, None);
        assert!(leap_year_result.is_ok());

        // Test non-leap year (should fail)
        let non_leap_result = to_datetime("2023-02-29T12:00:00Z", None, None);
        assert!(non_leap_result.is_err());

        // Test year boundaries
        let y2k_result = to_datetime("2000-01-01T00:00:00Z", None, None);
        assert!(y2k_result.is_ok());
    }

    #[test]
    fn test_unix_timestamp_parsing() {
        // Test Unix timestamp in seconds (10 digits)
        let unix_seconds = to_datetime("1735566123", None, None);
        assert!(unix_seconds.is_ok());
        let dt = unix_seconds.unwrap();
        assert_eq!(dt.inner.year(), 2024);
        assert_eq!(dt.inner.month(), 12);
        assert_eq!(dt.inner.day(), 30);

        // Test Unix timestamp in milliseconds (13 digits)
        let unix_millis = to_datetime("1735566123000", None, None);
        assert!(unix_millis.is_ok());
        let dt = unix_millis.unwrap();
        assert_eq!(dt.inner.year(), 2024);
        assert_eq!(dt.inner.month(), 12);
        assert_eq!(dt.inner.day(), 30);

        // Test Unix timestamp in microseconds (16 digits)
        let unix_micros = to_datetime("1735566123000000", None, None);
        assert!(unix_micros.is_ok());
        let dt = unix_micros.unwrap();
        assert_eq!(dt.inner.year(), 2024);
        assert_eq!(dt.inner.month(), 12);
        assert_eq!(dt.inner.day(), 30);

        // Test Unix timestamp in nanoseconds (19 digits)
        let unix_nanos = to_datetime("1735566123000000000", None, None);
        assert!(unix_nanos.is_ok());
        let dt = unix_nanos.unwrap();
        assert_eq!(dt.inner.year(), 2024);
        assert_eq!(dt.inner.month(), 12);
        assert_eq!(dt.inner.day(), 30);

        // Test invalid Unix timestamp (wrong length)
        let invalid_unix = to_datetime("12345", None, None);
        assert!(invalid_unix.is_err());

        // Test Unix timestamp with non-numeric characters
        let invalid_chars = to_datetime("1735566123a", None, None);
        assert!(invalid_chars.is_err());
    }

    #[test]
    fn test_new_timestamp_formats() {
        // Test Python logging format with comma separator
        let python_result = to_datetime("2023-07-04 12:34:56,123", None, None);
        assert!(python_result.is_ok());
        let dt = python_result.unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 7);
        assert_eq!(dt.inner.day(), 4);
        assert_eq!(dt.inner.hour(), 12);
        assert_eq!(dt.inner.minute(), 34);
        assert_eq!(dt.inner.second(), 56);

        // Note: Ambiguous formats like "7/4/2023 12:34:56 PM" are not supported
        // in automatic parsing due to month/day ambiguity. Use explicit format instead.

        // Test MySQL legacy format
        let mysql_result = to_datetime("230704 12:34:56", None, None);
        assert!(mysql_result.is_ok());
        let dt = mysql_result.unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 7);
        assert_eq!(dt.inner.day(), 4);
        assert_eq!(dt.inner.hour(), 12);
        assert_eq!(dt.inner.minute(), 34);
        assert_eq!(dt.inner.second(), 56);

        // Test Nginx error log format
        let nginx_result = to_datetime("2023/07/04 12:34:56", None, None);
        assert!(nginx_result.is_ok());
        let dt = nginx_result.unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 7);
        assert_eq!(dt.inner.day(), 4);
        assert_eq!(dt.inner.hour(), 12);
        assert_eq!(dt.inner.minute(), 34);
        assert_eq!(dt.inner.second(), 56);

        // Test BSD syslog with year
        let bsd_result = to_datetime("Jul 04 2023 12:34:56", None, None);
        assert!(bsd_result.is_ok());
        let dt = bsd_result.unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 7);
        assert_eq!(dt.inner.day(), 4);
        assert_eq!(dt.inner.hour(), 12);
        assert_eq!(dt.inner.minute(), 34);
        assert_eq!(dt.inner.second(), 56);

        // Test Java SimpleDateFormat
        let java_result = to_datetime("Jul 04, 2023 12:34:56 PM", None, None);
        assert!(java_result.is_ok());
        let dt = java_result.unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 7);
        assert_eq!(dt.inner.day(), 4);
        assert_eq!(dt.inner.hour(), 12);
        assert_eq!(dt.inner.minute(), 34);
        assert_eq!(dt.inner.second(), 56);

        // Test German format with dots
        let german_result = to_datetime("04.07.2023 12:34:56", None, None);
        assert!(german_result.is_ok());
        let dt = german_result.unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 7);
        assert_eq!(dt.inner.day(), 4);
        assert_eq!(dt.inner.hour(), 12);
        assert_eq!(dt.inner.minute(), 34);
        assert_eq!(dt.inner.second(), 56);
    }

    #[test]
    fn test_klp_inspired_formats() {
        // Test space-separated ISO 8601 with fractional seconds and Z
        let space_iso_frac_z = to_datetime("2023-07-04 12:34:56.123Z", None, None);
        assert!(space_iso_frac_z.is_ok());
        let dt = space_iso_frac_z.unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 7);
        assert_eq!(dt.inner.day(), 4);
        assert_eq!(dt.inner.hour(), 12);
        assert_eq!(dt.inner.minute(), 34);
        assert_eq!(dt.inner.second(), 56);

        // Test space-separated ISO 8601 without fractional seconds but with Z
        let space_iso_z = to_datetime("2023-07-04 12:34:56Z", None, None);
        assert!(space_iso_z.is_ok());
        let dt = space_iso_z.unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 7);
        assert_eq!(dt.inner.day(), 4);
        assert_eq!(dt.inner.hour(), 12);
        assert_eq!(dt.inner.minute(), 34);
        assert_eq!(dt.inner.second(), 56);

        // Test space-separated ISO 8601 with timezone offset
        let space_iso_tz = to_datetime("2023-07-04 12:34:56+0000", None, None);
        assert!(space_iso_tz.is_ok());
        let dt = space_iso_tz.unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 7);
        assert_eq!(dt.inner.day(), 4);
        assert_eq!(dt.inner.hour(), 12);
        assert_eq!(dt.inner.minute(), 34);
        assert_eq!(dt.inner.second(), 56);

        // Test space-separated ISO 8601 with fractional seconds and timezone
        let space_iso_frac_tz = to_datetime("2023-07-04 12:34:56.123+0000", None, None);
        assert!(space_iso_frac_tz.is_ok());
        let dt = space_iso_frac_tz.unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 7);
        assert_eq!(dt.inner.day(), 4);
        assert_eq!(dt.inner.hour(), 12);
        assert_eq!(dt.inner.minute(), 34);
        assert_eq!(dt.inner.second(), 56);

        // Test classic Unix timestamp with weekday
        // July 4th, 2023 was a Tuesday
        let unix_weekday = to_datetime("Tue Jul 04 12:34:56 2023", None, None);
        assert!(unix_weekday.is_ok());
        let dt = unix_weekday.unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 7);
        assert_eq!(dt.inner.day(), 4);
        assert_eq!(dt.inner.hour(), 12);
        assert_eq!(dt.inner.minute(), 34);
        assert_eq!(dt.inner.second(), 56);
    }

    #[test]
    fn test_oracle_format_parsing() {
        // Test Oracle format - this one is complex and may need adjustment
        let oracle_result = to_datetime("04-JUL-23 12:34:56.123 PM", None, None);
        assert!(oracle_result.is_ok());
        let dt = oracle_result.unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 7);
        assert_eq!(dt.inner.day(), 4);
        assert_eq!(dt.inner.hour(), 12);
        assert_eq!(dt.inner.minute(), 34);
        assert_eq!(dt.inner.second(), 56);
    }

    #[test]
    fn test_adaptive_parsing_in_rhai() {
        // Test that the Rhai to_datetime function benefits from adaptive parsing
        // by parsing similar formats multiple times

        // First parse - should learn the format
        let result1 = to_datetime("2023-07-04 12:34:56", None, None);
        assert!(result1.is_ok());
        let dt1 = result1.unwrap();
        assert_eq!(dt1.inner.year(), 2023);
        assert_eq!(dt1.inner.month(), 7);
        assert_eq!(dt1.inner.day(), 4);

        // Second parse with same format - should be faster due to learning
        let result2 = to_datetime("2023-07-05 13:45:07", None, None);
        assert!(result2.is_ok());
        let dt2 = result2.unwrap();
        assert_eq!(dt2.inner.year(), 2023);
        assert_eq!(dt2.inner.month(), 7);
        assert_eq!(dt2.inner.day(), 5);

        // Third parse with same format - should still work efficiently
        let result3 = to_datetime("2023-07-06 14:56:08", None, None);
        assert!(result3.is_ok());
        let dt3 = result3.unwrap();
        assert_eq!(dt3.inner.year(), 2023);
        assert_eq!(dt3.inner.month(), 7);
        assert_eq!(dt3.inner.day(), 6);
    }

    #[test]
    fn test_explicit_format_for_ambiguous_dates() {
        // Test US format (M/D/YYYY) with explicit format
        let us_result = to_datetime("7/4/2023 12:34:56 PM", Some("%m/%d/%Y %I:%M:%S %p"), None);
        assert!(us_result.is_ok());
        let dt = us_result.unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 7); // July (US interpretation)
        assert_eq!(dt.inner.day(), 4);
        assert_eq!(dt.inner.hour(), 12);

        // Test European format (D/M/YYYY) with explicit format
        let eu_result = to_datetime("7/4/2023 12:34:56", Some("%d/%m/%Y %H:%M:%S"), None);
        assert!(eu_result.is_ok());
        let dt = eu_result.unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 4); // April (European interpretation)
        assert_eq!(dt.inner.day(), 7);
        assert_eq!(dt.inner.hour(), 12);

        // Test that ambiguous formats fail without explicit format
        let ambiguous_result = to_datetime("7/4/2023 12:34:56 PM", None, None);
        assert!(
            ambiguous_result.is_err(),
            "Ambiguous date format should fail without explicit format"
        );

        // Test Windows Event Log format variations with explicit format
        let windows_us = to_datetime("12/31/2023 11:59:59 PM", Some("%m/%d/%Y %I:%M:%S %p"), None);
        assert!(windows_us.is_ok());
        let dt = windows_us.unwrap();
        assert_eq!(dt.inner.year(), 2023);
        assert_eq!(dt.inner.month(), 12);
        assert_eq!(dt.inner.day(), 31);
        assert_eq!(dt.inner.hour(), 23); // 11 PM = 23:00
    }

    #[test]
    fn test_unix_timestamp_edge_cases() {
        // Test earliest Unix timestamp (1970-01-01 00:00:00)
        let epoch_result = to_datetime("0", None, None);
        assert!(epoch_result.is_err()); // Single digit should fail

        let epoch_result = to_datetime("0000000000", None, None);
        assert!(epoch_result.is_ok());
        let dt = epoch_result.unwrap();
        assert_eq!(dt.inner.year(), 1970);
        assert_eq!(dt.inner.month(), 1);
        assert_eq!(dt.inner.day(), 1);

        // Test year 2038 problem boundary
        let y2038_result = to_datetime("2147483647", None, None);
        assert!(y2038_result.is_ok());
        let dt = y2038_result.unwrap();
        assert_eq!(dt.inner.year(), 2038);
        assert_eq!(dt.inner.month(), 1);
        assert_eq!(dt.inner.day(), 19);

        // Test millisecond precision
        let millis_result = to_datetime("1735566123456", None, None);
        assert!(millis_result.is_ok());
        let dt = millis_result.unwrap();
        assert_eq!(dt.inner.year(), 2024);
        assert_eq!(dt.inner.month(), 12);
        assert_eq!(dt.inner.day(), 30);
    }

    #[test]
    fn test_round_to_minutes() {
        // 2023-07-04 12:34:56 UTC
        let mut dt = to_datetime("2023-07-04 12:34:56Z", None, None).unwrap();

        // Round to 5 minutes - should go to 12:30:00
        let rounded = round_to(&mut dt, "5m").unwrap();
        assert_eq!(rounded.inner.hour(), 12);
        assert_eq!(rounded.inner.minute(), 30);
        assert_eq!(rounded.inner.second(), 0);

        // Round to 1 minute - should go to 12:34:00
        let rounded = round_to(&mut dt, "1m").unwrap();
        assert_eq!(rounded.inner.hour(), 12);
        assert_eq!(rounded.inner.minute(), 34);
        assert_eq!(rounded.inner.second(), 0);
    }

    #[test]
    fn test_round_to_hours() {
        // 2023-07-04 12:34:56 UTC
        let mut dt = to_datetime("2023-07-04 12:34:56Z", None, None).unwrap();

        // Round to 1 hour - should go to 12:00:00
        let rounded = round_to(&mut dt, "1h").unwrap();
        assert_eq!(rounded.inner.hour(), 12);
        assert_eq!(rounded.inner.minute(), 0);
        assert_eq!(rounded.inner.second(), 0);

        // Round to 6 hours - should go to 12:00:00
        let rounded = round_to(&mut dt, "6h").unwrap();
        assert_eq!(rounded.inner.hour(), 12);
        assert_eq!(rounded.inner.minute(), 0);
        assert_eq!(rounded.inner.second(), 0);

        // Test 15:34:56 rounds to 12:00:00 with 6h interval
        let mut dt2 = to_datetime("2023-07-04 15:34:56Z", None, None).unwrap();
        let rounded = round_to(&mut dt2, "6h").unwrap();
        assert_eq!(rounded.inner.hour(), 12);
    }

    #[test]
    fn test_round_to_days() {
        // 2023-07-04 12:34:56 UTC
        let mut dt = to_datetime("2023-07-04 12:34:56Z", None, None).unwrap();

        // Round to 1 day - should go to 2023-07-04 00:00:00
        let rounded = round_to(&mut dt, "1d").unwrap();
        assert_eq!(rounded.inner.year(), 2023);
        assert_eq!(rounded.inner.month(), 7);
        assert_eq!(rounded.inner.day(), 4);
        assert_eq!(rounded.inner.hour(), 0);
        assert_eq!(rounded.inner.minute(), 0);
        assert_eq!(rounded.inner.second(), 0);
    }

    #[test]
    fn test_round_to_seconds() {
        // 2023-07-04 12:34:56.789Z
        let mut dt = to_datetime("2023-07-04T12:34:56.789Z", None, None).unwrap();

        // Round to 10 seconds - should go to 12:34:50.000
        let rounded = round_to(&mut dt, "10s").unwrap();
        assert_eq!(rounded.inner.hour(), 12);
        assert_eq!(rounded.inner.minute(), 34);
        assert_eq!(rounded.inner.second(), 50);

        // Round to 30 seconds - should go to 12:34:30.000
        let rounded = round_to(&mut dt, "30s").unwrap();
        assert_eq!(rounded.inner.hour(), 12);
        assert_eq!(rounded.inner.minute(), 34);
        assert_eq!(rounded.inner.second(), 30);
    }

    #[test]
    fn test_round_to_preserves_timezone() {
        // Parse as UTC then convert to America/New_York
        // This creates 2023-07-04 12:34:56 UTC, which is 08:34:56 EDT
        let dt_utc = to_datetime("2023-07-04 12:34:56Z", None, None).unwrap();
        let mut dt = DateTimeWrapper::new(
            dt_utc
                .inner
                .with_timezone(&"America/New_York".parse::<Tz>().unwrap()),
        );

        let rounded = round_to(&mut dt, "1h").unwrap();
        assert_eq!(rounded.inner.timezone().to_string(), "America/New_York");
        // 12:34:56 UTC = 08:34:56 EDT, rounds to 08:00:00 EDT
        assert_eq!(rounded.inner.hour(), 8);
        assert_eq!(rounded.inner.minute(), 0);
    }

    #[test]
    fn test_round_to_error_cases() {
        let mut dt = to_datetime("2023-07-04 12:34:56Z", None, None).unwrap();

        // Invalid duration format
        assert!(round_to(&mut dt, "invalid").is_err());

        // Empty string
        assert!(round_to(&mut dt, "").is_err());
    }

    #[test]
    fn test_round_to_edge_cases() {
        // Test rounding exactly on the boundary
        let mut dt = to_datetime("2023-07-04 12:30:00Z", None, None).unwrap();
        let rounded = round_to(&mut dt, "5m").unwrap();
        assert_eq!(rounded.inner.hour(), 12);
        assert_eq!(rounded.inner.minute(), 30);
        assert_eq!(rounded.inner.second(), 0);

        // Test midnight crossing with days
        let mut dt2 = to_datetime("2023-07-05 00:00:00Z", None, None).unwrap();
        let rounded = round_to(&mut dt2, "1d").unwrap();
        assert_eq!(rounded.inner.day(), 5);
        assert_eq!(rounded.inner.hour(), 0);
    }

    #[test]
    fn test_ceil_to_minutes() {
        // 12:34:56 ceil to 5m -> 12:35:00
        let mut dt = to_datetime("2023-07-04 12:34:56Z", None, None).unwrap();
        let ceiled = ceil_to(&mut dt, "5m").unwrap();
        assert_eq!(ceiled.inner.hour(), 12);
        assert_eq!(ceiled.inner.minute(), 35);
        assert_eq!(ceiled.inner.second(), 0);
    }

    #[test]
    fn test_ceil_to_on_boundary() {
        // Already on a 5m boundary -> stays unchanged
        let mut dt = to_datetime("2023-07-04 12:30:00Z", None, None).unwrap();
        let ceiled = ceil_to(&mut dt, "5m").unwrap();
        assert_eq!(ceiled.inner.hour(), 12);
        assert_eq!(ceiled.inner.minute(), 30);
        assert_eq!(ceiled.inner.second(), 0);
    }

    #[test]
    fn test_ceil_to_hours() {
        // 12:34:56 ceil to 1h -> 13:00:00
        let mut dt = to_datetime("2023-07-04 12:34:56Z", None, None).unwrap();
        let ceiled = ceil_to(&mut dt, "1h").unwrap();
        assert_eq!(ceiled.inner.hour(), 13);
        assert_eq!(ceiled.inner.minute(), 0);
        assert_eq!(ceiled.inner.second(), 0);
    }

    #[test]
    fn test_ceil_to_on_hour_boundary() {
        // Already on hour boundary -> stays unchanged
        let mut dt = to_datetime("2023-07-04 12:00:00Z", None, None).unwrap();
        let ceiled = ceil_to(&mut dt, "1h").unwrap();
        assert_eq!(ceiled.inner.hour(), 12);
        assert_eq!(ceiled.inner.minute(), 0);
    }

    #[test]
    fn test_ceil_to_days() {
        // 12:34:56 ceil to 1d -> next day 00:00:00
        let mut dt = to_datetime("2023-07-04 12:34:56Z", None, None).unwrap();
        let ceiled = ceil_to(&mut dt, "1d").unwrap();
        assert_eq!(ceiled.inner.day(), 5);
        assert_eq!(ceiled.inner.hour(), 0);
        assert_eq!(ceiled.inner.minute(), 0);
    }

    #[test]
    fn test_ceil_to_error_cases() {
        let mut dt = to_datetime("2023-07-04 12:34:56Z", None, None).unwrap();
        assert!(ceil_to(&mut dt, "invalid").is_err());
        assert!(ceil_to(&mut dt, "").is_err());
    }

    #[test]
    fn test_ceil_to_preserves_timezone() {
        let dt_utc = to_datetime("2023-07-04 12:34:56Z", None, None).unwrap();
        let mut dt = DateTimeWrapper::new(
            dt_utc
                .inner
                .with_timezone(&"America/New_York".parse::<Tz>().unwrap()),
        );

        let ceiled = ceil_to(&mut dt, "1h").unwrap();
        assert_eq!(ceiled.inner.timezone().to_string(), "America/New_York");
    }

    // Pre-epoch tests: verify floor-toward-negative-infinity (not toward zero)

    #[test]
    fn test_round_to_pre_epoch() {
        // 1969-12-31 23:47:00 UTC — 13 minutes before epoch
        let mut dt = to_datetime("1969-12-31 23:47:00Z", None, None).unwrap();

        // Floor to 5m: should go to 23:45:00, not 23:50:00
        let rounded = round_to(&mut dt, "5m").unwrap();
        assert_eq!(rounded.inner.hour(), 23);
        assert_eq!(rounded.inner.minute(), 45);
        assert_eq!(rounded.inner.second(), 0);
    }

    #[test]
    fn test_round_to_pre_epoch_on_boundary() {
        // Exactly on a 5m boundary before epoch
        let mut dt = to_datetime("1969-12-31 23:45:00Z", None, None).unwrap();
        let rounded = round_to(&mut dt, "5m").unwrap();
        assert_eq!(rounded.inner.hour(), 23);
        assert_eq!(rounded.inner.minute(), 45);
    }

    #[test]
    fn test_ceil_to_pre_epoch() {
        // 1969-12-31 23:47:00 UTC
        let mut dt = to_datetime("1969-12-31 23:47:00Z", None, None).unwrap();

        // Ceil to 5m: should go to 23:50:00
        let ceiled = ceil_to(&mut dt, "5m").unwrap();
        assert_eq!(ceiled.inner.hour(), 23);
        assert_eq!(ceiled.inner.minute(), 50);
        assert_eq!(ceiled.inner.second(), 0);
    }

    #[test]
    fn test_ceil_to_pre_epoch_on_boundary() {
        // Exactly on a 5m boundary before epoch — should stay
        let mut dt = to_datetime("1969-12-31 23:45:00Z", None, None).unwrap();
        let ceiled = ceil_to(&mut dt, "5m").unwrap();
        assert_eq!(ceiled.inner.hour(), 23);
        assert_eq!(ceiled.inner.minute(), 45);
    }

    #[test]
    fn test_floor_nanos_positive() {
        assert_eq!(floor_nanos(7, 5), 5);
        assert_eq!(floor_nanos(10, 5), 10);
        assert_eq!(floor_nanos(0, 5), 0);
    }

    #[test]
    fn test_floor_nanos_negative() {
        // -7 floored by 5 should be -10, not -5
        assert_eq!(floor_nanos(-7, 5), -10);
        assert_eq!(floor_nanos(-10, 5), -10); // exactly on boundary
        assert_eq!(floor_nanos(-1, 5), -5);
    }
}