nautilus-core 0.56.0

Core functionality for the Nautilus trading engine
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
// -------------------------------------------------------------------------------------------------
//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
//  https://nautechsystems.io
//
//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
//  You may not use this file except in compliance with the License.
//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.
// -------------------------------------------------------------------------------------------------

//! Common data and time functions.
use std::convert::TryFrom;

use chrono::{DateTime, Datelike, NaiveDate, SecondsFormat, TimeDelta, Utc, Weekday};

use crate::{UnixNanos, time::nanos_since_unix_epoch};

/// Number of milliseconds in one second.
pub const MILLISECONDS_IN_SECOND: u64 = 1_000;

/// Number of nanoseconds in one second.
pub const NANOSECONDS_IN_SECOND: u64 = 1_000_000_000;

/// Number of nanoseconds in one millisecond.
pub const NANOSECONDS_IN_MILLISECOND: u64 = 1_000_000;

/// Number of nanoseconds in one microsecond.
pub const NANOSECONDS_IN_MICROSECOND: u64 = 1_000;

/// Number of nanoseconds in one minute.
pub const NANOSECONDS_IN_MINUTE: u64 = 60 * NANOSECONDS_IN_SECOND;

/// Number of nanoseconds in one day.
pub const NANOSECONDS_IN_DAY: u64 = 24 * 60 * NANOSECONDS_IN_MINUTE;

/// Number of seconds in one minute.
pub const SECONDS_IN_MINUTE: u64 = 60;

/// Number of seconds in one hour.
pub const SECONDS_IN_HOUR: u64 = 60 * SECONDS_IN_MINUTE;

/// Number of seconds in one day.
pub const SECONDS_IN_DAY: u64 = 24 * SECONDS_IN_HOUR;

// Maximum finite seconds input that can be converted to nanoseconds without overflowing `u64`.
#[expect(
    clippy::cast_precision_loss,
    reason = "deriving a max-representable bound; f64 precision loss is part of the semantics"
)]
const MAX_SECS_FOR_NANOS: f64 = u64::MAX as f64 / NANOSECONDS_IN_SECOND as f64;
// Maximum finite seconds input that can be converted to milliseconds without overflowing `u64`.
#[expect(
    clippy::cast_precision_loss,
    reason = "deriving a max-representable bound; f64 precision loss is part of the semantics"
)]
const MAX_SECS_FOR_MILLIS: f64 = u64::MAX as f64 / MILLISECONDS_IN_SECOND as f64;
// Maximum finite milliseconds input that can be converted to nanoseconds without overflowing `u64`.
#[expect(
    clippy::cast_precision_loss,
    reason = "deriving a max-representable bound; f64 precision loss is part of the semantics"
)]
const MAX_MILLIS_FOR_NANOS: f64 = u64::MAX as f64 / NANOSECONDS_IN_MILLISECOND as f64;
// Maximum finite microseconds input that can be converted to nanoseconds without overflowing `u64`.
#[expect(
    clippy::cast_precision_loss,
    reason = "deriving a max-representable bound; f64 precision loss is part of the semantics"
)]
const MAX_MICROS_FOR_NANOS: f64 = u64::MAX as f64 / NANOSECONDS_IN_MICROSECOND as f64;

// Compile-time checks for time constants to prevent accidental modification
const _: () = {
    assert!(NANOSECONDS_IN_SECOND == 1_000_000_000);
    assert!(NANOSECONDS_IN_MILLISECOND == 1_000_000);
    assert!(NANOSECONDS_IN_MICROSECOND == 1_000);
    assert!(MILLISECONDS_IN_SECOND == 1_000);
    assert!(NANOSECONDS_IN_SECOND == MILLISECONDS_IN_SECOND * NANOSECONDS_IN_MILLISECOND);
    assert!(NANOSECONDS_IN_MILLISECOND == NANOSECONDS_IN_MICROSECOND * 1_000);
    assert!(NANOSECONDS_IN_SECOND / NANOSECONDS_IN_MILLISECOND == 1_000);
    assert!(NANOSECONDS_IN_SECOND / NANOSECONDS_IN_MICROSECOND == 1_000_000);
    assert!(SECONDS_IN_MINUTE == 60);
    assert!(SECONDS_IN_HOUR == 3_600);
    assert!(SECONDS_IN_DAY == 86_400);
    assert!(NANOSECONDS_IN_MINUTE == 60 * NANOSECONDS_IN_SECOND);
    assert!(NANOSECONDS_IN_DAY == 24 * 60 * NANOSECONDS_IN_MINUTE);
};

#[inline]
fn unix_nanos_to_datetime(unix_nanos: UnixNanos) -> anyhow::Result<DateTime<Utc>> {
    let nanos_i64 = i64::try_from(unix_nanos.as_u64()).map_err(|_| {
        anyhow::anyhow!(
            "UnixNanos value {} exceeds maximum representable datetime (i64::MAX)",
            unix_nanos.as_u64()
        )
    })?;
    Ok(DateTime::from_timestamp_nanos(nanos_i64))
}

/// List of weekdays (Monday to Friday).
pub const WEEKDAYS: [Weekday; 5] = [
    Weekday::Mon,
    Weekday::Tue,
    Weekday::Wed,
    Weekday::Thu,
    Weekday::Fri,
];

/// Converts seconds to nanoseconds (ns).
///
/// # Errors
///
/// Returns an error if `secs` is non-finite or exceeds `MAX_SECS_FOR_NANOS`.
#[expect(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss,
    reason = "Intentional for unit conversion, may lose precision after clamping"
)]
pub fn secs_to_nanos(secs: f64) -> anyhow::Result<u64> {
    anyhow::ensure!(secs.is_finite(), "seconds must be finite, was {secs}");
    if secs <= 0.0 {
        return Ok(0);
    }
    anyhow::ensure!(
        secs <= MAX_SECS_FOR_NANOS,
        "seconds {secs} exceeds maximum representable value {MAX_SECS_FOR_NANOS}"
    );
    let nanos = secs * NANOSECONDS_IN_SECOND as f64;
    Ok(nanos.trunc() as u64)
}

/// Converts seconds to milliseconds (ms).
///
/// # Errors
///
/// Returns an error if `secs` is non-finite or exceeds `MAX_SECS_FOR_MILLIS`.
#[expect(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss,
    reason = "Intentional for unit conversion, may lose precision after clamping"
)]
pub fn secs_to_millis(secs: f64) -> anyhow::Result<u64> {
    anyhow::ensure!(secs.is_finite(), "seconds must be finite, was {secs}");
    if secs <= 0.0 {
        return Ok(0);
    }
    anyhow::ensure!(
        secs <= MAX_SECS_FOR_MILLIS,
        "seconds {secs} exceeds maximum representable value {MAX_SECS_FOR_MILLIS}"
    );
    let millis = secs * MILLISECONDS_IN_SECOND as f64;
    Ok(millis.trunc() as u64)
}

/// Converts seconds to nanoseconds (ns), panicking on invalid input.
///
/// This is a convenience wrapper around [`secs_to_nanos`] when the caller expects
/// the input to be trusted and in-range.
///
/// # Panics
///
/// Panics if [`secs_to_nanos`] would return an error for `secs`.
#[must_use]
pub fn secs_to_nanos_unchecked(secs: f64) -> u64 {
    secs_to_nanos(secs).expect("secs_to_nanos_unchecked: invalid or overflowing input")
}

/// Converts minutes to seconds.
#[must_use]
pub const fn mins_to_secs(mins: u64) -> u64 {
    mins * SECONDS_IN_MINUTE
}

/// Converts minutes to nanoseconds.
#[must_use]
pub const fn mins_to_nanos(mins: u64) -> u64 {
    mins * NANOSECONDS_IN_MINUTE
}

/// Converts milliseconds (ms) to nanoseconds (ns).
///
/// Casting f64 to u64 by truncating the fractional part is intentional for unit conversion,
/// which may lose precision and drop negative values after clamping.
///
/// # Errors
///
/// Returns an error if `millis` is non-finite or exceeds `MAX_MILLIS_FOR_NANOS`.
#[expect(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss,
    reason = "Intentional for unit conversion, may lose precision after clamping"
)]
pub fn millis_to_nanos(millis: f64) -> anyhow::Result<u64> {
    anyhow::ensure!(
        millis.is_finite(),
        "milliseconds must be finite, was {millis}"
    );

    if millis <= 0.0 {
        return Ok(0);
    }
    anyhow::ensure!(
        millis <= MAX_MILLIS_FOR_NANOS,
        "milliseconds {millis} exceeds maximum representable value {MAX_MILLIS_FOR_NANOS}"
    );
    let nanos = millis * NANOSECONDS_IN_MILLISECOND as f64;
    Ok(nanos.trunc() as u64)
}

/// Converts milliseconds (ms) to nanoseconds (ns), panicking on invalid input.
///
/// # Panics
///
/// Panics if [`millis_to_nanos`] would return an error for `millis`.
#[must_use]
pub fn millis_to_nanos_unchecked(millis: f64) -> u64 {
    millis_to_nanos(millis).expect("millis_to_nanos_unchecked: invalid or overflowing input")
}

/// Converts microseconds (μs) to nanoseconds (ns).
///
/// Casting f64 to u64 by truncating the fractional part is intentional for unit conversion,
/// which may lose precision and drop negative values after clamping.
///
/// # Errors
///
/// Returns an error if `micros` is non-finite or exceeds `MAX_MICROS_FOR_NANOS`.
#[expect(
    clippy::cast_possible_truncation,
    clippy::cast_sign_loss,
    clippy::cast_precision_loss,
    reason = "Intentional for unit conversion, may lose precision after clamping"
)]
pub fn micros_to_nanos(micros: f64) -> anyhow::Result<u64> {
    anyhow::ensure!(
        micros.is_finite(),
        "microseconds must be finite, was {micros}"
    );

    if micros <= 0.0 {
        return Ok(0);
    }
    anyhow::ensure!(
        micros <= MAX_MICROS_FOR_NANOS,
        "microseconds {micros} exceeds maximum representable value {MAX_MICROS_FOR_NANOS}"
    );
    let nanos = micros * NANOSECONDS_IN_MICROSECOND as f64;
    Ok(nanos.trunc() as u64)
}

/// Converts microseconds (μs) to nanoseconds (ns), panicking on invalid input.
///
/// # Panics
///
/// Panics if [`micros_to_nanos`] would return an error for `micros`.
#[must_use]
pub fn micros_to_nanos_unchecked(micros: f64) -> u64 {
    micros_to_nanos(micros).expect("micros_to_nanos_unchecked: invalid or overflowing input")
}

/// Converts nanoseconds (ns) to seconds.
///
/// Casting u64 to f64 may lose precision for large values,
/// but is acceptable when computing fractional seconds.
#[expect(
    clippy::cast_precision_loss,
    reason = "Precision loss acceptable for time conversion"
)]
#[must_use]
pub fn nanos_to_secs(nanos: u64) -> f64 {
    let seconds = nanos / NANOSECONDS_IN_SECOND;
    let rem_nanos = nanos % NANOSECONDS_IN_SECOND;
    (seconds as f64) + (rem_nanos as f64) / (NANOSECONDS_IN_SECOND as f64)
}

/// Converts nanoseconds (ns) to milliseconds (ms).
#[must_use]
pub const fn nanos_to_millis(nanos: u64) -> u64 {
    nanos / NANOSECONDS_IN_MILLISECOND
}

/// Converts nanoseconds (ns) to microseconds (μs).
#[must_use]
pub const fn nanos_to_micros(nanos: u64) -> u64 {
    nanos / NANOSECONDS_IN_MICROSECOND
}

/// Converts a UNIX nanoseconds timestamp to an ISO 8601 (RFC 3339) format string.
///
/// Returns the raw nanosecond value as a string if it exceeds the representable
/// datetime range (`i64::MAX`, approximately year 2262).
#[inline]
#[must_use]
pub fn unix_nanos_to_iso8601(unix_nanos: UnixNanos) -> String {
    match unix_nanos_to_datetime(unix_nanos) {
        Ok(dt) => dt.to_rfc3339_opts(SecondsFormat::Nanos, true),
        Err(_) => unix_nanos.as_u64().to_string(),
    }
}

/// Converts an ISO 8601 (RFC 3339) format string to UNIX nanoseconds timestamp.
///
/// This function accepts various ISO 8601 formats including:
/// - Full RFC 3339 with nanosecond precision: "2024-02-10T14:58:43.456789Z"
/// - RFC 3339 without fractional seconds: "2024-02-10T14:58:43Z"
/// - Simple date format: "2024-02-10" (interpreted as midnight UTC)
///
/// # Parameters
///
/// - `date_string`: The ISO 8601 formatted date string to parse
///
/// # Returns
///
/// Returns `Ok(UnixNanos)` if the string is successfully parsed, or an error if the format
/// is invalid or the timestamp is out of range.
///
/// # Errors
///
/// Returns an error if:
/// - The string format is not a valid ISO 8601 format
/// - The timestamp is out of range for `UnixNanos`
/// - The date/time values are invalid
#[inline]
pub fn iso8601_to_unix_nanos(date_string: &str) -> anyhow::Result<UnixNanos> {
    date_string
        .parse::<UnixNanos>()
        .map_err(|e| anyhow::anyhow!("Failed to parse ISO 8601 string '{date_string}': {e}"))
}

/// Converts a UNIX nanoseconds timestamp to an ISO 8601 (RFC 3339) format string
/// with millisecond precision.
///
/// Returns the raw nanosecond value as a string if it exceeds the representable
/// datetime range (`i64::MAX`, approximately year 2262).
#[inline]
#[must_use]
pub fn unix_nanos_to_iso8601_millis(unix_nanos: UnixNanos) -> String {
    match unix_nanos_to_datetime(unix_nanos) {
        Ok(dt) => dt.to_rfc3339_opts(SecondsFormat::Millis, true),
        Err(_) => unix_nanos.as_u64().to_string(),
    }
}

/// Floor the given UNIX nanoseconds to the nearest microsecond.
#[must_use]
pub const fn floor_to_nearest_microsecond(unix_nanos: u64) -> u64 {
    (unix_nanos / NANOSECONDS_IN_MICROSECOND) * NANOSECONDS_IN_MICROSECOND
}

/// Calculates the last weekday (Mon-Fri) from the given `year`, `month` and `day`.
///
/// # Errors
///
/// Returns an error if the date is invalid.
pub fn last_weekday_nanos(year: i32, month: u32, day: u32) -> anyhow::Result<UnixNanos> {
    let date =
        NaiveDate::from_ymd_opt(year, month, day).ok_or_else(|| anyhow::anyhow!("Invalid date"))?;
    let current_weekday = date.weekday().number_from_monday();

    // Calculate the offset in days for closest weekday (Mon-Fri)
    let offset = i64::from(match current_weekday {
        1..=5 => 0, // Monday to Friday, no adjustment needed
        6 => 1,     // Saturday, adjust to previous Friday
        _ => 2,     // Sunday, adjust to previous Friday
    });
    // Calculate last closest weekday
    let last_closest = date - TimeDelta::days(offset);

    // Convert to UNIX nanoseconds
    let unix_timestamp_ns = last_closest
        .and_hms_nano_opt(0, 0, 0, 0)
        .ok_or_else(|| anyhow::anyhow!("Failed `and_hms_nano_opt`"))?;

    // Convert timestamp nanos safely from i64 to u64
    let raw_ns = unix_timestamp_ns
        .and_utc()
        .timestamp_nanos_opt()
        .ok_or_else(|| anyhow::anyhow!("Failed `timestamp_nanos_opt`"))?;
    let ns_u64 =
        u64::try_from(raw_ns).map_err(|_| anyhow::anyhow!("Negative timestamp: {raw_ns}"))?;
    Ok(UnixNanos::from(ns_u64))
}

/// Check whether the given UNIX nanoseconds timestamp is within the last 24 hours.
///
/// # Errors
///
/// Returns an error if the timestamp is invalid.
pub fn is_within_last_24_hours(timestamp_ns: UnixNanos) -> anyhow::Result<bool> {
    // Use the time seam so the comparison is deterministic under
    // `simulation` + `cfg(madsim)` and we avoid a wall-clock call that
    // would otherwise bypass the DST contract.
    let timestamp_ns = timestamp_ns.as_u64();
    let now_ns = nanos_since_unix_epoch();

    // Future timestamps are not within the last 24 hours
    if timestamp_ns > now_ns {
        return Ok(false);
    }

    Ok(now_ns - timestamp_ns <= NANOSECONDS_IN_DAY)
}

/// Subtract `n` months from a chrono `DateTime<Utc>`.
///
/// # Errors
///
/// Returns an error if the resulting date would be invalid or out of range.
pub fn subtract_n_months(datetime: DateTime<Utc>, n: u32) -> anyhow::Result<DateTime<Utc>> {
    match datetime.checked_sub_months(chrono::Months::new(n)) {
        Some(result) => Ok(result),
        None => anyhow::bail!("Failed to subtract {n} months from {datetime}"),
    }
}

/// Add `n` months to a chrono `DateTime<Utc>`.
///
/// # Errors
///
/// Returns an error if the resulting date would be invalid or out of range.
pub fn add_n_months(datetime: DateTime<Utc>, n: u32) -> anyhow::Result<DateTime<Utc>> {
    match datetime.checked_add_months(chrono::Months::new(n)) {
        Some(result) => Ok(result),
        None => anyhow::bail!("Failed to add {n} months to {datetime}"),
    }
}

/// Subtract `n` months from a given UNIX nanoseconds timestamp.
///
/// # Errors
///
/// Returns an error if the resulting timestamp is out of range or invalid.
#[expect(
    clippy::cast_sign_loss,
    reason = "explicit `if timestamp < 0` guard before the cast"
)]
pub fn subtract_n_months_nanos(unix_nanos: UnixNanos, n: u32) -> anyhow::Result<UnixNanos> {
    let datetime = unix_nanos_to_datetime(unix_nanos)?;
    let result = subtract_n_months(datetime, n)?;
    let timestamp = match result.timestamp_nanos_opt() {
        Some(ts) => ts,
        None => anyhow::bail!("Timestamp out of range after subtracting {n} months"),
    };

    if timestamp < 0 {
        anyhow::bail!("Negative timestamp not allowed");
    }

    Ok(UnixNanos::from(timestamp as u64))
}

/// Add `n` months to a given UNIX nanoseconds timestamp.
///
/// # Errors
///
/// Returns an error if the resulting timestamp is out of range or invalid.
#[expect(
    clippy::cast_sign_loss,
    reason = "explicit `if timestamp < 0` guard before the cast"
)]
pub fn add_n_months_nanos(unix_nanos: UnixNanos, n: u32) -> anyhow::Result<UnixNanos> {
    let datetime = unix_nanos_to_datetime(unix_nanos)?;
    let result = add_n_months(datetime, n)?;
    let timestamp = match result.timestamp_nanos_opt() {
        Some(ts) => ts,
        None => anyhow::bail!("Timestamp out of range after adding {n} months"),
    };

    if timestamp < 0 {
        anyhow::bail!("Negative timestamp not allowed");
    }

    Ok(UnixNanos::from(timestamp as u64))
}

/// Add `n` years to a chrono `DateTime<Utc>`.
///
/// # Errors
///
/// Returns an error if the resulting date would be invalid or out of range.
pub fn add_n_years(datetime: DateTime<Utc>, n: u32) -> anyhow::Result<DateTime<Utc>> {
    let months = n.checked_mul(12).ok_or_else(|| {
        anyhow::anyhow!("Failed to add {n} years to {datetime}: month count overflow")
    })?;

    match datetime.checked_add_months(chrono::Months::new(months)) {
        Some(result) => Ok(result),
        None => anyhow::bail!("Failed to add {n} years to {datetime}"),
    }
}

/// Subtract `n` years from a chrono `DateTime<Utc>`.
///
/// # Errors
///
/// Returns an error if the resulting date would be invalid or out of range.
pub fn subtract_n_years(datetime: DateTime<Utc>, n: u32) -> anyhow::Result<DateTime<Utc>> {
    let months = n.checked_mul(12).ok_or_else(|| {
        anyhow::anyhow!("Failed to subtract {n} years from {datetime}: month count overflow")
    })?;

    match datetime.checked_sub_months(chrono::Months::new(months)) {
        Some(result) => Ok(result),
        None => anyhow::bail!("Failed to subtract {n} years from {datetime}"),
    }
}

/// Add `n` years to a given UNIX nanoseconds timestamp.
///
/// # Errors
///
/// Returns an error if the resulting timestamp is out of range or invalid.
#[expect(
    clippy::cast_sign_loss,
    reason = "explicit `if timestamp < 0` guard before the cast"
)]
pub fn add_n_years_nanos(unix_nanos: UnixNanos, n: u32) -> anyhow::Result<UnixNanos> {
    let datetime = unix_nanos_to_datetime(unix_nanos)?;
    let result = add_n_years(datetime, n)?;
    let timestamp = match result.timestamp_nanos_opt() {
        Some(ts) => ts,
        None => anyhow::bail!("Timestamp out of range after adding {n} years"),
    };

    if timestamp < 0 {
        anyhow::bail!("Negative timestamp not allowed");
    }

    Ok(UnixNanos::from(timestamp as u64))
}

/// Subtract `n` years from a given UNIX nanoseconds timestamp.
///
/// # Errors
///
/// Returns an error if the resulting timestamp is out of range or invalid.
#[expect(
    clippy::cast_sign_loss,
    reason = "explicit `if timestamp < 0` guard before the cast"
)]
pub fn subtract_n_years_nanos(unix_nanos: UnixNanos, n: u32) -> anyhow::Result<UnixNanos> {
    let datetime = unix_nanos_to_datetime(unix_nanos)?;
    let result = subtract_n_years(datetime, n)?;
    let timestamp = match result.timestamp_nanos_opt() {
        Some(ts) => ts,
        None => anyhow::bail!("Timestamp out of range after subtracting {n} years"),
    };

    if timestamp < 0 {
        anyhow::bail!("Negative timestamp not allowed");
    }

    Ok(UnixNanos::from(timestamp as u64))
}

/// Returns the last valid day of `(year, month)`.
///
/// Returns `None` if `month` is not in the range 1..=12.
#[must_use]
pub const fn last_day_of_month(year: i32, month: u32) -> Option<u32> {
    // Validate month range 1-12
    if month < 1 || month > 12 {
        return None;
    }

    // February leap-year logic
    Some(match month {
        2 => {
            if is_leap_year(year) {
                29
            } else {
                28
            }
        }
        4 | 6 | 9 | 11 => 30,
        _ => 31, // January, March, May, July, August, October, December
    })
}

/// Basic leap-year check
#[must_use]
pub const fn is_leap_year(year: i32) -> bool {
    (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)
}

/// Convert optional `DateTime` to optional `UnixNanos` timestamp.
pub fn datetime_to_unix_nanos(value: Option<DateTime<Utc>>) -> Option<UnixNanos> {
    value
        .and_then(|dt| dt.timestamp_nanos_opt())
        .and_then(|nanos| u64::try_from(nanos).ok())
        .map(UnixNanos::from)
}

#[cfg(test)]
#[expect(
    clippy::float_cmp,
    reason = "Exact float comparisons acceptable in tests"
)]
mod tests {
    use chrono::{DateTime, TimeDelta, TimeZone, Timelike, Utc};
    use rstest::rstest;

    use super::*;

    #[rstest]
    #[case(0.0, 0)]
    #[case(1.0, 1_000_000_000)]
    #[case(1.1, 1_100_000_000)]
    #[case(42.0, 42_000_000_000)]
    #[case(0.000_123_5, 123_500)]
    #[case(0.000_000_01, 10)]
    #[case(0.000_000_001, 1)]
    #[case(9.999_999_999, 9_999_999_999)]
    fn test_secs_to_nanos(#[case] value: f64, #[case] expected: u64) {
        let result = secs_to_nanos(value).unwrap();
        assert_eq!(result, expected);
    }

    #[rstest]
    #[case(0.0, 0)]
    #[case(1.0, 1_000)]
    #[case(1.1, 1_100)]
    #[case(42.0, 42_000)]
    #[case(0.012_34, 12)]
    #[case(0.001, 1)]
    fn test_secs_to_millis(#[case] value: f64, #[case] expected: u64) {
        let result = secs_to_millis(value).unwrap();
        assert_eq!(result, expected);
    }

    #[rstest]
    fn test_secs_to_nanos_unchecked_matches_checked() {
        assert_eq!(secs_to_nanos_unchecked(1.1), secs_to_nanos(1.1).unwrap());
    }

    #[rstest]
    fn test_secs_to_nanos_non_finite_errors() {
        let err = secs_to_nanos(f64::NAN).unwrap_err();
        assert!(err.to_string().contains("finite"));
    }

    #[rstest]
    fn test_secs_to_nanos_overflow_errors() {
        let err = secs_to_nanos(MAX_SECS_FOR_NANOS + 1.0).unwrap_err();
        assert!(err.to_string().contains("exceeds"));
    }

    #[rstest]
    fn test_secs_to_millis_non_finite_errors() {
        let err = secs_to_millis(f64::INFINITY).unwrap_err();
        assert!(err.to_string().contains("finite"));
    }

    #[rstest]
    fn test_millis_to_nanos_overflow_errors() {
        let err = millis_to_nanos(MAX_MILLIS_FOR_NANOS + 1.0).unwrap_err();
        assert!(err.to_string().contains("exceeds"));
    }

    #[rstest]
    fn test_millis_to_nanos_non_finite_errors() {
        let err = millis_to_nanos(f64::NEG_INFINITY).unwrap_err();
        assert!(err.to_string().contains("finite"));
    }

    #[rstest]
    fn test_micros_to_nanos_non_finite_errors() {
        let err = micros_to_nanos(f64::NAN).unwrap_err();
        assert!(err.to_string().contains("finite"));
    }

    #[rstest]
    #[case(0, 0)]
    #[case(1, 60)]
    #[case(5, 300)]
    #[case(60, 3600)]
    #[case(1440, 86400)]
    fn test_mins_to_secs(#[case] mins: u64, #[case] expected: u64) {
        assert_eq!(mins_to_secs(mins), expected);
    }

    #[rstest]
    #[case(0, 0)]
    #[case(1, 60_000_000_000)]
    #[case(5, 300_000_000_000)]
    #[case(60, 3_600_000_000_000)]
    fn test_mins_to_nanos(#[case] mins: u64, #[case] expected: u64) {
        assert_eq!(mins_to_nanos(mins), expected);
    }

    #[rstest]
    fn test_micros_to_nanos_overflow_errors() {
        // Use * 2.0 because + 1.0 doesn't change MAX_MICROS_FOR_NANOS due to f64 precision
        let err = micros_to_nanos(MAX_MICROS_FOR_NANOS * 2.0).unwrap_err();
        assert!(err.to_string().contains("exceeds"));
    }

    #[rstest]
    fn test_secs_to_nanos_negative_infinity_errors() {
        let result = secs_to_nanos(f64::NEG_INFINITY);
        assert!(result.is_err());
    }

    #[rstest]
    #[case(2024, 0)] // Month below range
    #[case(2024, 13)] // Month above range
    fn test_last_day_of_month_invalid_month(#[case] year: i32, #[case] month: u32) {
        assert!(last_day_of_month(year, month).is_none());
    }

    #[rstest]
    #[case(0.0, 0)]
    #[case(1.0, 1_000_000)]
    #[case(1.1, 1_100_000)]
    #[case(42.0, 42_000_000)]
    #[case(0.000_123_4, 123)]
    #[case(0.000_01, 10)]
    #[case(0.000_001, 1)]
    #[case(9.999_999, 9_999_999)]
    fn test_millis_to_nanos(#[case] value: f64, #[case] expected: u64) {
        let result = millis_to_nanos(value).unwrap();
        assert_eq!(result, expected);
    }

    #[rstest]
    fn test_millis_to_nanos_unchecked_matches_checked() {
        assert_eq!(
            millis_to_nanos_unchecked(1.1),
            millis_to_nanos(1.1).unwrap()
        );
    }

    #[rstest]
    #[case(0.0, 0)]
    #[case(1.0, 1_000)]
    #[case(1.1, 1_100)]
    #[case(42.0, 42_000)]
    #[case(0.1234, 123)]
    #[case(0.01, 10)]
    #[case(0.001, 1)]
    #[case(9.999, 9_999)]
    fn test_micros_to_nanos(#[case] value: f64, #[case] expected: u64) {
        let result = micros_to_nanos(value).unwrap();
        assert_eq!(result, expected);
    }

    #[rstest]
    fn test_micros_to_nanos_unchecked_matches_checked() {
        assert_eq!(
            micros_to_nanos_unchecked(1.1),
            micros_to_nanos(1.1).unwrap()
        );
    }

    #[rstest]
    #[case(0, 0.0)]
    #[case(1, 1e-09)]
    #[case(1_000_000_000, 1.0)]
    #[case(42_897_123_111, 42.897_123_111)]
    fn test_nanos_to_secs(#[case] value: u64, #[case] expected: f64) {
        let result = nanos_to_secs(value);
        assert_eq!(result, expected);
    }

    #[rstest]
    #[case(0, 0)]
    #[case(1_000_000, 1)]
    #[case(1_000_000_000, 1000)]
    #[case(42_897_123_111, 42897)]
    fn test_nanos_to_millis(#[case] value: u64, #[case] expected: u64) {
        let result = nanos_to_millis(value);
        assert_eq!(result, expected);
    }

    #[rstest]
    #[case(0, 0)]
    #[case(1_000, 1)]
    #[case(1_000_000_000, 1_000_000)]
    #[case(42_897_123, 42_897)]
    fn test_nanos_to_micros(#[case] value: u64, #[case] expected: u64) {
        let result = nanos_to_micros(value);
        assert_eq!(result, expected);
    }

    #[rstest]
    #[case(0, "1970-01-01T00:00:00.000000000Z")] // Unix epoch
    #[case(1, "1970-01-01T00:00:00.000000001Z")] // 1 nanosecond
    #[case(1_000, "1970-01-01T00:00:00.000001000Z")] // 1 microsecond
    #[case(1_000_000, "1970-01-01T00:00:00.001000000Z")] // 1 millisecond
    #[case(1_000_000_000, "1970-01-01T00:00:01.000000000Z")] // 1 second
    #[case(1_702_857_600_000_000_000, "2023-12-18T00:00:00.000000000Z")] // Specific date
    fn test_unix_nanos_to_iso8601(#[case] nanos: u64, #[case] expected: &str) {
        let result = unix_nanos_to_iso8601(UnixNanos::from(nanos));
        assert_eq!(result, expected);
    }

    #[rstest]
    #[case(0, "1970-01-01T00:00:00.000Z")] // Unix epoch
    #[case(1_000_000, "1970-01-01T00:00:00.001Z")] // 1 millisecond
    #[case(1_000_000_000, "1970-01-01T00:00:01.000Z")] // 1 second
    #[case(1_702_857_600_123_456_789, "2023-12-18T00:00:00.123Z")] // With millisecond precision
    fn test_unix_nanos_to_iso8601_millis(#[case] nanos: u64, #[case] expected: &str) {
        let result = unix_nanos_to_iso8601_millis(UnixNanos::from(nanos));
        assert_eq!(result, expected);
    }

    #[rstest]
    #[case(2023, 12, 15, 1_702_598_400_000_000_000)] // Fri
    #[case(2023, 12, 16, 1_702_598_400_000_000_000)] // Sat
    #[case(2023, 12, 17, 1_702_598_400_000_000_000)] // Sun
    #[case(2023, 12, 18, 1_702_857_600_000_000_000)] // Mon
    fn test_last_closest_weekday_nanos_with_valid_date(
        #[case] year: i32,
        #[case] month: u32,
        #[case] day: u32,
        #[case] expected: u64,
    ) {
        let result = last_weekday_nanos(year, month, day).unwrap().as_u64();
        assert_eq!(result, expected);
    }

    #[rstest]
    fn test_last_closest_weekday_nanos_with_invalid_date() {
        let result = last_weekday_nanos(2023, 4, 31);
        assert!(result.is_err());
    }

    #[rstest]
    fn test_last_closest_weekday_nanos_with_nonexistent_date() {
        let result = last_weekday_nanos(2023, 2, 30);
        assert!(result.is_err());
    }

    #[rstest]
    fn test_last_closest_weekday_nanos_with_invalid_conversion() {
        let result = last_weekday_nanos(9999, 12, 31);
        assert!(result.is_err());
    }

    #[rstest]
    fn test_is_within_last_24_hours_when_now() {
        let now_ns = Utc::now().timestamp_nanos_opt().unwrap();
        assert!(is_within_last_24_hours(UnixNanos::from(now_ns.cast_unsigned())).unwrap());
    }

    #[rstest]
    fn test_is_within_last_24_hours_when_two_days_ago() {
        let past_ns = (Utc::now() - TimeDelta::try_days(2).unwrap())
            .timestamp_nanos_opt()
            .unwrap();
        assert!(!is_within_last_24_hours(UnixNanos::from(past_ns.cast_unsigned())).unwrap());
    }

    #[rstest]
    fn test_is_within_last_24_hours_when_future() {
        // Future timestamps should return false
        let future_ns = (Utc::now() + TimeDelta::try_hours(1).unwrap())
            .timestamp_nanos_opt()
            .unwrap();
        assert!(!is_within_last_24_hours(UnixNanos::from(future_ns.cast_unsigned())).unwrap());

        // One day in the future should also return false
        let future_ns = (Utc::now() + TimeDelta::try_days(1).unwrap())
            .timestamp_nanos_opt()
            .unwrap();
        assert!(!is_within_last_24_hours(UnixNanos::from(future_ns.cast_unsigned())).unwrap());
    }

    #[rstest]
    #[case(Utc.with_ymd_and_hms(2024, 3, 31, 12, 0, 0).unwrap(), 1, Utc.with_ymd_and_hms(2024, 2, 29, 12, 0, 0).unwrap())] // Leap year February
    #[case(Utc.with_ymd_and_hms(2024, 3, 31, 12, 0, 0).unwrap(), 12, Utc.with_ymd_and_hms(2023, 3, 31, 12, 0, 0).unwrap())] // One year earlier
    #[case(Utc.with_ymd_and_hms(2024, 1, 31, 12, 0, 0).unwrap(), 1, Utc.with_ymd_and_hms(2023, 12, 31, 12, 0, 0).unwrap())] // Wrapping to previous year
    #[case(Utc.with_ymd_and_hms(2024, 3, 31, 12, 0, 0).unwrap(), 2, Utc.with_ymd_and_hms(2024, 1, 31, 12, 0, 0).unwrap())] // Multiple months back
    fn test_subtract_n_months(
        #[case] input: DateTime<Utc>,
        #[case] months: u32,
        #[case] expected: DateTime<Utc>,
    ) {
        let result = subtract_n_months(input, months).unwrap();
        assert_eq!(result, expected);
    }

    #[rstest]
    #[case(Utc.with_ymd_and_hms(2023, 2, 28, 12, 0, 0).unwrap(), 1, Utc.with_ymd_and_hms(2023, 3, 28, 12, 0, 0).unwrap())] // Simple month addition
    #[case(Utc.with_ymd_and_hms(2024, 1, 31, 12, 0, 0).unwrap(), 1, Utc.with_ymd_and_hms(2024, 2, 29, 12, 0, 0).unwrap())] // Leap year February
    #[case(Utc.with_ymd_and_hms(2023, 12, 31, 12, 0, 0).unwrap(), 1, Utc.with_ymd_and_hms(2024, 1, 31, 12, 0, 0).unwrap())] // Wrapping to next year
    #[case(Utc.with_ymd_and_hms(2023, 1, 31, 12, 0, 0).unwrap(), 13, Utc.with_ymd_and_hms(2024, 2, 29, 12, 0, 0).unwrap())] // Crossing year boundary with multiple months
    fn test_add_n_months(
        #[case] input: DateTime<Utc>,
        #[case] months: u32,
        #[case] expected: DateTime<Utc>,
    ) {
        let result = add_n_months(input, months).unwrap();
        assert_eq!(result, expected);
    }

    #[rstest]
    fn test_add_n_years_overflow() {
        let datetime = Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap();
        let err = add_n_years(datetime, u32::MAX).unwrap_err();
        assert!(err.to_string().contains("month count overflow"));
    }

    #[rstest]
    fn test_subtract_n_years_overflow() {
        let datetime = Utc.with_ymd_and_hms(2024, 1, 1, 0, 0, 0).unwrap();
        let err = subtract_n_years(datetime, u32::MAX).unwrap_err();
        assert!(err.to_string().contains("month count overflow"));
    }

    #[rstest]
    fn test_add_n_years_nanos_overflow() {
        let nanos = UnixNanos::from(0);
        let err = add_n_years_nanos(nanos, u32::MAX).unwrap_err();
        assert!(err.to_string().contains("month count overflow"));
    }

    #[rstest]
    #[case(2024, 2, 29)] // Leap year February
    #[case(2023, 2, 28)] // Non-leap year February
    #[case(2024, 12, 31)] // December
    #[case(2023, 11, 30)] // November
    fn test_last_day_of_month(#[case] year: i32, #[case] month: u32, #[case] expected: u32) {
        let result = last_day_of_month(year, month).unwrap();
        assert_eq!(result, expected);
    }

    #[rstest]
    #[case(2024, true)] // Leap year divisible by 4
    #[case(1900, false)] // Not leap year, divisible by 100 but not 400
    #[case(2000, true)] // Leap year, divisible by 400
    #[case(2023, false)] // Non-leap year
    fn test_is_leap_year(#[case] year: i32, #[case] expected: bool) {
        let result = is_leap_year(year);
        assert_eq!(result, expected);
    }

    #[rstest]
    #[case("1970-01-01T00:00:00.000000000Z", 0)] // Unix epoch
    #[case("1970-01-01T00:00:00.000000001Z", 1)] // 1 nanosecond
    #[case("1970-01-01T00:00:00.001000000Z", 1_000_000)] // 1 millisecond
    #[case("1970-01-01T00:00:01.000000000Z", 1_000_000_000)] // 1 second
    #[case("2023-12-18T00:00:00.000000000Z", 1_702_857_600_000_000_000)] // Specific date
    #[case("2024-02-10T14:58:43.456789Z", 1_707_577_123_456_789_000)] // RFC3339 with fractions
    #[case("2024-02-10T14:58:43Z", 1_707_577_123_000_000_000)] // RFC3339 without fractions
    #[case("2024-02-10", 1_707_523_200_000_000_000)] // Simple date format
    fn test_iso8601_to_unix_nanos(#[case] input: &str, #[case] expected: u64) {
        let result = iso8601_to_unix_nanos(input).unwrap();
        assert_eq!(result.as_u64(), expected);
    }

    #[rstest]
    #[case("invalid-date")] // Invalid format
    #[case("2024-02-30")] // Invalid date
    #[case("2024-13-01")] // Invalid month
    #[case("not a timestamp")] // Random string
    fn test_iso8601_to_unix_nanos_invalid(#[case] input: &str) {
        let result = iso8601_to_unix_nanos(input);
        assert!(result.is_err());
    }

    #[rstest]
    fn test_iso8601_roundtrip() {
        let original_nanos = UnixNanos::from(1_707_577_123_456_789_000);
        let iso8601_string = unix_nanos_to_iso8601(original_nanos);
        let parsed_nanos = iso8601_to_unix_nanos(&iso8601_string).unwrap();
        assert_eq!(parsed_nanos, original_nanos);
    }

    #[rstest]
    fn test_add_n_years_nanos_normal_case() {
        // Test adding 1 year from 2020-01-01
        let start = UnixNanos::from(Utc.with_ymd_and_hms(2020, 1, 1, 0, 0, 0).unwrap());
        let result = add_n_years_nanos(start, 1).unwrap();
        let expected = UnixNanos::from(Utc.with_ymd_and_hms(2021, 1, 1, 0, 0, 0).unwrap());
        assert_eq!(result, expected);
    }

    #[rstest]
    fn test_add_n_years_nanos_prevents_negative_timestamp() {
        // Edge case: ensure we catch if somehow a negative timestamp would be produced
        // This is a defensive check - in practice, adding years shouldn't produce negative
        // timestamps from valid UnixNanos, but we verify the check is in place
        let start = UnixNanos::from(0); // Epoch
        // Adding years to epoch should never produce negative, but the check is there
        let result = add_n_years_nanos(start, 1);
        assert!(result.is_ok());
    }

    #[rstest]
    fn test_datetime_to_unix_nanos_at_epoch() {
        // Unix epoch (1970-01-01 00:00:00 UTC) should return 0 nanoseconds
        let epoch = Utc.timestamp_opt(0, 0).unwrap();
        let result = datetime_to_unix_nanos(Some(epoch));
        assert_eq!(result, Some(UnixNanos::from(0)));
    }

    #[rstest]
    fn test_datetime_to_unix_nanos_typical_datetime() {
        let dt = Utc
            .with_ymd_and_hms(2024, 1, 15, 13, 30, 45)
            .unwrap()
            .with_nanosecond(123_456_789)
            .unwrap();
        let result = datetime_to_unix_nanos(Some(dt));

        // Expected: 1705325445123456789 nanoseconds
        assert!(result.is_some());
        assert_eq!(result.unwrap().as_u64(), 1_705_325_445_123_456_789);
    }

    #[rstest]
    fn test_datetime_to_unix_nanos_before_epoch() {
        // Pre-epoch datetime (1969-12-31 23:59:59 UTC) should return None
        // because negative timestamps can't be converted to u64
        let before_epoch = Utc.with_ymd_and_hms(1969, 12, 31, 23, 59, 59).unwrap();
        let result = datetime_to_unix_nanos(Some(before_epoch));
        assert_eq!(result, None);
    }

    #[rstest]
    fn test_datetime_to_unix_nanos_one_second_after_epoch() {
        // 1970-01-01 00:00:01 UTC = 1_000_000_000 nanoseconds
        let dt = Utc.timestamp_opt(1, 0).unwrap();
        let result = datetime_to_unix_nanos(Some(dt));
        assert_eq!(result, Some(UnixNanos::from(1_000_000_000)));
    }

    #[rstest]
    fn test_datetime_to_unix_nanos_with_subsecond_precision() {
        // Test with microseconds: 1970-01-01 00:00:00.000001 UTC
        let dt = Utc.timestamp_opt(0, 1_000).unwrap(); // 1 microsecond = 1000 nanos
        let result = datetime_to_unix_nanos(Some(dt));
        assert_eq!(result, Some(UnixNanos::from(1_000)));
    }

    #[rstest]
    fn test_nanos_helpers_return_err_for_values_above_i64_max() {
        let large = UnixNanos::from(u64::MAX);
        assert!(subtract_n_months_nanos(large, 1).is_err());
        assert!(add_n_months_nanos(large, 1).is_err());
        assert!(add_n_years_nanos(large, 1).is_err());
        assert!(subtract_n_years_nanos(large, 1).is_err());
    }
}