utc-dt 0.3.1

Simple, fast and small UTC date, timestamp and datetime library for Rust.
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
//! Time module.
//!
//! Implements core time concepts via UTC Timestamps, UTC Days and UTC Time-of-Days.

use crate::constants::*;
use crate::util::StrWriter;
use core::error::Error;
use core::fmt::{Display, Formatter, Write};
use core::num::ParseIntError;
use core::ops::*;
use core::time::Duration;

#[cfg(feature = "alloc")]
use alloc::{format, string::String};

#[cfg(feature = "std")]
use std::time::{SystemTime, SystemTimeError};

/// UTC Timestamp.
///
/// A UTC Timestamp is a Duration since the Unix Epoch.
///
/// ## Examples
#[cfg_attr(not(feature = "std"), doc = "```rust,ignore")]
#[cfg_attr(feature = "std", doc = "```rust")]
/// use core::time::Duration;
///
/// use utc_dt::time::{
///     UTCTimestamp,
///     UTCDay,
///     UTCTimeOfDay,
/// };
///
/// // An example duration.
/// // When a duration is used, it is assumed to be relative to the unix epoch.
/// // Thursday, 15 June 2023 10:18:08.903
/// let example_duration = Duration::from_millis(1686824288903);
///
/// // UTC Timestamp from a duration
/// let utc_timestamp = UTCTimestamp::from(example_duration); // OR
/// let utc_timestamp = UTCTimestamp::from_duration(example_duration);
/// // UTC timestamp from the local system time.
/// // Not available for #![no_std]
/// let utc_timestamp = UTCTimestamp::try_from_system_time().unwrap();
/// // UTC Timestamp from a time measurement (for secs, millis, micros, nanos)
/// let utc_timestamp = UTCTimestamp::from_millis(1686824288903);
/// // Use UTC Timestamp to get a time measurement since the epoch (for secs, millis, micros, nanos)
/// let utc_millis = utc_timestamp.as_millis();
/// // Use UTC Timestamp to get time-of-day
/// let utc_tod: UTCTimeOfDay = utc_timestamp.as_tod();
/// // Use UTC Timestamp to get days since epoch (ie. UTC Day)
/// let utc_day: UTCDay = utc_timestamp.as_day();
/// // UTC Timestamp from UTC Day and time-of-day components
/// let utc_timestamp = UTCTimestamp::from_day_and_tod(utc_day, utc_tod);
/// // Manipulate UTC Timestamps with standard math operators
/// assert_eq!(utc_timestamp + utc_timestamp, utc_timestamp * 2);
/// assert_eq!(utc_timestamp - example_duration, UTCTimestamp::ZERO);
/// // Easily apply offsets of various measurements to timestamps
/// let utc_timestamp_plus_1s = utc_timestamp.saturating_add_millis(1000);
/// let utc_timestamp_minus_1s = utc_timestamp.saturating_sub_secs(1);
/// ```
///
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct UTCTimestamp(Duration);

impl UTCTimestamp {
    /// The 'Zero' UTC Timestamp
    ///
    /// Equivalent to the instant of the epoch
    pub const ZERO: UTCTimestamp = UTCTimestamp(Duration::ZERO);

    /// The maximum UTC Timestamp
    ///
    /// Equal to `November 9, 584_554_051_223`
    pub const MAX: UTCTimestamp = UTCTimestamp(Duration::MAX);

    /// Create a UTC Timestamp from UTC day
    #[inline]
    pub const fn from_day(day: UTCDay) -> Self {
        let secs = day.0 * SECONDS_PER_DAY;
        Self(Duration::from_secs(secs))
    }

    /// Create a UTC Timestamp from UTC day and time-of-day components
    #[inline]
    pub const fn from_day_and_tod(day: UTCDay, tod: UTCTimeOfDay) -> Self {
        let secs = (day.0 * SECONDS_PER_DAY).saturating_add(tod.as_secs() as u64);
        let subsec_ns = tod.as_subsec_ns();
        Self(Duration::new(secs, subsec_ns))
    }

    /// Try to create a UTC Timestamp from the local system time.
    #[cfg(feature = "std")]
    pub fn try_from_system_time() -> Result<Self, SystemTimeError> {
        let duration = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
        Ok(UTCTimestamp(duration))
    }

    /// Create UTC Timestamp from a duration.
    /// Constant evaluation alternative to `From<Duration>`.
    #[inline]
    pub const fn from_duration(d: Duration) -> Self {
        Self(d)
    }

    /// UTC Timestamp as internal Duration since the Unix Epoch.
    #[inline]
    pub const fn as_duration(&self) -> Duration {
        self.0
    }

    /// Consume UTC Timestamp into the internal Duration since the Unix Epoch.
    #[inline]
    pub const fn to_duration(self) -> Duration {
        self.0
    }

    /// Get the UTC time-of-day in nanoseconds.
    #[inline]
    pub const fn as_tod(&self) -> UTCTimeOfDay {
        let ns = ((self.0.as_secs() % SECONDS_PER_DAY) * NANOS_PER_SECOND)
            + (self.0.subsec_nanos() as u64);
        // SAFETY: nanos is within NANOS_PER_DAY
        unsafe { UTCTimeOfDay::from_nanos_unchecked(ns) }
    }

    /// Get the number of UTC days since the Unix Epoch.
    #[inline]
    pub const fn as_day(&self) -> UTCDay {
        UTCDay(self.0.as_secs() / SECONDS_PER_DAY)
    }

    /// Create UTC Timestamp from seconds since the Unix Epoch.
    #[inline]
    pub const fn from_secs(secs: u64) -> Self {
        UTCTimestamp(Duration::from_secs(secs))
    }

    /// Convert to seconds measured from the Unix Epoch.
    #[inline]
    pub const fn as_secs(&self) -> u64 {
        self.0.as_secs()
    }

    /// Create UTC Timestamp from milliseconds since the Unix Epoch.
    #[inline]
    pub const fn from_millis(millis: u64) -> Self {
        UTCTimestamp(Duration::from_millis(millis))
    }

    /// Convert to milliseconds measured from the Unix Epoch.
    #[inline]
    pub const fn as_millis(&self) -> u128 {
        self.0.as_millis()
    }

    /// Create UTC Timestamp from microseconds since the Unix Epoch.
    #[inline]
    pub const fn from_micros(micros: u64) -> Self {
        UTCTimestamp(Duration::from_micros(micros))
    }

    /// Convert to microseconds measured from the Unix Epoch.
    #[inline]
    pub const fn as_micros(&self) -> u128 {
        self.0.as_micros()
    }

    /// Create UTC Timestamp from nanoseconds since the Unix Epoch.
    #[inline]
    pub const fn from_nanos(nanos: u64) -> Self {
        UTCTimestamp(Duration::from_nanos(nanos))
    }

    /// Convert to seconds measured from the Unix Epoch.
    #[inline]
    pub const fn as_nanos(&self) -> u128 {
        self.0.as_nanos()
    }

    /// Checked `UTCTimestamp` addition. Computes `self + other`, returning [`None`]
    /// if overflow occurred.
    #[inline]
    pub const fn checked_add(self, rhs: UTCTimestamp) -> Option<UTCTimestamp> {
        match self.0.checked_add(rhs.0) {
            Some(duration) => Some(UTCTimestamp(duration)),
            None => None,
        }
    }

    /// Checked `UTCTimestamp` addition with `Duration`. Computes `self + other`, returning [`None`]
    /// if overflow occurred.
    #[inline]
    pub const fn checked_add_duration(self, rhs: Duration) -> Option<UTCTimestamp> {
        match self.0.checked_add(rhs) {
            Some(duration) => Some(UTCTimestamp(duration)),
            None => None,
        }
    }

    /// Saturating `UTCTimestamp` addition. Computes `self + other`, returning [`UTCTimestamp::MAX`]
    /// if overflow occurred.
    #[inline]
    pub const fn saturating_add(self, rhs: UTCTimestamp) -> UTCTimestamp {
        match self.checked_add(rhs) {
            Some(res) => res,
            None => UTCTimestamp::MAX,
        }
    }

    /// Saturating `UTCTimestamp` addition with `Duration`. Computes `self + other`, returning [`UTCTimestamp::MAX`]
    /// if overflow occurred.
    #[inline]
    pub const fn saturating_add_duration(self, rhs: Duration) -> UTCTimestamp {
        match self.checked_add_duration(rhs) {
            Some(res) => res,
            None => UTCTimestamp::MAX,
        }
    }

    /// Saturating `UTCTimestamp` addition with nanoseconds. Computes `self + other`, returning [`UTCTimestamp::MAX`]
    /// if overflow occurred.
    #[inline]
    pub const fn saturating_add_nanos(self, rhs: u64) -> UTCTimestamp {
        self.saturating_add(UTCTimestamp::from_nanos(rhs))
    }

    /// Saturating `UTCTimestamp` addition with microseconds. Computes `self + other`, returning [`UTCTimestamp::MAX`]
    /// if overflow occurred.
    #[inline]
    pub const fn saturating_add_micros(self, rhs: u64) -> UTCTimestamp {
        self.saturating_add(UTCTimestamp::from_micros(rhs))
    }

    /// Saturating `UTCTimestamp` addition with milliseconds. Computes `self + other`, returning [`UTCTimestamp::MAX`]
    /// if overflow occurred.
    #[inline]
    pub const fn saturating_add_millis(self, rhs: u64) -> UTCTimestamp {
        self.saturating_add(UTCTimestamp::from_millis(rhs))
    }

    /// Saturating `UTCTimestamp` addition with seconds. Computes `self + other`, returning [`UTCTimestamp::MAX`]
    /// if overflow occurred.
    #[inline]
    pub const fn saturating_add_secs(self, rhs: u64) -> UTCTimestamp {
        self.saturating_add(UTCTimestamp::from_secs(rhs))
    }

    /// Checked `UTCTimestamp` subtraction. Computes `self - other`, returning [`None`]
    /// if the result would be negative or if overflow occurred.
    #[inline]
    pub const fn checked_sub(self, rhs: UTCTimestamp) -> Option<UTCTimestamp> {
        match self.0.checked_sub(rhs.0) {
            Some(duration) => Some(UTCTimestamp(duration)),
            None => None,
        }
    }

    /// Checked `UTCTimestamp` subtraction with `Duration`. Computes `self - other`, returning [`None`]
    /// if the result would be negative or if overflow occurred.
    #[inline]
    pub const fn checked_sub_duration(self, rhs: Duration) -> Option<UTCTimestamp> {
        match self.0.checked_sub(rhs) {
            Some(duration) => Some(UTCTimestamp(duration)),
            None => None,
        }
    }

    /// Saturating `UTCTimestamp` subtraction. Computes `self - other`, returning [`UTCTimestamp::ZERO`]
    /// if the result would be negative or if overflow occurred.
    #[inline]
    pub const fn saturating_sub(self, rhs: UTCTimestamp) -> UTCTimestamp {
        match self.checked_sub(rhs) {
            Some(res) => res,
            None => UTCTimestamp::ZERO,
        }
    }

    /// Saturating `UTCTimestamp` subtraction with `Duration`. Computes `self - other`, returning [`UTCTimestamp::ZERO`]
    /// if the result would be negative or if overflow occurred.
    #[inline]
    pub const fn saturating_sub_duration(self, rhs: Duration) -> UTCTimestamp {
        match self.checked_sub_duration(rhs) {
            Some(res) => res,
            None => UTCTimestamp::ZERO,
        }
    }

    /// Saturating `UTCTimestamp` subtraction with nanoseconds. Computes `self + other`, returning [`UTCTimestamp::ZERO`]
    /// if the result would be negative or if overflow occurred.
    #[inline]
    pub const fn saturating_sub_nanos(self, rhs: u64) -> UTCTimestamp {
        self.saturating_sub(UTCTimestamp::from_nanos(rhs))
    }

    /// Saturating `UTCTimestamp` subtraction with microseconds. Computes `self + other`, returning [`UTCTimestamp::ZERO`]
    /// if the result would be negative or if overflow occurred.
    #[inline]
    pub const fn saturating_sub_micros(self, rhs: u64) -> UTCTimestamp {
        self.saturating_sub(UTCTimestamp::from_micros(rhs))
    }

    /// Saturating `UTCTimestamp` subtraction with milliseconds. Computes `self + other`, returning [`UTCTimestamp::ZERO`]
    /// if the result would be negative or if overflow occurred.
    #[inline]
    pub const fn saturating_sub_millis(self, rhs: u64) -> UTCTimestamp {
        self.saturating_sub(UTCTimestamp::from_millis(rhs))
    }

    /// Saturating `UTCTimestamp` subtraction with seconds. Computes `self + other`, returning [`UTCTimestamp::ZERO`]
    /// if the result would be negative or if overflow occurred.
    #[inline]
    pub const fn saturating_sub_secs(self, rhs: u64) -> UTCTimestamp {
        self.saturating_sub(UTCTimestamp::from_secs(rhs))
    }

    /// Checked `UTCTimestamp` multiplication. Computes `self * other`, returning
    /// [`None`] if overflow occurred.
    #[inline]
    pub const fn checked_mul(self, rhs: u32) -> Option<UTCTimestamp> {
        match self.0.checked_mul(rhs) {
            Some(duration) => Some(UTCTimestamp(duration)),
            None => None,
        }
    }

    /// Saturating `UTCTimestamp` multiplication. Computes `self * other`, returning
    /// [`UTCTimestamp::MAX`] if overflow occurred.
    #[inline]
    pub const fn saturating_mul(self, rhs: u32) -> UTCTimestamp {
        match self.checked_mul(rhs) {
            Some(res) => res,
            None => UTCTimestamp::MAX,
        }
    }

    /// Checked `UTCTimestamp` division. Computes `self / other`, returning [`None`]
    /// if `other` == 0.
    #[inline]
    pub const fn checked_div(self, rhs: u32) -> Option<UTCTimestamp> {
        match self.0.checked_div(rhs) {
            Some(duration) => Some(UTCTimestamp(duration)),
            None => None,
        }
    }
}

impl From<Duration> for UTCTimestamp {
    fn from(value: Duration) -> Self {
        Self(value)
    }
}

impl From<UTCDay> for UTCTimestamp {
    #[inline]
    fn from(day: UTCDay) -> Self {
        UTCTimestamp::from_day(day)
    }
}

impl Add for UTCTimestamp {
    type Output = UTCTimestamp;

    fn add(self, rhs: Self) -> Self::Output {
        self.checked_add(rhs)
            .expect("overflow when adding timestamps")
    }
}

impl Add<Duration> for UTCTimestamp {
    type Output = UTCTimestamp;

    fn add(self, rhs: Duration) -> Self::Output {
        self.checked_add_duration(rhs)
            .expect("overflow when adding timestamps")
    }
}

impl AddAssign for UTCTimestamp {
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs
    }
}

impl AddAssign<Duration> for UTCTimestamp {
    fn add_assign(&mut self, rhs: Duration) {
        *self = *self + rhs
    }
}

impl Sub for UTCTimestamp {
    type Output = UTCTimestamp;

    fn sub(self, rhs: Self) -> Self::Output {
        self.checked_sub(rhs)
            .expect("overflow when subtracting timestamps")
    }
}

impl Sub<Duration> for UTCTimestamp {
    type Output = UTCTimestamp;

    fn sub(self, rhs: Duration) -> Self::Output {
        self.checked_sub_duration(rhs)
            .expect("overflow when subtracting timestamps")
    }
}

impl SubAssign for UTCTimestamp {
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl SubAssign<Duration> for UTCTimestamp {
    fn sub_assign(&mut self, rhs: Duration) {
        *self = *self - rhs;
    }
}

impl Mul<u32> for UTCTimestamp {
    type Output = UTCTimestamp;

    fn mul(self, rhs: u32) -> Self::Output {
        self.checked_mul(rhs)
            .expect("overflow when multiplying timestamp by scalar")
    }
}

impl Mul<UTCTimestamp> for u32 {
    type Output = UTCTimestamp;

    fn mul(self, rhs: UTCTimestamp) -> Self::Output {
        rhs * self
    }
}

impl MulAssign<u32> for UTCTimestamp {
    fn mul_assign(&mut self, rhs: u32) {
        *self = *self * rhs
    }
}

impl Div<u32> for UTCTimestamp {
    type Output = UTCTimestamp;

    fn div(self, rhs: u32) -> Self::Output {
        self.checked_div(rhs)
            .expect("divide by zero error when dividing timestamp by scalar")
    }
}

impl DivAssign<u32> for UTCTimestamp {
    fn div_assign(&mut self, rhs: u32) {
        *self = *self / rhs
    }
}

/// Common methods for creating and converting between UTC structures.
///
/// ## Examples
#[cfg_attr(not(feature = "std"), doc = "```rust,ignore")]
#[cfg_attr(feature = "std", doc = "```rust")]
/// use core::time::Duration;
///
/// use utc_dt::UTCDatetime;
/// use utc_dt::time::{
///     UTCTimestamp,
///     UTCDay,
///     UTCTimeOfDay,
///     UTCTransformations,
/// };
/// use utc_dt::date::UTCDate;
///
/// // An example duration.
/// // When a duration is used, it is assumed to be relative to the unix epoch.
/// // Thursday, 15 June 2023 10:18:08.903
/// let example_duration = Duration::from_millis(1686824288903);
/// // UTC Timestamp from a duration
/// let utc_timestamp = UTCTimestamp::from(example_duration);
///
/// // Example shortcuts using `UTCTransformations`
/// // UTC Day / UTC Date / UTC Datetime from a duration
/// let utc_day = UTCDay::from_duration(example_duration); // OR
/// let utc_day = UTCDay::from(example_duration);
/// let utc_date = UTCDate::from_duration(example_duration); // OR
/// let utc_date = UTCDate::from(example_duration);
/// let utc_datetime = UTCDatetime::from_duration(example_duration); // OR
/// let utc_datetime = UTCDatetime::from(example_duration);
///
/// // UTC Day / UTC Date / UTC Datetime from a timestamp
/// let utc_day = UTCDay::from_timestamp(utc_timestamp); // OR
/// let utc_day = UTCDay::from(utc_timestamp);
/// let utc_date = UTCDate::from_timestamp(utc_timestamp); // OR
/// let utc_date = UTCDate::from(utc_timestamp);
/// let utc_datetime = UTCDatetime::from_timestamp(utc_timestamp); // OR
/// let utc_datetime = UTCDatetime::from(utc_timestamp);
///
/// // UTC Day / UTC Date / UTC Datetime from local system time
/// // Not available for #![no_std]
/// let utc_day = UTCDay::try_from_system_time().unwrap();
/// let utc_date = UTCDate::try_from_system_time().unwrap();
/// let utc_datetime = UTCDatetime::try_from_system_time().unwrap();
///
/// // UTC Day / UTC Date / UTC Datetime from u64 epoch measurements
/// let utc_day = UTCDay::from_secs(1686824288);
/// let utc_date = UTCDate::from_millis(1686824288_000);
/// let utc_datetime = UTCDate::from_micros(1686824288_000_000);
///
/// // Convert from UTC Day / UTC Date / UTC Datetime back to various types
/// let utc_duration: Duration = utc_day.as_duration();
/// let utc_timestamp: UTCTimestamp = utc_date.as_timestamp();
/// let utc_secs: u64 = utc_date.as_secs();
/// let utc_millis: u128 = utc_datetime.as_millis();
/// let utc_micros: u128 = utc_day.as_micros();
/// let utc_nanos: u128 = utc_date.as_nanos();
/// ```
///
pub trait UTCTransformations
where
    Self: Sized,
{
    /// Create from a duration measured from the Unix Epoch.
    #[inline]
    fn from_duration(duration: Duration) -> Self {
        let timestamp = UTCTimestamp(duration);
        Self::from_timestamp(timestamp)
    }

    /// Convert to a duration measured from the Unix Epoch.
    #[inline]
    fn as_duration(&self) -> Duration {
        self.as_timestamp().as_duration()
    }

    /// Create from seconds measured from the Unix Epoch.
    #[inline]
    fn from_secs(secs: u64) -> Self {
        let timestamp = UTCTimestamp::from_secs(secs);
        Self::from_timestamp(timestamp)
    }

    /// Convert to seconds measured from the Unix Epoch.
    #[inline]
    fn as_secs(&self) -> u64 {
        self.as_timestamp().as_secs()
    }

    /// Create from milliseconds measured from the Unix Epoch.
    #[inline]
    fn from_millis(millis: u64) -> Self {
        let timestamp = UTCTimestamp::from_millis(millis);
        Self::from_timestamp(timestamp)
    }

    /// Convert to milliseconds measured from the Unix Epoch.
    #[inline]
    fn as_millis(&self) -> u128 {
        self.as_timestamp().as_millis()
    }

    /// Create from microseconds measured from the Unix Epoch.
    #[inline]
    fn from_micros(micros: u64) -> Self {
        let timestamp = UTCTimestamp::from_micros(micros);
        Self::from_timestamp(timestamp)
    }

    /// Convert to microseconds measured from the Unix Epoch.
    #[inline]
    fn as_micros(&self) -> u128 {
        self.as_timestamp().as_micros()
    }

    /// Create from nanoseconds measured from the Unix Epoch.
    #[inline]
    fn from_nanos(nanos: u64) -> Self {
        let timestamp = UTCTimestamp::from_nanos(nanos);
        Self::from_timestamp(timestamp)
    }

    /// Convert to nanoseconds measured from the Unix Epoch.
    #[inline]
    fn as_nanos(&self) -> u128 {
        self.as_timestamp().as_nanos()
    }

    /// Create from local system time
    #[cfg(feature = "std")]
    fn try_from_system_time() -> Result<Self, SystemTimeError> {
        let timestamp = UTCTimestamp::try_from_system_time()?;
        Ok(Self::from_timestamp(timestamp))
    }

    /// Create from a UTC timestamp.
    fn from_timestamp(timestamp: UTCTimestamp) -> Self;
    /// Convert to a UTC timestamp.
    fn as_timestamp(&self) -> UTCTimestamp;
}

/// UTC Day count.
///
/// UTC Day is equal to the number of days since the Unix Epoch.
///
/// ## Examples
#[cfg_attr(not(feature = "std"), doc = "```rust,ignore")]
#[cfg_attr(feature = "std", doc = "```rust")]
/// use utc_dt::time::UTCDay;
///
/// // UTC Day from an integer
/// let utc_day = UTCDay::try_from_u64(19523).unwrap();
/// // Integer from UTC Day
/// let day_u64 = utc_day.as_u64(); // OR
/// let day_u64 = utc_day.to_u64();
/// // Use UTC Day to get the weekday
/// let weekday = utc_day.as_weekday();
/// // Manipulate UTC Days with standard math operators
/// assert_eq!(utc_day - utc_day, utc_day / u64::MAX);
/// assert_eq!(utc_day + 19523, utc_day * 2);
/// ```
///
/// ## Safety
/// Unchecked methods are provided for use in hot paths requiring high levels of optimisation.
/// These methods assume valid input.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct UTCDay(u64);

impl UTCDay {
    /// The zero UTC day value.
    ///
    /// Equal to the epoch day.
    pub const ZERO: Self = Self(0);

    /// The maximum time of day value.
    ///
    /// Maximum day support is limited by the maximum `UTCTimestamp`.
    pub const MAX: Self = Self(213_503_982_334_601);

    /// Create UTC Day from integer
    ///
    /// ## Safety
    /// Unsafe if the user passes an unsupported count of UTC days, exceeding `UTCDay::MAX`.
    #[inline]
    pub const unsafe fn from_u64_unchecked(u: u64) -> Self {
        Self(u)
    }

    /// Try create UTC Day from integer.
    pub fn try_from_u64(u: u64) -> Result<Self, UTCDayErrOutOfRange> {
        let day = unsafe { Self::from_u64_unchecked(u) };
        if day > Self::MAX {
            return Err(UTCDayErrOutOfRange(day.0));
        }
        Ok(day)
    }

    /// UTC Day as internal integer
    #[inline]
    pub const fn as_u64(&self) -> u64 {
        self.0
    }

    /// Consume UTC Day to internal integer
    #[inline]
    pub const fn to_u64(self) -> u64 {
        self.0
    }

    /// Calculate and return the day of the week in numerical form
    /// `[0, 6]` represents `[Sun, Sat]`
    ///
    /// Reference:
    /// <http://howardhinnant.github.io/date_algorithms.html#weekday_from_days>
    pub fn as_weekday(&self) -> u8 {
        ((self.0 + 4) % 7) as u8
    }

    /// Checked `UTCDay` addition. Computes `self + other`, returning [`None`]
    /// if overflow occurred.
    #[inline]
    pub fn checked_add(self, rhs: UTCDay) -> Option<UTCDay> {
        self.0
            .checked_add(rhs.0)
            .map(|u| UTCDay(u).min(UTCDay::MAX))
    }

    /// Checked `UTCDay` addition with `u64`. Computes `self + other`, returning [`None`]
    /// if overflow occurred.
    #[inline]
    pub fn checked_add_u64(self, rhs: u64) -> Option<UTCDay> {
        self.0.checked_add(rhs).map(|u| UTCDay(u).min(UTCDay::MAX))
    }

    /// Saturating `UTCDay` addition. Computes `self + other`, returning [`UTCDay::MAX`]
    /// if overflow occurred.
    #[inline]
    pub fn saturating_add(self, rhs: UTCDay) -> UTCDay {
        match self.checked_add(rhs) {
            Some(res) => res,
            None => UTCDay::MAX,
        }
    }

    /// Saturating `UTCDay` addition with `u64`. Computes `self + other`, returning [`UTCDay::MAX`]
    /// if overflow occurred.
    #[inline]
    pub fn saturating_add_u64(self, rhs: u64) -> UTCDay {
        match self.checked_add_u64(rhs) {
            Some(res) => res,
            None => UTCDay::MAX,
        }
    }

    /// Checked `UTCDay` subtraction. Computes `self - other`, returning [`None`]
    /// if the result would be negative or if overflow occurred.
    #[inline]
    pub const fn checked_sub(self, rhs: UTCDay) -> Option<UTCDay> {
        match self.0.checked_sub(rhs.0) {
            Some(u) => Some(UTCDay(u)),
            None => None,
        }
    }

    /// Checked `UTCDay` subtraction with `u64`. Computes `self - other`, returning [`None`]
    /// if the result would be negative or if overflow occurred.
    #[inline]
    pub const fn checked_sub_u64(self, rhs: u64) -> Option<UTCDay> {
        match self.0.checked_sub(rhs) {
            Some(u) => Some(UTCDay(u)),
            None => None,
        }
    }

    /// Saturating `UTCDay` subtraction. Computes `self - other`, returning [`UTCDay::ZERO`]
    /// if the result would be negative or if overflow occurred.
    #[inline]
    pub const fn saturating_sub(self, rhs: UTCDay) -> UTCDay {
        match self.checked_sub(rhs) {
            Some(res) => res,
            None => UTCDay::ZERO,
        }
    }

    /// Saturating `UTCDay` subtraction with `u64`. Computes `self - other`, returning [`UTCDay::ZERO`]
    /// if the result would be negative or if overflow occurred.
    #[inline]
    pub const fn saturating_sub_u64(self, rhs: u64) -> UTCDay {
        match self.checked_sub_u64(rhs) {
            Some(res) => res,
            None => UTCDay::ZERO,
        }
    }

    /// Checked `UTCDay` multiplication. Computes `self * other`, returning
    /// [`None`] if overflow occurred.
    #[inline]
    pub fn checked_mul(self, rhs: u64) -> Option<UTCDay> {
        self.0.checked_mul(rhs).map(|u| UTCDay(u).min(UTCDay::MAX))
    }

    /// Saturating `UTCDay` multiplication. Computes `self * other`, returning
    /// [`UTCDay::MAX`] if overflow occurred.
    #[inline]
    pub fn saturating_mul(self, rhs: u64) -> UTCDay {
        match self.checked_mul(rhs) {
            Some(res) => res,
            None => UTCDay::MAX,
        }
    }

    /// Checked `UTCDay` division. Computes `self / other`, returning [`None`]
    /// if `other` == 0.
    #[inline]
    pub const fn checked_div(self, rhs: u64) -> Option<UTCDay> {
        match self.0.checked_div(rhs) {
            Some(u) => Some(UTCDay(u)),
            None => None,
        }
    }
}

/// Error type for UTCDay out of range
#[derive(Debug, Clone)]
pub struct UTCDayErrOutOfRange(u64);

impl Display for UTCDayErrOutOfRange {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        write!(f, "UTC day ({}) exceeding maximum", self.0)
    }
}

impl Error for UTCDayErrOutOfRange {}

impl UTCTransformations for UTCDay {
    #[inline]
    fn from_secs(secs: u64) -> Self {
        Self(secs / SECONDS_PER_DAY)
    }

    #[inline]
    fn as_secs(&self) -> u64 {
        self.0 * SECONDS_PER_DAY
    }

    #[inline]
    fn from_millis(millis: u64) -> Self {
        Self(millis / MILLIS_PER_DAY)
    }

    #[inline]
    fn as_millis(&self) -> u128 {
        self.0 as u128 * MILLIS_PER_DAY as u128
    }

    #[inline]
    fn from_micros(micros: u64) -> Self {
        Self(micros / MICROS_PER_DAY)
    }

    #[inline]
    fn as_micros(&self) -> u128 {
        self.0 as u128 * MICROS_PER_DAY as u128
    }

    #[inline]
    fn from_nanos(nanos: u64) -> Self {
        Self(nanos / NANOS_PER_DAY)
    }

    #[inline]
    fn as_nanos(&self) -> u128 {
        self.0 as u128 * NANOS_PER_DAY as u128
    }

    #[inline]
    fn from_timestamp(timestamp: UTCTimestamp) -> Self {
        timestamp.as_day()
    }

    #[inline]
    fn as_timestamp(&self) -> UTCTimestamp {
        UTCTimestamp::from_day(*self)
    }
}

impl Add for UTCDay {
    type Output = UTCDay;

    fn add(self, rhs: Self) -> Self::Output {
        self.checked_add(rhs).expect("overflow when adding days")
    }
}

impl Add<u64> for UTCDay {
    type Output = UTCDay;

    fn add(self, rhs: u64) -> Self::Output {
        self.checked_add_u64(rhs)
            .expect("overflow when adding days")
    }
}

impl AddAssign for UTCDay {
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs
    }
}

impl AddAssign<u64> for UTCDay {
    fn add_assign(&mut self, rhs: u64) {
        *self = *self + rhs
    }
}

impl Sub for UTCDay {
    type Output = UTCDay;

    fn sub(self, rhs: Self) -> Self::Output {
        self.checked_sub(rhs)
            .expect("overflow when subtracting days")
    }
}

impl Sub<u64> for UTCDay {
    type Output = UTCDay;

    fn sub(self, rhs: u64) -> Self::Output {
        self.checked_sub_u64(rhs)
            .expect("overflow when subtracting days")
    }
}

impl SubAssign for UTCDay {
    fn sub_assign(&mut self, rhs: Self) {
        *self = *self - rhs;
    }
}

impl SubAssign<u64> for UTCDay {
    fn sub_assign(&mut self, rhs: u64) {
        *self = *self - rhs;
    }
}

impl Mul<u64> for UTCDay {
    type Output = UTCDay;

    fn mul(self, rhs: u64) -> Self::Output {
        self.checked_mul(rhs)
            .expect("overflow when multiplying day by scalar")
    }
}

impl Mul<UTCDay> for u64 {
    type Output = UTCDay;

    fn mul(self, rhs: UTCDay) -> Self::Output {
        rhs * self
    }
}

impl MulAssign<u64> for UTCDay {
    fn mul_assign(&mut self, rhs: u64) {
        *self = *self * rhs
    }
}

impl Div<u64> for UTCDay {
    type Output = UTCDay;

    fn div(self, rhs: u64) -> Self::Output {
        self.checked_div(rhs)
            .expect("divide by zero error when dividing day by scalar")
    }
}

impl DivAssign<u64> for UTCDay {
    fn div_assign(&mut self, rhs: u64) {
        *self = *self / rhs
    }
}

impl TryFrom<u64> for UTCDay {
    type Error = UTCDayErrOutOfRange;

    fn try_from(value: u64) -> Result<Self, Self::Error> {
        Self::try_from_u64(value)
    }
}

impl From<Duration> for UTCDay {
    #[inline]
    fn from(duration: Duration) -> Self {
        Self::from_duration(duration)
    }
}

impl From<UTCTimestamp> for UTCDay {
    #[inline]
    fn from(timestamp: UTCTimestamp) -> Self {
        Self::from_timestamp(timestamp)
    }
}

/// UTC Time of Day
///
/// A time of day measurement with nanosecond resolution.
///
/// ## Examples
#[cfg_attr(not(feature = "std"), doc = "```rust,ignore")]
#[cfg_attr(feature = "std", doc = "```rust")]
/// use utc_dt::time::UTCTimeOfDay;
///
/// // UTC Time of Day from a time measurement (for secs, millis, micros, nanos)
/// let utc_tod = UTCTimeOfDay::try_from_millis(37088903).unwrap(); // OR
/// let utc_tod = unsafe { UTCTimeOfDay::from_millis_unchecked(37088903) };
/// // UTC Time of Day from hours, minutes, seconds and subseconds
/// let utc_tod = UTCTimeOfDay::try_from_hhmmss(10, 18, 08, 903_000_000).unwrap(); // OR
/// let utc_tod = unsafe { UTCTimeOfDay::from_hhmmss_unchecked(10, 18, 08, 903_000_000) };
/// // UTC Time of Day as a time measurement (for secs, millis, micros, nanos)
/// let utc_tod_us = utc_tod.as_micros();
/// // UTC Time of Day as hours, minutes and seconds
/// let (hrs, mins, secs) = utc_tod.as_hhmmss();
/// // UTC Time of Day subsecond component (in nanoseconds)
/// let subsec_ns = utc_tod.as_subsec_ns();
/// // Parse a UTC Time of Day from an ISO 8601 time string `(Thh:mm:ssZ)`
/// let utc_tod = UTCTimeOfDay::try_from_iso_tod("T10:18:08.903Z").unwrap();
/// // Get a time of day string formatted according to ISO 8601 `(Thh:mm:ssZ)`
/// const PRECISION_MICROS: usize = 6;
/// let iso_tod = utc_tod.as_iso_tod(PRECISION_MICROS);
/// assert_eq!(iso_tod, "T10:18:08.903000Z");
/// // Write ISO 8601 time of day str to a stack buffer
/// let mut buf = [0; UTCTimeOfDay::iso_tod_len(PRECISION_MICROS)];
/// let _bytes_written = utc_tod.write_iso_tod(&mut buf, PRECISION_MICROS).unwrap();
/// let iso_tod_str = core::str::from_utf8(&buf).unwrap();
/// assert_eq!(iso_tod_str, "T10:18:08.903000Z");
/// ```
///
/// ## Safety
/// Unchecked methods are provided for use in hot paths requiring high levels of optimisation.
/// These methods assume valid input.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct UTCTimeOfDay(u64);

impl Display for UTCTimeOfDay {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        let (hrs, mins, secs) = self.as_hhmmss();
        write!(
            f,
            "T{:02}:{:02}:{:02}.{:09}Z",
            hrs,
            mins,
            secs,
            self.as_subsec_ns()
        )
    }
}

impl UTCTimeOfDay {
    /// The zero time of day value
    pub const ZERO: Self = Self(0);

    /// The maximum time of day value.
    ///
    /// Equal to the number of nanoseconds in a day.
    pub const MAX: Self = Self(NANOS_PER_DAY - 1);

    /// The minimum length of an ISO time (in UTF8 characters)
    pub const MIN_ISO_TOD_LEN: usize = 10;

    /// The maximum supported subsecond precision of an ISO time
    pub const MAX_ISO_TOD_PRECISION: usize = 9;

    /// Unchecked method to create time of day from nanoseconds
    ///
    /// ### Safety
    /// Unsafe if the user passes an invalid time-of-day nanoseconds component (exceeding NANOS_PER_DAY).
    /// Invalid inputs are not checked and may cause a panic in other methods.
    #[inline]
    pub const unsafe fn from_nanos_unchecked(nanos: u64) -> Self {
        Self(nanos)
    }

    /// Unchecked method to create time of day from microseconds
    ///
    /// ### Safety
    /// Unsafe if the user passes an invalid time-of-day microsecond component (exceeding MICROS_PER_DAY).
    /// Invalid inputs are not checked and may cause a panic in other methods.
    #[inline]
    pub const unsafe fn from_micros_unchecked(micros: u64) -> Self {
        Self(micros * NANOS_PER_MICRO)
    }

    /// Unchecked method to create time of day from milliseconds
    ///
    /// ### Safety
    /// Unsafe if the user passes an invalid time-of-day millisecond component (exceeding MILLIS_PER_DAY).
    /// Invalid inputs are not checked and may cause a panic in other methods.
    #[inline]
    pub const unsafe fn from_millis_unchecked(millis: u32) -> Self {
        Self((millis as u64) * NANOS_PER_MILLI)
    }

    /// Unchecked method to create time of day from seconds
    ///
    /// ### Safety
    /// Unsafe if the user passes an invalid time-of-day seconds component (exceeding SECONDS_PER_DAY).
    /// Invalid inputs are not checked and may cause a panic in other methods.
    #[inline]
    pub const unsafe fn from_secs_unchecked(secs: u32) -> Self {
        Self((secs as u64) * NANOS_PER_SECOND)
    }

    const fn _ns_from_hhmmss(hrs: u8, mins: u8, secs: u8, subsec_ns: u32) -> u64 {
        (subsec_ns as u64)
            + (hrs as u64) * NANOS_PER_HOUR
            + (mins as u64) * NANOS_PER_MINUTE
            + (secs as u64) * NANOS_PER_SECOND
    }

    /// Unchecked method to create UTC time of day from hours, minutes, seconds and subsecond (nanosecond) components
    ///
    /// # Safety
    /// Unsafe if the user passes a measure of time exceeding a day.
    /// Invalid inputs are not checked and may cause a panic in other methods.
    #[inline]
    pub const unsafe fn from_hhmmss_unchecked(hrs: u8, mins: u8, secs: u8, subsec_ns: u32) -> Self {
        Self(Self::_ns_from_hhmmss(hrs, mins, secs, subsec_ns))
    }

    /// Try to create UTC time of day from nanoseconds
    pub fn try_from_nanos(nanos: u64) -> Result<Self, UTCTimeOfDayError> {
        // SAFETY: we immediately check that nanos was within NANOS_PER_DAY (tod does not exceed UTCTimeOfDay::MAX)
        let tod = unsafe { Self::from_nanos_unchecked(nanos) };
        if tod > Self::MAX {
            return Err(UTCTimeOfDayError::ExcessNanos(nanos));
        }
        Ok(tod)
    }

    /// Try to create UTC time of day from microseconds
    pub fn try_from_micros(micros: u64) -> Result<Self, UTCTimeOfDayError> {
        // SAFETY: we immediately check that micros was within MICROS_PER_DAY (tod does not exceed UTCTimeOfDay::MAX)
        let tod = unsafe { Self::from_micros_unchecked(micros) };
        if tod > Self::MAX {
            return Err(UTCTimeOfDayError::ExcessMicros(micros));
        }
        Ok(tod)
    }

    /// Try to create UTC time of day from milliseconds
    pub fn try_from_millis(millis: u32) -> Result<Self, UTCTimeOfDayError> {
        // SAFETY: we immediately check that millis was within MILLIS_PER_DAY (tod does not exceed UTCTimeOfDay::MAX)
        let tod = unsafe { Self::from_millis_unchecked(millis) };
        if tod > Self::MAX {
            return Err(UTCTimeOfDayError::ExcessMillis(millis));
        }
        Ok(tod)
    }

    /// Try to create UTC time of day from seconds
    pub fn try_from_secs(secs: u32) -> Result<Self, UTCTimeOfDayError> {
        // SAFETY: we immediately check that secs was within SECONDS_PER_DAY (tod does not exceed UTCTimeOfDay::MAX)
        let tod = unsafe { Self::from_secs_unchecked(secs) };
        if tod > Self::MAX {
            return Err(UTCTimeOfDayError::ExcessSeconds(secs));
        }
        Ok(tod)
    }

    /// Try to create UTC time of day from hours, minutes, seconds and subsecond (nanosecond) components
    ///
    /// Inputs are not limited by divisions. eg. 61 minutes is valid input, 61 seconds, etc.
    /// The time described must not exceed the number of nanoseconds in a day.
    pub fn try_from_hhmmss(
        hrs: u8,
        mins: u8,
        secs: u8,
        subsec_ns: u32,
    ) -> Result<Self, UTCTimeOfDayError> {
        Self::try_from_nanos(Self::_ns_from_hhmmss(hrs, mins, secs, subsec_ns))
    }

    /// Consume self into nanoseconds
    #[inline]
    pub const fn to_nanos(self) -> u64 {
        self.0
    }

    /// Time of day as nanoseconds
    #[inline]
    pub const fn as_nanos(&self) -> u64 {
        self.0
    }

    /// Time of day as microseconds
    #[inline]
    pub const fn as_micros(&self) -> u64 {
        self.0 / NANOS_PER_MICRO
    }

    /// Time of day as milliseconds
    #[inline]
    pub const fn as_millis(&self) -> u32 {
        (self.0 / NANOS_PER_MILLI) as u32
    }

    /// Time of day as seconds
    #[inline]
    pub const fn as_secs(&self) -> u32 {
        (self.0 / NANOS_PER_SECOND) as u32
    }

    /// Time of day as hours, minutes and seconds (hhmmss) components
    ///
    /// Returns tuple `(hrs: u8, mins: u8, secs: u8)`
    pub const fn as_hhmmss(&self) -> (u8, u8, u8) {
        let hrs = (self.0 / NANOS_PER_HOUR) as u8;
        let mins = ((self.0 % NANOS_PER_HOUR) / NANOS_PER_MINUTE) as u8;
        let secs = ((self.0 % NANOS_PER_MINUTE) / NANOS_PER_SECOND) as u8;
        (hrs, mins, secs)
    }

    /// Return subsecond component of time of day (in nanoseconds)
    #[inline]
    pub const fn as_subsec_ns(&self) -> u32 {
        (self.0 % NANOS_PER_SECOND) as u32
    }

    /// Time of day from UTC timestamp
    pub const fn from_timestamp(timestamp: UTCTimestamp) -> Self {
        timestamp.as_tod()
    }

    /// Try parse time-of-day from an ISO str in the format:
    /// * `Thh:mm:ssZ`
    /// * `Thh:mm:ss.nnnZ` (up to 9 decimal places)
    ///
    /// Conforms to ISO 8601:
    /// <https://www.w3.org/TR/NOTE-datetime>
    pub fn try_from_iso_tod(iso: &str) -> Result<Self, UTCTimeOfDayError> {
        let len = iso.len();
        if len < Self::MIN_ISO_TOD_LEN {
            return Err(UTCTimeOfDayError::InsufficientStrLen(
                len,
                Self::MIN_ISO_TOD_LEN,
            ));
        }
        let (hour_str, rem) = iso[1..].split_at(2); // remainder = ":mm:ss.nnnZ"
        let (minute_str, rem) = rem[1..].split_at(2); // remainder = ":ss.nnnZ"
        let (second_str, rem) = rem[1..].split_at(2); // remainder = ".nnnZ"
        let hrs: u8 = hour_str.parse()?;
        let mins: u8 = minute_str.parse()?;
        let secs: u8 = second_str.parse()?;
        // calculate subseconds
        let rem_len = rem.len();
        let subsec_ns: u32 = if rem_len > 1 {
            let subsec_str = &rem[1..(rem_len - 1)]; // "nnn"
            let precision: u32 = subsec_str.len() as u32;
            if precision > Self::MAX_ISO_TOD_PRECISION as u32 {
                return Err(UTCTimeOfDayError::ExcessPrecision(precision));
            }
            if precision == 0 {
                0
            } else {
                let subsec: u32 = subsec_str.parse()?;
                subsec * 10u32.pow(Self::MAX_ISO_TOD_PRECISION as u32 - precision)
            }
        } else {
            0
        };
        Self::try_from_hhmmss(hrs, mins, secs, subsec_ns)
    }

    /// Return time-of-day as a string in the format:
    /// * Precision = `0`: `Thh:mm:ssZ`
    /// * Precision = `3`: `Thh:mm:ss.nnnZ`
    ///
    /// Conforms to ISO 8601:
    /// <https://www.w3.org/TR/NOTE-datetime>
    #[cfg(feature = "alloc")]
    pub fn as_iso_tod(&self, precision: usize) -> String {
        let len = Self::iso_tod_len(precision);
        let mut s = format!("{self}");
        s.truncate(len - 1);
        s.push('Z');
        s
    }

    /// Internal truncated buffer write
    #[inline]
    pub(crate) fn _write_iso_tod_trunc(&self, w: &mut StrWriter) {
        // unwrap infallible
        write!(w, "{self}").unwrap();
        w.buf[w.written - 1] = b'Z';
    }

    /// Write time-of-day to a buffer in the format:
    /// * Precision = `0`: `Thh:mm:ssZ`
    /// * Precision = `3`: `Thh:mm:ss.nnnZ`
    ///
    /// The buffer should have a minimum length as given by [UTCTimeOfDay::iso_tod_len].
    ///
    /// A buffer of insufficient length will error ([UTCTimeOfDayError::InsufficientStrLen]).
    ///
    /// Returns number of UTF8 characters (bytes) written
    ///
    /// Conforms to ISO 8601:
    /// <https://www.w3.org/TR/NOTE-datetime>
    pub fn write_iso_tod(
        &self,
        buf: &mut [u8],
        precision: usize,
    ) -> Result<usize, UTCTimeOfDayError> {
        let write_len = Self::iso_tod_len(precision);
        if write_len > buf.len() {
            return Err(UTCTimeOfDayError::InsufficientStrLen(buf.len(), write_len));
        }
        let mut writer = StrWriter::new(&mut buf[..write_len]);
        self._write_iso_tod_trunc(&mut writer);
        Ok(writer.written)
    }

    /// Calculate the number of characters in an ISO time-of-day str
    #[inline]
    pub const fn iso_tod_len(precision: usize) -> usize {
        if precision == 0 {
            Self::MIN_ISO_TOD_LEN
        } else if precision < Self::MAX_ISO_TOD_PRECISION {
            Self::MIN_ISO_TOD_LEN + precision + 1
        } else {
            // clamp to precision to max
            Self::MIN_ISO_TOD_LEN + Self::MAX_ISO_TOD_PRECISION + 1
        }
    }
}

/// Error type for UTCTimeOfDay methods
#[derive(Debug, Clone)]
pub enum UTCTimeOfDayError {
    /// Error raised parsing int to string
    ParseErr(ParseIntError),
    /// Error raised due to excessive ISO precision
    ExcessPrecision(u32),
    /// Error raised due to nanos exceeding nanos in a day
    ExcessNanos(u64),
    /// Error raised due to micros exceeding micros in a day
    ExcessMicros(u64),
    /// Error raised due to millis exceeding millis in a day
    ExcessMillis(u32),
    /// Error raised due to seconds exceeding seconds in a day
    ExcessSeconds(u32),
    /// Error raised due to insufficient length of input ISO time-of-day str
    InsufficientStrLen(usize, usize),
}

impl Display for UTCTimeOfDayError {
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::ParseErr(e) => e.fmt(f),
            Self::ExcessPrecision(p) => write!(f, "ISO precision ({p}) exceeds maximum of 9"),
            Self::ExcessNanos(n) => write!(f, "nanoseconds ({n}) not within a day"),
            Self::ExcessMicros(u) => write!(f, "microseconds ({u}) not within a day"),
            Self::ExcessMillis(m) => write!(f, "milliseconds ({m}) not within a day"),
            Self::ExcessSeconds(s) => write!(f, "seconds ({s}) not within a day"),
            Self::InsufficientStrLen(l, m) => {
                write!(f, "insufficient ISO time str len ({l}), {m} required")
            }
        }
    }
}

impl Error for UTCTimeOfDayError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        match self {
            Self::ParseErr(e) => e.source(),
            _ => None,
        }
    }
}

impl From<ParseIntError> for UTCTimeOfDayError {
    fn from(value: ParseIntError) -> Self {
        Self::ParseErr(value)
    }
}