spacepackets 0.17.0

Generic implementations for various CCSDS and ECSS packet standards
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
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
//! Module to generate or read CCSDS Day Segmented (CDS) timestamps as specified in
//! [CCSDS 301.0-B-4](https://public.ccsds.org/Pubs/301x0b4e1.pdf) section 3.3 .
//!
//! The core data structure to do this is the [CdsTime] struct and the
//! [get_dyn_time_provider_from_bytes] function to retrieve correct instances of the
//! struct from a bytestream.
use crate::private::Sealed;
use crate::ByteConversionError;
use core::cmp::Ordering;
use core::fmt::Debug;
use core::ops::{Add, AddAssign};
use core::time::Duration;

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

#[cfg(feature = "chrono")]
use chrono::Datelike;

#[cfg(feature = "alloc")]
use super::ccsds_time_code_from_p_field;
#[cfg(feature = "alloc")]
use alloc::boxed::Box;
#[cfg(feature = "alloc")]
use core::any::Any;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

use super::{
    ccsds_to_unix_days, unix_to_ccsds_days, CcsdsTimeCode, CcsdsTimeProvider,
    DateBeforeCcsdsEpochError, TimeReader, TimeWriter, TimestampError, UnixTime, MS_PER_DAY,
    SECONDS_PER_DAY,
};

/// Base value for the preamble field for a time field parser to determine the time field type.
pub const P_FIELD_BASE: u8 = (CcsdsTimeCode::Cds as u8) << 4;
/// Minimum allowed length for a CDS timestamp.
pub const MIN_CDS_FIELD_LEN: usize = 7;
/// Maximum allowed days value for 24 bit days field.
pub const MAX_DAYS_24_BITS: u32 = 2_u32.pow(24) - 1;

/// Generic trait implemented by token structs to specify the length of day field at type
/// level. This trait is only meant to be implemented in this crate and therefore sealed.
pub trait ProvidesDaysLength: Sealed + Clone {
    /// Raw field type.
    type FieldType: Debug
        + Copy
        + Clone
        + PartialEq
        + Eq
        + TryFrom<i32>
        + TryFrom<u32>
        + TryFrom<i64>
        + From<u16>
        + Into<u32>
        + Into<i64>;
}

/// Type level token to be used as a generic parameter to [CdsTime].
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub struct DaysLen16Bits {}

impl Sealed for DaysLen16Bits {}
impl ProvidesDaysLength for DaysLen16Bits {
    type FieldType = u16;
}

/// Type level token to be used as a generic parameter to [CdsTime].
#[derive(Debug, Copy, Clone, PartialEq, Eq, Default)]
pub struct DaysLen24Bits {}
impl Sealed for DaysLen24Bits {}
impl ProvidesDaysLength for DaysLen24Bits {
    type FieldType = u32;
}

/// Length of day segment.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum LengthOfDaySegment {
    /// Shorter 16 bits length of days field.
    Short16Bits = 0,
    /// Larger 24 bits length of days field.
    Long24Bits = 1,
}

/// Sub-millisecond precision indicator.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum SubmillisPrecision {
    /// No sub-millisecond precision present.
    Absent = 0b00,
    /// Microsecond precision present.
    Microseconds = 0b01,
    /// Picoseconds precision present.
    Picoseconds = 0b10,
    /// Reserved.
    Reserved = 0b11,
}

/// CDS timestamp error.
#[derive(Debug, PartialEq, Eq, Copy, Clone, thiserror::Error)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum CdsError {
    /// CCSDS days value exceeds maximum allowed size or is negative
    #[error("invalid ccsds days {0}")]
    InvalidCcsdsDays(i64),
    /// There are distinct constructors depending on the days field width detected in the preamble
    /// field. This error will be returned if there is a missmatch.
    #[error("wrong constructor for length of day {0:?} detected in preamble")]
    InvalidCtorForDaysOfLenInPreamble(LengthOfDaySegment),
    /// Date is before the CCSDS epoch (1958-01-01T00:00:00+00:00)
    #[error("date before CCSDS epoch: {0}")]
    DateBeforeCcsdsEpoch(#[from] DateBeforeCcsdsEpochError),
}

/// Retrieve the [LengthOfDaySegment] from the p-field byte.
pub fn length_of_day_segment_from_pfield(pfield: u8) -> LengthOfDaySegment {
    if (pfield >> 2) & 0b1 == 1 {
        return LengthOfDaySegment::Long24Bits;
    }
    LengthOfDaySegment::Short16Bits
}

/// Retrieve the [SubmillisPrecision] from the p-field byte.
#[inline]
pub fn precision_from_pfield(pfield: u8) -> SubmillisPrecision {
    match pfield & 0b11 {
        0b01 => SubmillisPrecision::Microseconds,
        0b10 => SubmillisPrecision::Picoseconds,
        0b00 => SubmillisPrecision::Absent,
        0b11 => SubmillisPrecision::Reserved,
        _ => panic!("pfield to SubmillisPrecision failed"),
    }
}

/// This object is the abstraction for the CCSDS Day Segmented Time Code (CDS).
///
/// It has the capability to generate and read timestamps as specified in the CCSDS 301.0-B-4
/// section 3.3 . The width of the days field is configured at compile time via the generic
/// [ProvidesDaysLength] trait which is implemented by [DaysLen16Bits] and [DaysLen24Bits].
///
/// If you do not want to perform a forward check of the days length field with
/// [length_of_day_segment_from_pfield] and you have [alloc] support, you can also
/// use [get_dyn_time_provider_from_bytes] to retrieve the correct instance as a [DynCdsTimeProvider]
/// trait object.
///
/// Custom epochs are not supported yet.
/// Furthermore, the preamble field (p-field) is explicitly conveyed.
/// That means it will always be present when writing the time stamp to a raw buffer, and it
/// must be present when reading a CDS timestamp from a raw buffer.
///
/// # Example
///
/// ```
/// use core::time::Duration;
/// use spacepackets::time::cds::{CdsTime, length_of_day_segment_from_pfield, LengthOfDaySegment};
/// use spacepackets::time::{TimeWriter, CcsdsTimeCode, CcsdsTimeProvider};
///
/// let timestamp_now = CdsTime::now_with_u16_days().unwrap();
/// let mut raw_stamp = [0; 7];
/// {
///     let written = timestamp_now.write_to_bytes(&mut raw_stamp).unwrap();
///     assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCode::Cds as u8);
///     assert_eq!(written, 7);
/// }
/// {
///     assert_eq!(length_of_day_segment_from_pfield(raw_stamp[0]), LengthOfDaySegment::Short16Bits);
///     let read_result = CdsTime::from_bytes_with_u16_days(&raw_stamp);
///     assert!(read_result.is_ok());
///     let stamp_deserialized = read_result.unwrap();
///     assert_eq!(stamp_deserialized.len_as_bytes(), 7);
/// }
/// // It is possible to add a  Duration offset to a timestamp provider. Add 5 minutes offset here
/// let offset = Duration::from_secs(60 * 5);
/// let former_unix_seconds = timestamp_now.unix_secs();
/// let timestamp_in_5_minutes = timestamp_now + offset;
/// assert_eq!(timestamp_in_5_minutes.unix_secs(), former_unix_seconds + 5 * 60);
/// ```
#[derive(Debug, Copy, Clone, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct CdsTime<DaysLen: ProvidesDaysLength = DaysLen16Bits> {
    pfield: u8,
    ccsds_days: DaysLen::FieldType,
    ms_of_day: u32,
    submillis: u32,
}

/// Base properties for all CDS time providers.
///
/// Also exists to encapsulate properties used by private converters.
pub trait CdsBase {
    /// Sub-millisecond precision indicator.
    fn submillis_precision(&self) -> SubmillisPrecision;

    /// Sub-milliseconds.
    fn submillis(&self) -> u32;

    /// Milliseconds of day.
    fn ms_of_day(&self) -> u32;

    /// CCSDS days since the epoch as [u32].
    fn ccsds_days_as_u32(&self) -> u32;
}

/// Generic properties for all CDS time providers.
pub trait CdsTimestamp: CdsBase {
    /// Length of day segment.
    fn len_of_day_seg(&self) -> LengthOfDaySegment;
}

/// Generic implementation of [CcsdsTimeProvider] for CDS time providers.
#[cfg(feature = "alloc")]
pub trait DynCdsTimeProvider: CcsdsTimeProvider + CdsTimestamp + TimeWriter + Any {}

#[cfg(feature = "alloc")]
impl DynCdsTimeProvider for CdsTime<DaysLen16Bits> {}
#[cfg(feature = "alloc")]
impl DynCdsTimeProvider for CdsTime<DaysLen24Bits> {}

/// This function returns the correct [CdsTime] instance from a raw byte array
/// by checking the length of days field. It also checks the CCSDS time code for correctness.
///
/// # Example
///
/// ```
/// use spacepackets::time::cds::{
///     CdsTime, LengthOfDaySegment, get_dyn_time_provider_from_bytes, SubmillisPrecision,
/// };
/// use spacepackets::time::{TimeWriter, CcsdsTimeCode, CcsdsTimeProvider};
///
/// let timestamp_now = CdsTime::new_with_u16_days(24, 24);
/// let mut raw_stamp = [0; 7];
/// {
///     let written = timestamp_now.write_to_bytes(&mut raw_stamp).unwrap();
///     assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCode::Cds as u8);
///     assert_eq!(written, 7);
/// }
/// {
///     let dyn_provider = get_dyn_time_provider_from_bytes(&raw_stamp).unwrap();
///     assert_eq!(dyn_provider.len_of_day_seg(), LengthOfDaySegment::Short16Bits);
///     assert_eq!(dyn_provider.ccsds_days_as_u32(), 24);
///     assert_eq!(dyn_provider.ms_of_day(), 24);
///     assert_eq!(dyn_provider.submillis_precision(), SubmillisPrecision::Absent);
/// }
/// ```
#[cfg(feature = "alloc")]
pub fn get_dyn_time_provider_from_bytes(
    buf: &[u8],
) -> Result<Box<dyn DynCdsTimeProvider>, TimestampError> {
    let time_code = ccsds_time_code_from_p_field(buf[0]);
    if let Err(e) = time_code {
        return Err(TimestampError::InvalidTimeCode {
            expected: CcsdsTimeCode::Cds,
            found: e,
        });
    }
    let time_code = time_code.unwrap();
    if time_code != CcsdsTimeCode::Cds {
        return Err(TimestampError::InvalidTimeCode {
            expected: CcsdsTimeCode::Cds,
            found: time_code as u8,
        });
    }
    if length_of_day_segment_from_pfield(buf[0]) == LengthOfDaySegment::Short16Bits {
        Ok(Box::new(CdsTime::from_bytes_with_u16_days(buf)?))
    } else {
        Ok(Box::new(CdsTime::from_bytes_with_u24_days(buf)?))
    }
}

impl<ProvidesDaysLen: ProvidesDaysLength> CdsBase for CdsTime<ProvidesDaysLen> {
    fn submillis_precision(&self) -> SubmillisPrecision {
        precision_from_pfield(self.pfield)
    }

    fn ms_of_day(&self) -> u32 {
        self.ms_of_day
    }

    fn ccsds_days_as_u32(&self) -> u32 {
        self.ccsds_days.into()
    }

    fn submillis(&self) -> u32 {
        self.submillis
    }
}

impl<ProvidesDaysLen: ProvidesDaysLength> CdsTime<ProvidesDaysLen> {
    /// Please note that a precision value of 0 will be converted to [None] (no precision).
    pub fn set_submillis(&mut self, prec: SubmillisPrecision, value: u32) -> bool {
        self.pfield &= !(0b11);
        if let SubmillisPrecision::Absent = prec {
            // self.submillis_precision = prec;
            self.submillis = 0;
            return true;
        }
        // self.submillis_precision = prec;
        match prec {
            SubmillisPrecision::Microseconds => {
                if value > u16::MAX as u32 {
                    return false;
                }
                self.pfield |= SubmillisPrecision::Microseconds as u8;
                self.submillis = value;
            }
            SubmillisPrecision::Picoseconds => {
                self.pfield |= SubmillisPrecision::Picoseconds as u8;
                self.submillis = value;
            }
            _ => (),
        }
        true
    }

    /// Clear the submillisecond field.
    pub fn clear_submillis(&mut self) {
        self.pfield &= !(0b11);
        self.submillis = 0;
    }

    /// CCSDS days since the CCSDS epoch.
    pub fn ccsds_days(&self) -> ProvidesDaysLen::FieldType {
        self.ccsds_days
    }

    /// Maps the submillisecond precision to a nanosecond value. This will reduce precision when
    /// using picosecond resolution, but significantly simplifies comparison of timestamps.
    pub fn precision_as_ns(&self) -> Option<u32> {
        match self.submillis_precision() {
            SubmillisPrecision::Microseconds => Some(self.submillis * 1000),
            SubmillisPrecision::Picoseconds => Some(self.submillis / 1000),
            _ => None,
        }
    }

    fn generic_raw_read_checks(
        buf: &[u8],
        days_len: LengthOfDaySegment,
    ) -> Result<SubmillisPrecision, TimestampError> {
        if buf.len() < MIN_CDS_FIELD_LEN {
            return Err(TimestampError::ByteConversion(
                ByteConversionError::FromSliceTooSmall {
                    expected: MIN_CDS_FIELD_LEN,
                    found: buf.len(),
                },
            ));
        }
        let pfield = buf[0];
        match CcsdsTimeCode::try_from((pfield >> 4) & 0b111) {
            Ok(cds_type) => match cds_type {
                CcsdsTimeCode::Cds => (),
                _ => {
                    return Err(TimestampError::InvalidTimeCode {
                        expected: CcsdsTimeCode::Cds,
                        found: cds_type as u8,
                    })
                }
            },
            _ => {
                return Err(TimestampError::InvalidTimeCode {
                    expected: CcsdsTimeCode::Cds,
                    found: (pfield >> 4) & 0b111,
                });
            }
        };
        if ((pfield >> 3) & 0b1) == 1 {
            return Err(TimestampError::CustomEpochNotSupported);
        }
        let days_len_from_pfield = length_of_day_segment_from_pfield(pfield);
        if days_len_from_pfield != days_len {
            return Err(CdsError::InvalidCtorForDaysOfLenInPreamble(days_len_from_pfield).into());
        }
        let stamp_len = Self::calc_stamp_len(pfield);
        if buf.len() < stamp_len {
            return Err(TimestampError::ByteConversion(
                ByteConversionError::FromSliceTooSmall {
                    expected: stamp_len,
                    found: buf.len(),
                },
            ));
        }
        Ok(precision_from_pfield(pfield))
    }

    fn calc_stamp_len(pfield: u8) -> usize {
        let mut init_len = 7;
        if length_of_day_segment_from_pfield(pfield) == LengthOfDaySegment::Long24Bits {
            init_len += 1
        }
        match pfield & 0b11 {
            0b01 => {
                init_len += 2;
            }
            0b10 => {
                init_len += 4;
            }
            _ => (),
        }
        init_len
    }

    #[inline]
    fn calc_unix_seconds(&self) -> UnixTime {
        let unix_days_seconds =
            ccsds_to_unix_days(i64::try_from(self.ccsds_days()).unwrap()) * SECONDS_PER_DAY as i64;
        self.calc_unix_seconds_internal(unix_days_seconds, self.ms_of_day())
    }
    #[inline]
    fn calc_unix_seconds_internal(&self, mut unix_days_seconds: i64, ms_of_day: u32) -> UnixTime {
        let seconds_of_day = (ms_of_day / 1000) as i64;
        if unix_days_seconds < 0 {
            unix_days_seconds -= seconds_of_day;
        } else {
            unix_days_seconds += seconds_of_day;
        }
        let mut subsec_nanos = (ms_of_day % 1000) * 10_u32.pow(6);
        if let Some(precision) = self.precision_as_ns() {
            subsec_nanos += precision;
        }
        UnixTime::new(unix_days_seconds, subsec_nanos)
    }

    fn length_check(&self, buf: &[u8], len_as_bytes: usize) -> Result<(), ByteConversionError> {
        if buf.len() < len_as_bytes {
            return Err(ByteConversionError::ToSliceTooSmall {
                expected: len_as_bytes,
                found: buf.len(),
            });
        }
        Ok(())
    }

    fn generic_new(
        days_len: LengthOfDaySegment,
        ccsds_days: ProvidesDaysLen::FieldType,
        ms_of_day: u32,
    ) -> Result<Self, CdsError>
    where
        i64: From<ProvidesDaysLen::FieldType>,
    {
        let provider = Self {
            pfield: Self::generate_p_field(days_len, SubmillisPrecision::Absent),
            ccsds_days,
            ms_of_day,
            submillis: 0,
        };
        Ok(provider)
    }

    #[cfg(feature = "chrono")]
    fn from_dt_generic(
        dt: &chrono::DateTime<chrono::Utc>,
        days_len: LengthOfDaySegment,
    ) -> Result<Self, CdsError> {
        let conv_from_dt = ConversionFromChronoDatetime::new(dt)?;
        Self::generic_from_conversion(days_len, conv_from_dt)
    }

    #[cfg(feature = "chrono")]
    fn from_dt_generic_us_prec(
        dt: &chrono::DateTime<chrono::Utc>,
        days_len: LengthOfDaySegment,
    ) -> Result<Self, CdsError> {
        let conv_from_dt = ConversionFromChronoDatetime::new_with_submillis_us_prec(dt)?;
        Self::generic_from_conversion(days_len, conv_from_dt)
    }

    #[cfg(feature = "chrono")]
    fn from_dt_generic_ps_prec(
        dt: &chrono::DateTime<chrono::Utc>,
        days_len: LengthOfDaySegment,
    ) -> Result<Self, CdsError> {
        let conv_from_dt = ConversionFromChronoDatetime::new_with_submillis_ps_prec(dt)?;
        Self::generic_from_conversion(days_len, conv_from_dt)
    }

    fn from_unix_generic(
        unix_stamp: &UnixTime,
        days_len: LengthOfDaySegment,
        submillis_prec: SubmillisPrecision,
    ) -> Result<Self, CdsError> {
        let conv_from_dt =
            ConversionFromUnix::new(unix_stamp.secs, unix_stamp.subsec_nanos, submillis_prec)?;
        Self::generic_from_conversion(days_len, conv_from_dt)
    }

    #[cfg(feature = "std")]
    fn now_generic(days_len: LengthOfDaySegment) -> Result<Self, StdTimestampError> {
        let conversion_from_now = ConversionFromNow::new()?;
        Self::generic_from_conversion(days_len, conversion_from_now)
            .map_err(|e| StdTimestampError::Timestamp(TimestampError::from(e)))
    }

    #[cfg(feature = "std")]
    fn now_generic_with_us_prec(days_len: LengthOfDaySegment) -> Result<Self, StdTimestampError> {
        let conversion_from_now = ConversionFromNow::new_with_submillis_us_prec()?;
        Self::generic_from_conversion(days_len, conversion_from_now)
            .map_err(|e| StdTimestampError::Timestamp(TimestampError::from(e)))
    }

    #[cfg(feature = "std")]
    fn from_now_generic_ps_prec(days_len: LengthOfDaySegment) -> Result<Self, StdTimestampError> {
        let conversion_from_now = ConversionFromNow::new_with_submillis_ps_prec()?;
        Self::generic_from_conversion(days_len, conversion_from_now)
            .map_err(|e| StdTimestampError::Timestamp(TimestampError::from(e)))
    }

    fn generic_from_conversion<C: CdsConverter>(
        days_len: LengthOfDaySegment,
        converter: C,
    ) -> Result<Self, CdsError> {
        let ccsds_days: ProvidesDaysLen::FieldType = converter
            .ccsds_days_as_u32()
            .try_into()
            .map_err(|_| CdsError::InvalidCcsdsDays(converter.ccsds_days_as_u32().into()))?;
        let provider = Self {
            pfield: Self::generate_p_field(days_len, converter.submillis_precision()),
            ccsds_days,
            ms_of_day: converter.ms_of_day(),
            submillis: converter.submillis(),
        };
        Ok(provider)
    }

    #[cfg(feature = "std")]
    fn generic_conversion_from_now(&self) -> Result<ConversionFromNow, SystemTimeError> {
        Ok(match self.submillis_precision() {
            SubmillisPrecision::Microseconds => ConversionFromNow::new_with_submillis_us_prec()?,
            SubmillisPrecision::Picoseconds => ConversionFromNow::new_with_submillis_ps_prec()?,
            _ => ConversionFromNow::new()?,
        })
    }

    fn generate_p_field(day_seg_len: LengthOfDaySegment, submillis_prec: SubmillisPrecision) -> u8 {
        let mut pfield = P_FIELD_BASE | ((day_seg_len as u8) << 2);
        match submillis_prec {
            SubmillisPrecision::Microseconds => pfield |= SubmillisPrecision::Microseconds as u8,
            SubmillisPrecision::Picoseconds => pfield |= SubmillisPrecision::Picoseconds as u8,
            SubmillisPrecision::Reserved => pfield |= SubmillisPrecision::Reserved as u8,
            _ => (),
        }
        pfield
    }

    /// Update the time from the current time.
    #[cfg(feature = "std")]
    pub fn update_from_now(&mut self) -> Result<(), StdTimestampError> {
        let conversion_from_now = self.generic_conversion_from_now()?;
        let ccsds_days: ProvidesDaysLen::FieldType = conversion_from_now
            .unix_conversion
            .ccsds_days
            .try_into()
            .map_err(|_| {
                StdTimestampError::Timestamp(
                    CdsError::InvalidCcsdsDays(
                        conversion_from_now.unix_conversion.ccsds_days as i64,
                    )
                    .into(),
                )
            })?;
        self.ccsds_days = ccsds_days;
        self.ms_of_day = conversion_from_now.unix_conversion.ms_of_day;
        Ok(())
    }
}

impl CdsTime<DaysLen24Bits> {
    /// Generate a new timestamp provider with the days field width set to 24 bits
    pub fn new_with_u24_days(ccsds_days: u32, ms_of_day: u32) -> Result<Self, CdsError> {
        if ccsds_days > MAX_DAYS_24_BITS {
            return Err(CdsError::InvalidCcsdsDays(ccsds_days.into()));
        }
        Self::generic_new(LengthOfDaySegment::Long24Bits, ccsds_days, ms_of_day)
    }

    /// Generate a time stamp from the current time using the system clock.
    #[cfg(feature = "std")]
    pub fn now_with_u24_days() -> Result<Self, StdTimestampError> {
        Self::now_generic(LengthOfDaySegment::Long24Bits)
    }

    /// Create a provider from a [`chrono::DateTime<chrono::Utc>`] struct.
    ///
    /// ## Errors
    ///
    /// This function will return [CdsError::DateBeforeCcsdsEpoch] if the time is before the CCSDS
    /// epoch (1958-01-01T00:00:00+00:00) or the CCSDS days value exceeds the allowed bit width
    /// (24 bits).
    #[cfg(feature = "chrono")]
    pub fn from_dt_with_u24_days(dt: &chrono::DateTime<chrono::Utc>) -> Result<Self, CdsError> {
        Self::from_dt_generic(dt, LengthOfDaySegment::Long24Bits)
    }

    /// Create a provider from a generic UNIX timestamp (seconds since 1970-01-01T00:00:00+00:00).
    ///
    /// ## Errors
    ///
    /// This function will return [CdsError::DateBeforeCcsdsEpoch] if the time is before the CCSDS
    /// epoch (1958-01-01T00:00:00+00:00) or the CCSDS days value exceeds the allowed bit width
    /// (24 bits).
    pub fn from_unix_time_with_u24_day(
        unix_stamp: &UnixTime,
        submillis_prec: SubmillisPrecision,
    ) -> Result<Self, CdsError> {
        Self::from_unix_generic(unix_stamp, LengthOfDaySegment::Long24Bits, submillis_prec)
    }

    /// Like [Self::from_dt_with_u24_days] but with microsecond sub-millisecond precision.
    #[cfg(feature = "chrono")]
    pub fn from_dt_with_u24_days_us_precision(
        dt: &chrono::DateTime<chrono::Utc>,
    ) -> Result<Self, CdsError> {
        Self::from_dt_generic_us_prec(dt, LengthOfDaySegment::Long24Bits)
    }

    /// Like [Self::from_dt_with_u24_days] but with picoseconds sub-millisecond precision.
    #[cfg(feature = "chrono")]
    pub fn from_dt_with_u24_days_ps_precision(
        dt: &chrono::DateTime<chrono::Utc>,
    ) -> Result<Self, CdsError> {
        Self::from_dt_generic_ps_prec(dt, LengthOfDaySegment::Long24Bits)
    }

    /// Like [Self::now_with_u24_days] but with microsecond sub-millisecond precision.
    #[cfg(feature = "std")]
    pub fn now_with_u24_days_us_precision() -> Result<Self, StdTimestampError> {
        Self::now_generic_with_us_prec(LengthOfDaySegment::Long24Bits)
    }

    /// Like [Self::now_with_u24_days] but with picoseconds sub-millisecond precision.
    #[cfg(feature = "std")]
    pub fn now_with_u24_days_ps_precision() -> Result<Self, StdTimestampError> {
        Self::now_generic_with_us_prec(LengthOfDaySegment::Long24Bits)
    }

    /// Constructor from the CDS timestamp as a raw byte array.
    pub fn from_bytes_with_u24_days(buf: &[u8]) -> Result<Self, TimestampError> {
        let submillis_precision =
            Self::generic_raw_read_checks(buf, LengthOfDaySegment::Long24Bits)?;
        let mut temp_buf: [u8; 4] = [0; 4];
        temp_buf[1..4].copy_from_slice(&buf[1..4]);
        let cccsds_days: u32 = u32::from_be_bytes(temp_buf);
        let ms_of_day: u32 = u32::from_be_bytes(buf[4..8].try_into().unwrap());
        let mut provider = Self::new_with_u24_days(cccsds_days, ms_of_day)?;
        match submillis_precision {
            SubmillisPrecision::Microseconds => {
                provider.set_submillis(
                    SubmillisPrecision::Microseconds,
                    u16::from_be_bytes(buf[8..10].try_into().unwrap()) as u32,
                );
            }
            SubmillisPrecision::Picoseconds => {
                provider.set_submillis(
                    SubmillisPrecision::Picoseconds,
                    u32::from_be_bytes(buf[8..12].try_into().unwrap()),
                );
            }
            _ => (),
        }
        Ok(provider)
    }
}

impl CdsTime<DaysLen16Bits> {
    /// Generate a new timestamp provider with the days field width set to 16 bits
    pub fn new_with_u16_days(ccsds_days: u16, ms_of_day: u32) -> Self {
        // This should never fail, type system ensures CCSDS can not be negative or too large
        Self::generic_new(LengthOfDaySegment::Short16Bits, ccsds_days, ms_of_day).unwrap()
    }

    /// Create a provider from a [`chrono::DateTime<Utc>`] struct.
    ///
    /// This function will return a [CdsError::DateBeforeCcsdsEpoch] if the time is before the
    /// CCSDS epoch (01-01-1958 00:00:00) or the CCSDS days value exceeds the allowed bit width
    /// (16 bits).
    #[cfg(feature = "chrono")]
    pub fn from_dt_with_u16_days(dt: &chrono::DateTime<chrono::Utc>) -> Result<Self, CdsError> {
        Self::from_dt_generic(dt, LengthOfDaySegment::Short16Bits)
    }

    /// Generate a time stamp from the current time using the system clock.
    #[cfg(feature = "std")]
    pub fn now_with_u16_days() -> Result<Self, StdTimestampError> {
        Self::now_generic(LengthOfDaySegment::Short16Bits)
    }

    /// Create a provider from a generic UNIX timestamp (seconds since 1970-01-01T00:00:00+00:00).
    ///
    /// ## Errors
    ///
    /// This function will return [CdsError::DateBeforeCcsdsEpoch] if the time is before the CCSDS
    /// epoch (1958-01-01T00:00:00+00:00) or the CCSDS days value exceeds the allowed bit width
    /// (24 bits).
    pub fn from_unix_time_with_u16_days(
        unix_stamp: &UnixTime,
        submillis_prec: SubmillisPrecision,
    ) -> Result<Self, CdsError> {
        Self::from_unix_generic(unix_stamp, LengthOfDaySegment::Short16Bits, submillis_prec)
    }

    /// Like [Self::from_dt_with_u16_days] but with microsecond sub-millisecond precision.
    #[cfg(feature = "chrono")]
    pub fn from_dt_with_u16_days_us_precision(
        dt: &chrono::DateTime<chrono::Utc>,
    ) -> Result<Self, CdsError> {
        Self::from_dt_generic_us_prec(dt, LengthOfDaySegment::Short16Bits)
    }

    /// Like [Self::from_dt_with_u16_days] but with picoseconds sub-millisecond precision.
    #[cfg(feature = "chrono")]
    pub fn from_dt_with_u16_days_ps_precision(
        dt: &chrono::DateTime<chrono::Utc>,
    ) -> Result<Self, CdsError> {
        Self::from_dt_generic_ps_prec(dt, LengthOfDaySegment::Short16Bits)
    }

    /// Like [Self::now_with_u16_days] but with microsecond sub-millisecond precision.
    #[cfg(feature = "std")]
    pub fn now_with_u16_days_us_precision() -> Result<Self, StdTimestampError> {
        Self::now_generic_with_us_prec(LengthOfDaySegment::Short16Bits)
    }

    /// Like [Self::now_with_u16_days] but with picosecond sub-millisecond precision.
    #[cfg(feature = "std")]
    pub fn from_now_with_u16_days_ps_precision() -> Result<Self, StdTimestampError> {
        Self::from_now_generic_ps_prec(LengthOfDaySegment::Short16Bits)
    }

    /// Write the CDS timestamp to a raw byte array.
    pub fn write_to_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError> {
        self.length_check(buf, self.len_as_bytes())?;
        buf[0] = self.pfield;
        buf[1..3].copy_from_slice(self.ccsds_days.to_be_bytes().as_slice());
        buf[3..7].copy_from_slice(self.ms_of_day.to_be_bytes().as_slice());
        match self.submillis_precision() {
            SubmillisPrecision::Microseconds => {
                buf[7..9].copy_from_slice((self.submillis() as u16).to_be_bytes().as_slice());
            }
            SubmillisPrecision::Picoseconds => {
                buf[7..11].copy_from_slice(self.submillis().to_be_bytes().as_slice());
            }
            _ => (),
        }
        Ok(self.len_as_bytes())
    }

    /// Constructor from the CDS timestamp as a raw byte array.
    pub fn from_bytes_with_u16_days(buf: &[u8]) -> Result<Self, TimestampError> {
        let submillis_precision =
            Self::generic_raw_read_checks(buf, LengthOfDaySegment::Short16Bits)?;
        let ccsds_days: u16 = u16::from_be_bytes(buf[1..3].try_into().unwrap());
        let ms_of_day: u32 = u32::from_be_bytes(buf[3..7].try_into().unwrap());
        let mut provider = Self::new_with_u16_days(ccsds_days, ms_of_day);
        provider.pfield = buf[0];

        match submillis_precision {
            SubmillisPrecision::Microseconds => {
                provider.set_submillis(
                    SubmillisPrecision::Microseconds,
                    u16::from_be_bytes(buf[7..9].try_into().unwrap()) as u32,
                );
            }
            SubmillisPrecision::Picoseconds => {
                provider.set_submillis(
                    SubmillisPrecision::Picoseconds,
                    u32::from_be_bytes(buf[7..11].try_into().unwrap()),
                );
            }
            _ => (),
        }
        Ok(provider)
    }
}

/// Allows adding an duration in form of an offset. Please note that the CCSDS days will rollover
/// when they overflow, because addition needs to be infallible. The user needs to check for a
/// days overflow when this is a possibility and might be a problem.
impl Add<Duration> for CdsTime<DaysLen16Bits> {
    type Output = Self;

    fn add(self, duration: Duration) -> Self::Output {
        let (next_ccsds_days, next_ms_of_day, precision) =
            add_for_max_ccsds_days_val(&self, u16::MAX as u32, duration);
        let mut provider = Self::new_with_u16_days(next_ccsds_days as u16, next_ms_of_day);
        provider.set_submillis(self.submillis_precision(), precision);
        provider
    }
}

impl Add<Duration> for &CdsTime<DaysLen16Bits> {
    type Output = CdsTime<DaysLen16Bits>;

    fn add(self, duration: Duration) -> Self::Output {
        let (next_ccsds_days, next_ms_of_day, precision) =
            add_for_max_ccsds_days_val(self, u16::MAX as u32, duration);
        let mut provider = Self::Output::new_with_u16_days(next_ccsds_days as u16, next_ms_of_day);
        provider.set_submillis(self.submillis_precision(), precision);
        provider
    }
}

/// Allows adding an duration in form of an offset. Please note that the CCSDS days will rollover
/// when they overflow, because addition needs to be infallible. The user needs to check for a
/// days overflow when this is a possibility and might be a problem.
impl Add<Duration> for CdsTime<DaysLen24Bits> {
    type Output = Self;

    fn add(self, duration: Duration) -> Self::Output {
        let (next_ccsds_days, next_ms_of_day, precision) =
            add_for_max_ccsds_days_val(&self, MAX_DAYS_24_BITS, duration);
        let mut provider = Self::new_with_u24_days(next_ccsds_days, next_ms_of_day).unwrap();
        provider.set_submillis(self.submillis_precision(), precision);
        provider
    }
}

impl Add<Duration> for &CdsTime<DaysLen24Bits> {
    type Output = CdsTime<DaysLen24Bits>;
    fn add(self, duration: Duration) -> Self::Output {
        let (next_ccsds_days, next_ms_of_day, precision) =
            add_for_max_ccsds_days_val(self, MAX_DAYS_24_BITS, duration);
        let mut provider =
            Self::Output::new_with_u24_days(next_ccsds_days, next_ms_of_day).unwrap();
        provider.set_submillis(self.submillis_precision(), precision);
        provider
    }
}

/// Allows adding an duration in form of an offset. Please note that the CCSDS days will rollover
/// when they overflow, because addition needs to be infallible. The user needs to check for a
/// days overflow when this is a possibility and might be a problem.
impl AddAssign<Duration> for CdsTime<DaysLen16Bits> {
    fn add_assign(&mut self, duration: Duration) {
        let (next_ccsds_days, next_ms_of_day, submillis) =
            add_for_max_ccsds_days_val(self, u16::MAX as u32, duration);
        self.ccsds_days = next_ccsds_days as u16;
        self.ms_of_day = next_ms_of_day;
        self.submillis = submillis;
    }
}

/// Allows adding an duration in form of an offset. Please note that the CCSDS days will rollover
/// when they overflow, because addition needs to be infallible. The user needs to check for a
/// days overflow when this is a possibility and might be a problem.
impl AddAssign<Duration> for CdsTime<DaysLen24Bits> {
    fn add_assign(&mut self, duration: Duration) {
        let (next_ccsds_days, next_ms_of_day, submillis) =
            add_for_max_ccsds_days_val(self, MAX_DAYS_24_BITS, duration);
        self.ccsds_days = next_ccsds_days;
        self.ms_of_day = next_ms_of_day;
        self.submillis = submillis;
    }
}

#[cfg(feature = "chrono")]
impl TryFrom<chrono::DateTime<chrono::Utc>> for CdsTime<DaysLen16Bits> {
    type Error = CdsError;

    fn try_from(dt: chrono::DateTime<chrono::Utc>) -> Result<Self, Self::Error> {
        let conversion = ConversionFromChronoDatetime::new(&dt)?;
        Self::generic_from_conversion(LengthOfDaySegment::Short16Bits, conversion)
    }
}

#[cfg(feature = "chrono")]
impl TryFrom<chrono::DateTime<chrono::Utc>> for CdsTime<DaysLen24Bits> {
    type Error = CdsError;
    fn try_from(dt: chrono::DateTime<chrono::Utc>) -> Result<Self, Self::Error> {
        let conversion = ConversionFromChronoDatetime::new(&dt)?;
        Self::generic_from_conversion(LengthOfDaySegment::Long24Bits, conversion)
    }
}

impl<ProvidesDaysLen: ProvidesDaysLength> CcsdsTimeProvider for CdsTime<ProvidesDaysLen> {
    #[inline]
    fn len_as_bytes(&self) -> usize {
        Self::calc_stamp_len(self.pfield)
    }

    #[inline]
    fn p_field(&self) -> (usize, [u8; 2]) {
        (1, [self.pfield, 0])
    }

    #[inline]
    fn ccdsd_time_code(&self) -> CcsdsTimeCode {
        CcsdsTimeCode::Cds
    }

    #[inline]
    fn unix_time(&self) -> UnixTime {
        self.calc_unix_seconds()
    }
}

impl TimeReader for CdsTime<DaysLen16Bits> {
    fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError> {
        Self::from_bytes_with_u16_days(buf)
    }
}

impl TimeReader for CdsTime<DaysLen24Bits> {
    fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError> {
        Self::from_bytes_with_u24_days(buf)
    }
}

impl TimeWriter for CdsTime<DaysLen16Bits> {
    fn write_to_bytes(&self, buf: &mut [u8]) -> Result<usize, TimestampError> {
        self.write_to_bytes(buf).map_err(TimestampError::from)
    }

    fn len_written(&self) -> usize {
        self.len_as_bytes()
    }
}

impl CdsTime<DaysLen24Bits> {
    /// Write the CDS timestamp to a raw byte array.
    pub fn write_to_bytes(&self, buf: &mut [u8]) -> Result<usize, ByteConversionError> {
        self.length_check(buf, self.len_as_bytes())?;
        buf[0] = self.pfield;
        let be_days = self.ccsds_days.to_be_bytes();
        buf[1..4].copy_from_slice(&be_days[1..4]);
        buf[4..8].copy_from_slice(self.ms_of_day.to_be_bytes().as_slice());
        match self.submillis_precision() {
            SubmillisPrecision::Microseconds => {
                buf[8..10].copy_from_slice((self.submillis() as u16).to_be_bytes().as_slice());
            }
            SubmillisPrecision::Picoseconds => {
                buf[8..12].copy_from_slice(self.submillis().to_be_bytes().as_slice());
            }
            _ => (),
        }
        Ok(self.len_as_bytes())
    }
}

impl TimeWriter for CdsTime<DaysLen24Bits> {
    fn write_to_bytes(&self, buf: &mut [u8]) -> Result<usize, TimestampError> {
        self.write_to_bytes(buf).map_err(TimestampError::from)
    }

    fn len_written(&self) -> usize {
        self.len_as_bytes()
    }
}

impl<DaysLenProvider: ProvidesDaysLength> PartialEq for CdsTime<DaysLenProvider> {
    fn eq(&self, other: &Self) -> bool {
        if self.ccsds_days == other.ccsds_days
            && self.ms_of_day == other.ms_of_day
            && self.precision_as_ns().unwrap_or(0) == other.precision_as_ns().unwrap_or(0)
        {
            return true;
        }
        false
    }
}

impl<DaysLenProvider: ProvidesDaysLength> PartialOrd for CdsTime<DaysLenProvider> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        if self == other {
            return Some(Ordering::Equal);
        }
        match self.ccsds_days_as_u32().cmp(&other.ccsds_days_as_u32()) {
            Ordering::Less => return Some(Ordering::Less),
            Ordering::Greater => return Some(Ordering::Greater),
            _ => (),
        }
        match self.ms_of_day().cmp(&other.ms_of_day()) {
            Ordering::Less => return Some(Ordering::Less),
            Ordering::Greater => return Some(Ordering::Greater),
            _ => (),
        }
        match self
            .precision_as_ns()
            .unwrap_or(0)
            .cmp(&other.precision_as_ns().unwrap_or(0))
        {
            Ordering::Less => return Some(Ordering::Less),
            Ordering::Greater => return Some(Ordering::Greater),
            _ => (),
        }
        Some(Ordering::Equal)
    }
}

impl<DaysLenProvider: ProvidesDaysLength + Eq> Ord for CdsTime<DaysLenProvider> {
    fn cmp(&self, other: &Self) -> Ordering {
        PartialOrd::partial_cmp(self, other).unwrap()
    }
}

impl From<CdsTime<DaysLen16Bits>> for CdsTime<DaysLen24Bits> {
    fn from(value: CdsTime<DaysLen16Bits>) -> Self {
        // This function only fails if the days value exceeds 24 bits, which is not possible here,
        // so it is okay to unwrap.
        Self::new_with_u24_days(value.ccsds_days_as_u32(), value.ms_of_day()).unwrap()
    }
}

/// This conversion can fail if the days value exceeds 16 bits.
impl TryFrom<CdsTime<DaysLen24Bits>> for CdsTime<DaysLen16Bits> {
    type Error = CdsError;
    fn try_from(value: CdsTime<DaysLen24Bits>) -> Result<Self, CdsError> {
        let ccsds_days = value.ccsds_days_as_u32();
        if ccsds_days > u16::MAX as u32 {
            return Err(CdsError::InvalidCcsdsDays(ccsds_days as i64));
        }
        Ok(Self::new_with_u16_days(
            ccsds_days as u16,
            value.ms_of_day(),
        ))
    }
}

/// Private trait which serves as an abstraction for different converters.
trait CdsConverter: CdsBase {}

struct ConversionFromUnix {
    ccsds_days: u32,
    ms_of_day: u32,
    submilis_prec: SubmillisPrecision,
    submillis: u32,
}

impl ConversionFromUnix {
    fn new(
        unix_seconds: i64,
        subsec_nanos: u32,
        precision: SubmillisPrecision,
    ) -> Result<Self, DateBeforeCcsdsEpochError> {
        let (unix_days, secs_of_day) = calc_unix_days_and_secs_of_day(unix_seconds);
        let ccsds_days = unix_to_ccsds_days(unix_days);
        if ccsds_days == 0 && (secs_of_day > 0 || subsec_nanos > 0) || ccsds_days < 0 {
            return Err(DateBeforeCcsdsEpochError(
                UnixTime::new_checked(unix_seconds, subsec_nanos)
                    .expect("unix timestamp creation failed"),
            ));
        }
        let ms_of_day = secs_of_day * 1000 + subsec_nanos / 10_u32.pow(6);

        let submillis = match precision {
            SubmillisPrecision::Microseconds => (subsec_nanos / 1_000) % 1000,
            SubmillisPrecision::Picoseconds => (subsec_nanos % 10_u32.pow(6)) * 1000,
            _ => 0,
        };
        Ok(Self {
            ccsds_days: unix_to_ccsds_days(unix_days) as u32,
            ms_of_day,
            submilis_prec: precision,
            submillis,
        })
    }
}

impl CdsBase for ConversionFromUnix {
    #[inline]
    fn submillis_precision(&self) -> SubmillisPrecision {
        self.submilis_prec
    }

    #[inline]
    fn ms_of_day(&self) -> u32 {
        self.ms_of_day
    }

    #[inline]
    fn ccsds_days_as_u32(&self) -> u32 {
        self.ccsds_days
    }

    #[inline]
    fn submillis(&self) -> u32 {
        self.submillis
    }
}

impl CdsConverter for ConversionFromUnix {}

/// Helper struct which generates fields for the CDS time provider from a datetime.
#[cfg(feature = "chrono")]
struct ConversionFromChronoDatetime {
    unix_conversion: ConversionFromUnix,
    submillis_prec: SubmillisPrecision,
    submillis: u32,
}

#[cfg(feature = "chrono")]
impl CdsBase for ConversionFromChronoDatetime {
    #[inline]
    fn submillis_precision(&self) -> SubmillisPrecision {
        self.submillis_prec
    }

    delegate::delegate! {
        to self.unix_conversion {
            #[inline]
            fn ms_of_day(&self) -> u32;
            #[inline]
            fn ccsds_days_as_u32(&self) -> u32;
        }
    }

    #[inline]
    fn submillis(&self) -> u32 {
        self.submillis
    }
}

#[cfg(feature = "chrono")]
impl CdsConverter for ConversionFromChronoDatetime {}

#[inline]
fn calc_unix_days_and_secs_of_day(unix_seconds: i64) -> (i64, u32) {
    let mut secs_of_day = unix_seconds % SECONDS_PER_DAY as i64;
    let mut unix_days = (unix_seconds - secs_of_day) / SECONDS_PER_DAY as i64;
    // Imagine the CCSDS epoch time minus 5 seconds: We now have the last day in the year
    // 1969 (-1 unix days) shortly before midnight (SECONDS_PER_DAY - 5).
    if secs_of_day < 0 {
        unix_days -= 1;
        secs_of_day += SECONDS_PER_DAY as i64
    }
    (unix_days, secs_of_day as u32)
}

#[cfg(feature = "chrono")]
impl ConversionFromChronoDatetime {
    fn new(dt: &chrono::DateTime<chrono::Utc>) -> Result<Self, DateBeforeCcsdsEpochError> {
        Self::new_generic(dt, SubmillisPrecision::Absent)
    }

    fn new_with_submillis_us_prec(
        dt: &chrono::DateTime<chrono::Utc>,
    ) -> Result<Self, DateBeforeCcsdsEpochError> {
        Self::new_generic(dt, SubmillisPrecision::Microseconds)
    }

    fn new_with_submillis_ps_prec(
        dt: &chrono::DateTime<chrono::Utc>,
    ) -> Result<Self, DateBeforeCcsdsEpochError> {
        Self::new_generic(dt, SubmillisPrecision::Picoseconds)
    }

    fn new_generic(
        dt: &chrono::DateTime<chrono::Utc>,
        prec: SubmillisPrecision,
    ) -> Result<Self, DateBeforeCcsdsEpochError> {
        // The CDS timestamp does not support timestamps before the CCSDS epoch.
        if dt.year() < 1958 {
            return Err(DateBeforeCcsdsEpochError(UnixTime::from(*dt)));
        }
        // The contained values in the conversion should be all positive now
        let unix_conversion =
            ConversionFromUnix::new(dt.timestamp(), dt.timestamp_subsec_nanos(), prec)?;
        let mut submillis = 0;
        match prec {
            SubmillisPrecision::Microseconds => {
                submillis = dt.timestamp_subsec_micros() % 1000;
            }
            SubmillisPrecision::Picoseconds => {
                submillis = (dt.timestamp_subsec_nanos() % 10_u32.pow(6)) * 1000;
            }
            _ => (),
        }
        Ok(Self {
            unix_conversion,
            submillis_prec: prec,
            submillis,
        })
    }
}

#[cfg(feature = "std")]
struct ConversionFromNow {
    unix_conversion: ConversionFromUnix,
    submillis_prec: SubmillisPrecision,
    submillis: u32,
}

#[cfg(feature = "std")]
impl ConversionFromNow {
    fn new() -> Result<Self, SystemTimeError> {
        Self::new_generic(SubmillisPrecision::Absent)
    }

    fn new_with_submillis_us_prec() -> Result<Self, SystemTimeError> {
        Self::new_generic(SubmillisPrecision::Microseconds)
    }

    fn new_with_submillis_ps_prec() -> Result<Self, SystemTimeError> {
        Self::new_generic(SubmillisPrecision::Picoseconds)
    }

    fn new_generic(prec: SubmillisPrecision) -> Result<Self, SystemTimeError> {
        let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
        let epoch = now.as_secs();
        // This should always return a value with valid (non-negative) CCSDS days,
        // so it is okay to unwrap
        let unix_conversion =
            ConversionFromUnix::new(epoch as i64, now.subsec_nanos(), prec).unwrap();
        let mut submillis = 0;

        match prec {
            SubmillisPrecision::Microseconds => {
                submillis = now.subsec_micros() % 1000;
            }
            SubmillisPrecision::Picoseconds => {
                submillis = (now.subsec_nanos() % 10_u32.pow(6)) * 1000;
            }
            _ => (),
        }
        Ok(Self {
            unix_conversion,
            submillis_prec: prec,
            submillis,
        })
    }
}

#[cfg(feature = "std")]
impl CdsBase for ConversionFromNow {
    fn submillis_precision(&self) -> SubmillisPrecision {
        self.submillis_prec
    }
    delegate::delegate! {
        to self.unix_conversion {
            fn ms_of_day(&self) -> u32;
            fn ccsds_days_as_u32(&self) -> u32;
        }
    }

    fn submillis(&self) -> u32 {
        self.submillis
    }
}

#[cfg(feature = "std")]
impl CdsConverter for ConversionFromNow {}

fn add_for_max_ccsds_days_val<T: ProvidesDaysLength>(
    time_provider: &CdsTime<T>,
    max_days_val: u32,
    duration: Duration,
) -> (u32, u32, u32) {
    let mut next_ccsds_days = time_provider.ccsds_days_as_u32();
    let mut next_ms_of_day = time_provider.ms_of_day;
    // Increment CCSDS days by a certain amount while also accounting for overflow.
    let increment_days = |ccsds_days: &mut u32, days_inc: u32| {
        let days_addition: u64 = *ccsds_days as u64 + days_inc as u64;
        if days_addition > max_days_val as u64 {
            *ccsds_days = (days_addition - max_days_val as u64) as u32;
        } else {
            *ccsds_days += days_inc;
        }
    };
    // Increment MS of day by a certain amount while also accounting for overflow, where
    // the new value exceeds the MS of a day.
    let increment_ms_of_day = |ms_of_day: &mut u32, ms_inc: u32, ccsds_days: &mut u32| {
        *ms_of_day += ms_inc;
        if *ms_of_day >= MS_PER_DAY {
            *ms_of_day -= MS_PER_DAY;
            // Re-use existing closure to always amount for overflow.
            increment_days(ccsds_days, 1);
        }
    };
    let mut submillis = time_provider.submillis();
    match time_provider.submillis_precision() {
        SubmillisPrecision::Microseconds => {
            let subsec_micros = duration.subsec_micros();
            let subsec_millis = subsec_micros / 1000;
            let submilli_micros = subsec_micros % 1000;
            submillis += submilli_micros;
            if submillis >= 1000 {
                let carryover_us = submillis - 1000;
                increment_ms_of_day(&mut next_ms_of_day, 1, &mut next_ccsds_days);
                submillis = carryover_us;
            }
            increment_ms_of_day(&mut next_ms_of_day, subsec_millis, &mut next_ccsds_days);
        }
        SubmillisPrecision::Picoseconds => {
            let subsec_nanos = duration.subsec_nanos();
            let subsec_millis = subsec_nanos / 10_u32.pow(6);
            // 1 ms as ns is 1e6.
            let submilli_nanos = subsec_nanos % 10_u32.pow(6);
            // No overflow risk: The maximum value of an u32 is ~4.294e9, and one ms as ps
            // is 1e9. The amount ps can now have is always less than 2e9.
            submillis += submilli_nanos * 1000;
            if submillis >= 10_u32.pow(9) {
                let carry_over_ps = submillis - 10_u32.pow(9);
                increment_ms_of_day(&mut next_ms_of_day, 1, &mut next_ccsds_days);
                submillis = carry_over_ps;
            }
            increment_ms_of_day(&mut next_ms_of_day, subsec_millis, &mut next_ccsds_days);
        }
        _ => {
            increment_ms_of_day(
                &mut next_ms_of_day,
                duration.subsec_millis(),
                &mut next_ccsds_days,
            );
        }
    }
    // The subsecond millisecond were already handled.
    let full_seconds = duration.as_secs();
    let secs_of_day = (full_seconds % SECONDS_PER_DAY as u64) as u32;
    let ms_of_day = secs_of_day * 1000;
    increment_ms_of_day(&mut next_ms_of_day, ms_of_day, &mut next_ccsds_days);
    increment_days(
        &mut next_ccsds_days,
        (full_seconds as u32 - secs_of_day) / SECONDS_PER_DAY,
    );
    (next_ccsds_days, next_ms_of_day, submillis)
}

impl CdsTimestamp for CdsTime<DaysLen16Bits> {
    fn len_of_day_seg(&self) -> LengthOfDaySegment {
        LengthOfDaySegment::Short16Bits
    }
}

impl CdsTimestamp for CdsTime<DaysLen24Bits> {
    fn len_of_day_seg(&self) -> LengthOfDaySegment {
        LengthOfDaySegment::Long24Bits
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::time::TimestampError::{ByteConversion, InvalidTimeCode};
    use crate::time::{UnixTime, DAYS_CCSDS_TO_UNIX, MS_PER_DAY};
    use crate::ByteConversionError::FromSliceTooSmall;
    use alloc::string::ToString;
    use chrono::{Datelike, NaiveDate, Timelike};
    #[cfg(feature = "serde")]
    use postcard::{from_bytes, to_allocvec};
    use std::format;

    #[test]
    fn test_time_stamp_zero_args() {
        let time_stamper = CdsTime::new_with_u16_days(0, 0);
        let unix_stamp = time_stamper.unix_time();
        assert_eq!(
            unix_stamp.secs,
            (DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32) as i64
        );
        let subsecond_millis = unix_stamp.subsec_nanos;
        assert_eq!(subsecond_millis, 0);
        assert_eq!(
            time_stamper.submillis_precision(),
            SubmillisPrecision::Absent
        );
        assert_eq!(time_stamper.subsec_nanos(), 0);
        assert_eq!(time_stamper.ccdsd_time_code(), CcsdsTimeCode::Cds);
        assert_eq!(
            time_stamper.p_field(),
            (1, [(CcsdsTimeCode::Cds as u8) << 4, 0])
        );
        let date_time = time_stamper.chrono_date_time().unwrap();
        assert_eq!(date_time.year(), 1958);
        assert_eq!(date_time.month(), 1);
        assert_eq!(date_time.day(), 1);
        assert_eq!(date_time.hour(), 0);
        assert_eq!(date_time.minute(), 0);
        assert_eq!(date_time.second(), 0);
    }

    #[test]
    fn test_time_stamp_unix_epoch() {
        let time_stamper = CdsTime::new_with_u16_days((-DAYS_CCSDS_TO_UNIX) as u16, 0);
        assert_eq!(time_stamper.unix_time().secs, 0);
        assert_eq!(
            time_stamper.submillis_precision(),
            SubmillisPrecision::Absent
        );
        let date_time = time_stamper.chrono_date_time().unwrap();
        assert_eq!(date_time.year(), 1970);
        assert_eq!(date_time.month(), 1);
        assert_eq!(date_time.day(), 1);
        assert_eq!(date_time.hour(), 0);
        assert_eq!(date_time.minute(), 0);
        assert_eq!(date_time.second(), 0);
        let time_stamper = CdsTime::new_with_u16_days((-DAYS_CCSDS_TO_UNIX) as u16, 40);
        assert_eq!(time_stamper.subsec_nanos(), 40 * 10_u32.pow(6));
        assert_eq!(time_stamper.subsec_millis(), 40);
        let time_stamper = CdsTime::new_with_u16_days((-DAYS_CCSDS_TO_UNIX) as u16, 1040);
        assert_eq!(time_stamper.subsec_nanos(), 40 * 10_u32.pow(6));
        assert_eq!(time_stamper.subsec_millis(), 40);
    }

    #[test]
    fn test_large_days_field_write() {
        let time_stamper = CdsTime::new_with_u24_days(0x108020_u32, 0x10203040);
        assert!(time_stamper.is_ok());
        let time_stamper = time_stamper.unwrap();
        assert_eq!(time_stamper.len_as_bytes(), 8);
        let mut buf = [0; 16];
        let written = time_stamper.write_to_bytes(&mut buf);
        assert!(written.is_ok());
        let written = written.unwrap();
        assert_eq!(written, 8);
        assert_eq!(buf[1], 0x10);
        assert_eq!(buf[2], 0x80);
        assert_eq!(buf[3], 0x20);
        let ms = u32::from_be_bytes(buf[4..8].try_into().unwrap());
        assert_eq!(ms, 0x10203040);
        assert_eq!((buf[0] >> 2) & 0b1, 1);
    }

    #[test]
    fn test_large_days_field_read() {
        let time_stamper = CdsTime::new_with_u24_days(0x108020_u32, 0);
        assert!(time_stamper.is_ok());
        let time_stamper = time_stamper.unwrap();
        let mut buf = [0; 16];
        let written = time_stamper.write_to_bytes(&mut buf);
        assert!(written.is_ok());
        let provider = CdsTime::<DaysLen24Bits>::from_bytes(&buf);
        assert!(provider.is_ok());
        let provider = provider.unwrap();
        assert_eq!(provider.ccsds_days(), 0x108020);
        assert_eq!(provider.ms_of_day(), 0);
    }

    #[test]
    fn test_large_days_field_read_invalid_ctor() {
        let time_stamper = CdsTime::new_with_u24_days(0x108020, 0);
        assert!(time_stamper.is_ok());
        let time_stamper = time_stamper.unwrap();
        let mut buf = [0; 16];
        let written = time_stamper.write_to_bytes(&mut buf);
        assert!(written.is_ok());
        let faulty_ctor = CdsTime::<DaysLen16Bits>::from_bytes(&buf);
        assert!(faulty_ctor.is_err());
        let error = faulty_ctor.unwrap_err();
        if let TimestampError::Cds(CdsError::InvalidCtorForDaysOfLenInPreamble(len_of_day)) = error
        {
            assert_eq!(len_of_day, LengthOfDaySegment::Long24Bits);
        } else {
            panic!("Wrong error type");
        }
    }

    #[test]
    fn test_write() {
        let mut buf = [0; 16];
        let time_stamper_0 = CdsTime::new_with_u16_days(0, 0);
        let unix_stamp = time_stamper_0.unix_time();
        assert_eq!(
            unix_stamp.secs,
            (DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32).into()
        );
        let mut res = time_stamper_0.write_to_bytes(&mut buf);
        assert!(res.is_ok());
        assert_eq!(buf[0], (CcsdsTimeCode::Cds as u8) << 4);
        assert_eq!(
            u16::from_be_bytes(buf[1..3].try_into().expect("Byte conversion failed")),
            0
        );
        assert_eq!(
            u32::from_be_bytes(buf[3..7].try_into().expect("Byte conversion failed")),
            0
        );
        let time_stamper_1 = CdsTime::new_with_u16_days(u16::MAX - 1, u32::MAX - 1);
        res = time_stamper_1.write_to_bytes(&mut buf);
        assert!(res.is_ok());
        assert_eq!(buf[0], (CcsdsTimeCode::Cds as u8) << 4);
        assert_eq!(
            u16::from_be_bytes(buf[1..3].try_into().expect("Byte conversion failed")),
            u16::MAX - 1
        );
        assert_eq!(
            u32::from_be_bytes(buf[3..7].try_into().expect("Byte conversion failed")),
            u32::MAX - 1
        );
    }

    #[test]
    fn test_faulty_write_buf_too_small() {
        let mut buf = [0; 7];
        let time_stamper = CdsTime::new_with_u16_days(u16::MAX - 1, u32::MAX - 1);
        for i in 0..6 {
            let res = time_stamper.write_to_bytes(&mut buf[0..i]);
            assert!(res.is_err());
            let error = res.unwrap_err();
            match error {
                ByteConversionError::ToSliceTooSmall { found, expected } => {
                    assert_eq!(found, i);
                    assert_eq!(expected, 7);
                    assert_eq!(
                        error.to_string(),
                        format!(
                            "target slice with size {i} is too small, expected size of at least 7"
                        )
                    );
                }
                _ => panic!(
                    "{}",
                    format!("Invalid error {:?} detected", res.unwrap_err())
                ),
            }
        }
    }

    #[test]
    fn test_faulty_read_buf_too_small() {
        let buf = [0; 7];
        for i in 0..6 {
            let res = CdsTime::<DaysLen16Bits>::from_bytes(&buf[0..i]);
            assert!(res.is_err());
            let err = res.unwrap_err();
            match err {
                ByteConversion(e) => match e {
                    FromSliceTooSmall { found, expected } => {
                        assert_eq!(found, i);
                        assert_eq!(expected, 7);
                    }
                    _ => panic!("{}", format!("Invalid error {:?} detected", e)),
                },
                _ => {
                    panic!("Unexpected error {:?}", err);
                }
            }
        }
    }

    #[test]
    fn test_faulty_invalid_pfield() {
        let mut buf = [0; 16];
        let time_stamper_0 = CdsTime::new_with_u16_days(0, 0);
        let res = time_stamper_0.write_to_bytes(&mut buf);
        assert!(res.is_ok());
        buf[0] = 0;
        let res = CdsTime::<DaysLen16Bits>::from_bytes(&buf);
        assert!(res.is_err());
        let err = res.unwrap_err();
        if let InvalidTimeCode { expected, found } = err {
            assert_eq!(expected, CcsdsTimeCode::Cds);
            assert_eq!(found, 0);
            assert_eq!(err.to_string(), "invalid time code, expected Cds, found 0");
        }
    }

    #[test]
    fn test_reading() {
        let mut buf = [0; 16];
        let time_stamper = CdsTime::new_with_u16_days(u16::MAX - 1, u32::MAX - 1);
        let res = time_stamper.write_to_bytes(&mut buf);
        assert!(res.is_ok());
        assert_eq!(buf[0], (CcsdsTimeCode::Cds as u8) << 4);
        assert_eq!(
            u16::from_be_bytes(buf[1..3].try_into().expect("Byte conversion failed")),
            u16::MAX - 1
        );
        assert_eq!(
            u32::from_be_bytes(buf[3..7].try_into().expect("Byte conversion failed")),
            u32::MAX - 1
        );

        let read_stamp: CdsTime<DaysLen16Bits> =
            CdsTime::from_bytes(&buf).expect("Reading timestamp failed");
        assert_eq!(read_stamp.ccsds_days(), u16::MAX - 1);
        assert_eq!(read_stamp.ms_of_day(), u32::MAX - 1);
    }

    fn generic_now_test<T: ProvidesDaysLength>(
        timestamp_now: CdsTime<T>,
        compare_stamp: chrono::DateTime<chrono::Utc>,
    ) {
        let dt = timestamp_now.chrono_date_time().unwrap();
        if compare_stamp.year() > dt.year() {
            assert_eq!(compare_stamp.year() - dt.year(), 1);
        } else {
            assert_eq!(dt.year(), compare_stamp.year());
        }
        generic_dt_property_equality_check(dt.month(), compare_stamp.month(), 1, 12);

        assert_eq!(dt.day(), compare_stamp.day());
        if compare_stamp.day() < dt.day() {
            assert!(dt.day() >= 28);
            assert_eq!(compare_stamp.day(), 1);
        } else if compare_stamp.day() > dt.day() {
            assert_eq!(compare_stamp.day() - dt.day(), 1);
        } else {
            assert_eq!(compare_stamp.day(), dt.day());
        }
        generic_dt_property_equality_check(dt.hour(), compare_stamp.hour(), 0, 23);
        generic_dt_property_equality_check(dt.minute(), compare_stamp.minute(), 0, 59);
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_time_now() {
        let timestamp_now = CdsTime::now_with_u16_days().unwrap();
        let compare_stamp = chrono::Utc::now();
        generic_now_test(timestamp_now, compare_stamp);
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_time_now_us_prec() {
        let timestamp_now = CdsTime::now_with_u16_days_us_precision().unwrap();
        let compare_stamp = chrono::Utc::now();
        generic_now_test(timestamp_now, compare_stamp);
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_time_now_ps_prec() {
        let timestamp_now = CdsTime::from_now_with_u16_days_ps_precision().unwrap();
        let compare_stamp = chrono::Utc::now();
        generic_now_test(timestamp_now, compare_stamp);
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_time_now_ps_prec_u16_days() {
        let timestamp_now = CdsTime::from_now_with_u16_days_ps_precision().unwrap();
        let compare_stamp = chrono::Utc::now();
        generic_now_test(timestamp_now, compare_stamp);
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_time_now_ps_prec_u24_days() {
        let timestamp_now = CdsTime::now_with_u24_days_ps_precision().unwrap();
        let compare_stamp = chrono::Utc::now();
        generic_now_test(timestamp_now, compare_stamp);
    }

    #[test]
    fn test_submillis_precision_micros() {
        let mut time_stamper = CdsTime::new_with_u16_days(0, 0);
        time_stamper.set_submillis(SubmillisPrecision::Microseconds, 500);
        assert_eq!(
            time_stamper.submillis_precision(),
            SubmillisPrecision::Microseconds
        );
        assert_eq!(time_stamper.submillis(), 500);
        let mut write_buf: [u8; 16] = [0; 16];
        let written = time_stamper
            .write_to_bytes(&mut write_buf)
            .expect("Writing timestamp failed");
        assert_eq!(written, 9);
        let cross_check: u16 = 500;
        assert_eq!(write_buf[7..9], cross_check.to_be_bytes());
    }

    #[test]
    fn test_submillis_precision_picos() {
        let mut time_stamper = CdsTime::new_with_u16_days(0, 0);
        time_stamper.set_submillis(SubmillisPrecision::Picoseconds, 5e8 as u32);
        assert_eq!(
            time_stamper.submillis_precision(),
            SubmillisPrecision::Picoseconds
        );
        assert_eq!(time_stamper.submillis(), 5e8 as u32);
        let mut write_buf: [u8; 16] = [0; 16];
        let written = time_stamper
            .write_to_bytes(&mut write_buf)
            .expect("Writing timestamp failed");
        assert_eq!(written, 11);
        let cross_check: u32 = 5e8 as u32;
        assert_eq!(write_buf[7..11], cross_check.to_be_bytes());
    }

    #[test]
    fn read_stamp_with_ps_submillis_precision() {
        let mut time_stamper = CdsTime::new_with_u16_days(0, 0);
        time_stamper.set_submillis(SubmillisPrecision::Picoseconds, 5e8 as u32);
        let mut write_buf: [u8; 16] = [0; 16];
        let written = time_stamper
            .write_to_bytes(&mut write_buf)
            .expect("Writing timestamp failed");
        assert_eq!(written, 11);
        let stamp_deserialized = CdsTime::<DaysLen16Bits>::from_bytes(&write_buf);
        assert!(stamp_deserialized.is_ok());
        let stamp_deserialized = stamp_deserialized.unwrap();
        assert_eq!(stamp_deserialized.len_as_bytes(), 11);
        assert_eq!(
            stamp_deserialized.submillis_precision(),
            SubmillisPrecision::Picoseconds
        );
        assert_eq!(stamp_deserialized.submillis(), 5e8 as u32);
    }

    #[test]
    fn read_stamp_with_us_submillis_precision() {
        let mut time_stamper = CdsTime::new_with_u16_days(0, 0);
        time_stamper.set_submillis(SubmillisPrecision::Microseconds, 500);
        let mut write_buf: [u8; 16] = [0; 16];
        let written = time_stamper
            .write_to_bytes(&mut write_buf)
            .expect("Writing timestamp failed");
        assert_eq!(written, 9);
        let stamp_deserialized = CdsTime::<DaysLen16Bits>::from_bytes(&write_buf);
        assert!(stamp_deserialized.is_ok());
        let stamp_deserialized = stamp_deserialized.unwrap();
        assert_eq!(stamp_deserialized.len_as_bytes(), 9);
        assert_eq!(
            stamp_deserialized.submillis_precision(),
            SubmillisPrecision::Microseconds
        );
        assert_eq!(stamp_deserialized.submillis(), 500);
    }

    #[test]
    fn read_u24_stamp_with_us_submillis_precision() {
        let mut time_stamper = CdsTime::new_with_u24_days(u16::MAX as u32 + 1, 0).unwrap();
        time_stamper.set_submillis(SubmillisPrecision::Microseconds, 500);
        let mut write_buf: [u8; 16] = [0; 16];
        let written = time_stamper
            .write_to_bytes(&mut write_buf)
            .expect("Writing timestamp failed");
        // 1 byte pfield + 3 bytes days + 4 bytes ms of day + 2 bytes us precision
        assert_eq!(written, 10);
        let stamp_deserialized = CdsTime::from_bytes_with_u24_days(&write_buf);
        assert!(stamp_deserialized.is_ok());
        let stamp_deserialized = stamp_deserialized.unwrap();
        assert_eq!(stamp_deserialized.len_as_bytes(), 10);
        assert_eq!(stamp_deserialized.ccsds_days(), u16::MAX as u32 + 1);
        assert_eq!(
            stamp_deserialized.submillis_precision(),
            SubmillisPrecision::Microseconds
        );
        assert_eq!(stamp_deserialized.submillis(), 500);
    }

    #[test]
    fn read_u24_stamp_with_ps_submillis_precision() {
        let mut time_stamper = CdsTime::new_with_u24_days(u16::MAX as u32 + 1, 0).unwrap();
        time_stamper.set_submillis(SubmillisPrecision::Picoseconds, 5e8 as u32);
        let mut write_buf: [u8; 16] = [0; 16];
        let written = time_stamper
            .write_to_bytes(&mut write_buf)
            .expect("Writing timestamp failed");
        // 1 byte pfield + 3 bytes days + 4 bytes ms of day + 4 bytes us precision
        assert_eq!(written, 12);
        let stamp_deserialized = CdsTime::from_bytes_with_u24_days(&write_buf);
        assert!(stamp_deserialized.is_ok());
        let stamp_deserialized = stamp_deserialized.unwrap();
        assert_eq!(stamp_deserialized.len_as_bytes(), 12);
        assert_eq!(stamp_deserialized.ccsds_days(), u16::MAX as u32 + 1);
        assert_eq!(
            stamp_deserialized.submillis_precision(),
            SubmillisPrecision::Picoseconds
        );
        assert_eq!(stamp_deserialized.submillis(), 5e8 as u32);
    }

    fn generic_dt_case_0_no_prec(subsec_millis: u32) -> chrono::DateTime<chrono::Utc> {
        NaiveDate::from_ymd_opt(2023, 1, 14)
            .unwrap()
            .and_hms_milli_opt(16, 49, 30, subsec_millis)
            .unwrap()
            .and_local_timezone(chrono::Utc)
            .unwrap()
    }

    fn generic_check_dt_case_0<DaysLen: ProvidesDaysLength>(
        time_provider: &CdsTime<DaysLen>,
        subsec_millis: u32,
        datetime_utc: chrono::DateTime<chrono::Utc>,
    ) {
        // https://www.timeanddate.com/date/durationresult.html?d1=01&m1=01&y1=1958&d2=14&m2=01&y2=2023
        // Leap years need to be accounted for as well.
        assert_eq!(time_provider.ccsds_days, 23754.into());
        assert_eq!(
            time_provider.ms_of_day,
            30 * 1000 + 49 * 60 * 1000 + 16 * 60 * 60 * 1000 + subsec_millis
        );
        assert_eq!(time_provider.chrono_date_time().unwrap(), datetime_utc);
    }

    #[test]
    fn test_creation_from_dt_u16_days() {
        let subsec_millis = 250;
        let datetime_utc = generic_dt_case_0_no_prec(subsec_millis);
        let time_provider = CdsTime::from_dt_with_u16_days(&datetime_utc).unwrap();
        generic_check_dt_case_0(&time_provider, subsec_millis, datetime_utc);
        let time_provider_2: CdsTime<DaysLen16Bits> =
            datetime_utc.try_into().expect("conversion failed");
        // Test the TryInto trait impl
        assert_eq!(time_provider, time_provider_2);
    }
    #[test]
    fn test_creation_from_dt_u24_days() {
        let subsec_millis = 250;
        let datetime_utc = generic_dt_case_0_no_prec(subsec_millis);
        let time_provider = CdsTime::from_dt_with_u24_days(&datetime_utc).unwrap();
        generic_check_dt_case_0(&time_provider, subsec_millis, datetime_utc);
        let time_provider_2: CdsTime<DaysLen24Bits> =
            datetime_utc.try_into().expect("conversion failed");
        // Test the TryInto trait impl
        assert_eq!(time_provider, time_provider_2);
    }

    fn generic_dt_case_1_us_prec(subsec_millis: u32) -> chrono::DateTime<chrono::Utc> {
        // 250 ms + 500 us
        let subsec_micros = subsec_millis * 1000 + 500;
        NaiveDate::from_ymd_opt(2023, 1, 14)
            .unwrap()
            .and_hms_micro_opt(16, 49, 30, subsec_micros)
            .unwrap()
            .and_local_timezone(chrono::Utc)
            .unwrap()
    }

    fn generic_check_dt_case_1_us_prec<DaysLen: ProvidesDaysLength>(
        time_provider: &CdsTime<DaysLen>,
        subsec_millis: u32,
        datetime_utc: chrono::DateTime<chrono::Utc>,
    ) {
        // https://www.timeanddate.com/date/durationresult.html?d1=01&m1=01&y1=1958&d2=14&m2=01&y2=2023
        // Leap years need to be accounted for as well.
        assert_eq!(time_provider.ccsds_days, 23754.into());
        assert_eq!(
            time_provider.ms_of_day,
            30 * 1000 + 49 * 60 * 1000 + 16 * 60 * 60 * 1000 + subsec_millis
        );
        assert_eq!(
            time_provider.submillis_precision(),
            SubmillisPrecision::Microseconds
        );
        assert_eq!(time_provider.submillis(), 500);
        assert_eq!(time_provider.chrono_date_time().unwrap(), datetime_utc);
    }

    #[test]
    fn test_creation_from_dt_u16_days_us_prec() {
        let subsec_millis = 250;
        let datetime_utc = generic_dt_case_1_us_prec(subsec_millis);
        let time_provider = CdsTime::from_dt_with_u16_days_us_precision(&datetime_utc).unwrap();
        generic_check_dt_case_1_us_prec(&time_provider, subsec_millis, datetime_utc);
    }

    #[test]
    fn test_creation_from_dt_u24_days_us_prec() {
        let subsec_millis = 250;
        let datetime_utc = generic_dt_case_1_us_prec(subsec_millis);
        let time_provider = CdsTime::from_dt_with_u24_days_us_precision(&datetime_utc).unwrap();
        generic_check_dt_case_1_us_prec(&time_provider, subsec_millis, datetime_utc);
    }

    fn generic_dt_case_2_ps_prec(subsec_millis: u32) -> (chrono::DateTime<chrono::Utc>, u32) {
        // 250 ms + 500 us
        let subsec_nanos = subsec_millis * 1000 * 1000 + 500 * 1000;
        let submilli_nanos = subsec_nanos % 10_u32.pow(6);
        (
            NaiveDate::from_ymd_opt(2023, 1, 14)
                .unwrap()
                .and_hms_nano_opt(16, 49, 30, subsec_nanos)
                .unwrap()
                .and_local_timezone(chrono::Utc)
                .unwrap(),
            submilli_nanos,
        )
    }

    fn generic_check_dt_case_2_ps_prec<DaysLen: ProvidesDaysLength>(
        time_provider: &CdsTime<DaysLen>,
        subsec_millis: u32,
        submilli_nanos: u32,
        datetime_utc: chrono::DateTime<chrono::Utc>,
    ) {
        // https://www.timeanddate.com/date/durationresult.html?d1=01&m1=01&y1=1958&d2=14&m2=01&y2=2023
        // Leap years need to be accounted for as well.
        assert_eq!(time_provider.ccsds_days, 23754.into());
        assert_eq!(
            time_provider.ms_of_day,
            30 * 1000 + 49 * 60 * 1000 + 16 * 60 * 60 * 1000 + subsec_millis
        );
        assert_eq!(
            time_provider.submillis_precision(),
            SubmillisPrecision::Picoseconds
        );
        assert_eq!(time_provider.submillis(), submilli_nanos * 1000);
        assert_eq!(time_provider.chrono_date_time().unwrap(), datetime_utc);
    }

    #[test]
    fn test_creation_from_dt_u16_days_ps_prec() {
        let subsec_millis = 250;
        let (datetime_utc, submilli_nanos) = generic_dt_case_2_ps_prec(subsec_millis);
        let time_provider = CdsTime::from_dt_with_u16_days_ps_precision(&datetime_utc).unwrap();
        generic_check_dt_case_2_ps_prec(
            &time_provider,
            subsec_millis,
            submilli_nanos,
            datetime_utc,
        );
    }

    #[test]
    fn test_creation_from_dt_u24_days_ps_prec() {
        let subsec_millis = 250;
        let (datetime_utc, submilli_nanos) = generic_dt_case_2_ps_prec(subsec_millis);
        let time_provider = CdsTime::from_dt_with_u24_days_ps_precision(&datetime_utc).unwrap();
        generic_check_dt_case_2_ps_prec(
            &time_provider,
            subsec_millis,
            submilli_nanos,
            datetime_utc,
        );
    }

    #[test]
    fn test_creation_from_unix_stamp_0_u16_days() {
        let unix_secs = 0;
        let subsec_millis = 0;
        let time_provider = CdsTime::from_unix_time_with_u16_days(
            &UnixTime::new(unix_secs, subsec_millis),
            SubmillisPrecision::Absent,
        )
        .expect("creating provider from unix stamp failed");
        assert_eq!(time_provider.ccsds_days, -DAYS_CCSDS_TO_UNIX as u16)
    }

    #[test]
    fn test_creation_from_unix_stamp_0_u24_days() {
        let unix_secs = 0;
        let subsec_millis = 0;
        let time_provider = CdsTime::from_unix_time_with_u24_day(
            &UnixTime::new(unix_secs, subsec_millis),
            SubmillisPrecision::Absent,
        )
        .expect("creating provider from unix stamp failed");
        assert_eq!(time_provider.ccsds_days, (-DAYS_CCSDS_TO_UNIX) as u32)
    }

    #[test]
    fn test_creation_from_unix_stamp_1() {
        let subsec_millis = 250;
        let datetime_utc = NaiveDate::from_ymd_opt(2023, 1, 14)
            .unwrap()
            .and_hms_milli_opt(16, 49, 30, subsec_millis)
            .unwrap()
            .and_local_timezone(chrono::Utc)
            .unwrap();
        let time_provider =
            CdsTime::from_unix_time_with_u16_days(&datetime_utc.into(), SubmillisPrecision::Absent)
                .expect("creating provider from unix stamp failed");
        // https://www.timeanddate.com/date/durationresult.html?d1=01&m1=01&y1=1958&d2=14&m2=01&y2=2023
        // Leap years need to be accounted for as well.
        assert_eq!(time_provider.ccsds_days, 23754);
        assert_eq!(
            time_provider.ms_of_day,
            30 * 1000 + 49 * 60 * 1000 + 16 * 60 * 60 * 1000 + subsec_millis
        );
        let dt_back = time_provider.chrono_date_time().unwrap();
        assert_eq!(datetime_utc, dt_back);
    }

    #[test]
    fn test_creation_0_ccsds_days() {
        let unix_secs = DAYS_CCSDS_TO_UNIX as i64 * SECONDS_PER_DAY as i64;
        let subsec_millis = 0;
        let time_provider = CdsTime::from_unix_time_with_u16_days(
            &UnixTime::new(unix_secs, subsec_millis),
            SubmillisPrecision::Absent,
        )
        .expect("creating provider from unix stamp failed");
        assert_eq!(time_provider.ccsds_days, 0)
    }

    #[test]
    fn test_invalid_creation_from_unix_stamp_days_too_large() {
        let invalid_unix_secs: i64 = (u16::MAX as i64 + 1) * SECONDS_PER_DAY as i64;
        let subsec_millis = 0;
        match CdsTime::from_unix_time_with_u16_days(
            &UnixTime::new(invalid_unix_secs, subsec_millis),
            SubmillisPrecision::Absent,
        ) {
            Ok(_) => {
                panic!("creation should not succeed")
            }
            Err(e) => {
                if let CdsError::InvalidCcsdsDays(days) = e {
                    assert_eq!(
                        days,
                        unix_to_ccsds_days(invalid_unix_secs / SECONDS_PER_DAY as i64)
                    );
                    assert_eq!(e.to_string(), "invalid ccsds days 69919");
                } else {
                    panic!("unexpected error {}", e)
                }
            }
        }
    }

    #[test]
    fn test_invalid_creation_from_unix_stamp_before_ccsds_epoch() {
        // This is a unix stamp before the CCSDS epoch (01-01-1958 00:00:00), this should be
        // precisely 31-12-1957 23:59:55
        let unix_secs = DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32 - 5;
        let subsec_millis = 0;
        match CdsTime::from_unix_time_with_u16_days(
            &UnixTime::new(unix_secs as i64, subsec_millis),
            SubmillisPrecision::Absent,
        ) {
            Ok(_) => {
                panic!("creation should not succeed")
            }
            Err(e) => {
                if let CdsError::DateBeforeCcsdsEpoch(DateBeforeCcsdsEpochError(unix_dt)) = e {
                    let dt = unix_dt.chrono_date_time();
                    if let chrono::LocalResult::Single(dt) = dt {
                        assert_eq!(dt.year(), 1957);
                        assert_eq!(dt.month(), 12);
                        assert_eq!(dt.day(), 31);
                        assert_eq!(dt.hour(), 23);
                        assert_eq!(dt.minute(), 59);
                        assert_eq!(dt.second(), 55);
                    } else {
                        panic!("unexpected error {}", e)
                    }
                } else {
                    panic!("unexpected error {}", e)
                }
            }
        }
    }

    #[test]
    fn test_addition_u16_days_day_increment() {
        let mut provider = CdsTime::new_with_u16_days(0, MS_PER_DAY - 5 * 1000);
        let seconds_offset = Duration::from_secs(10);
        assert_eq!(provider.ccsds_days, 0);
        assert_eq!(provider.ms_of_day, MS_PER_DAY - 5 * 1000);
        provider += seconds_offset;
        assert_eq!(provider.ccsds_days, 1);
        assert_eq!(provider.ms_of_day, 5000);
    }

    #[test]
    fn test_addition_u16_days() {
        let mut provider = CdsTime::new_with_u16_days(0, 0);
        let seconds_offset = Duration::from_secs(5);
        assert_eq!(provider.ccsds_days, 0);
        assert_eq!(provider.ms_of_day, 0);
        provider += seconds_offset;
        assert_eq!(provider.ms_of_day, 5000);
        // Add one day and test Add operator
        let provider2 = provider + Duration::from_secs(60 * 60 * 24);
        assert_eq!(provider2.ccsds_days, 1);
        assert_eq!(provider2.ms_of_day, 5000);
    }

    #[test]
    fn test_addition_u24_days() {
        let mut provider = CdsTime::new_with_u24_days(u16::MAX as u32, 0).unwrap();
        let seconds_offset = Duration::from_secs(5);
        assert_eq!(provider.ccsds_days, u16::MAX as u32);
        assert_eq!(provider.ms_of_day, 0);
        provider += seconds_offset;
        assert_eq!(provider.ms_of_day, 5000);
        // Add one day and test Add operator
        let provider2 = provider + Duration::from_secs(60 * 60 * 24);
        assert_eq!(provider2.ccsds_days, u16::MAX as u32 + 1);
        assert_eq!(provider2.ms_of_day, 5000);
    }

    #[test]
    fn test_dyn_creation_u24_days() {
        let stamp = CdsTime::new_with_u24_days(u16::MAX as u32 + 1, 24).unwrap();
        let mut buf: [u8; 32] = [0; 32];
        stamp.write_to_bytes(&mut buf).unwrap();
        let dyn_provider = get_dyn_time_provider_from_bytes(&buf);
        assert!(dyn_provider.is_ok());
        let dyn_provider = dyn_provider.unwrap();
        assert_eq!(dyn_provider.ccdsd_time_code(), CcsdsTimeCode::Cds);
        assert_eq!(dyn_provider.ccsds_days_as_u32(), u16::MAX as u32 + 1);
        assert_eq!(dyn_provider.ms_of_day(), 24);
        assert_eq!(
            dyn_provider.submillis_precision(),
            SubmillisPrecision::Absent
        );
        assert_eq!(
            dyn_provider.len_of_day_seg(),
            LengthOfDaySegment::Long24Bits
        );
    }

    #[test]
    fn test_addition_with_us_precision_u16_days() {
        let mut provider = CdsTime::new_with_u16_days(0, 0);
        provider.set_submillis(SubmillisPrecision::Microseconds, 0);
        let duration = Duration::from_micros(500);
        provider += duration;
        assert_eq!(
            provider.submillis_precision(),
            SubmillisPrecision::Microseconds
        );
        assert_eq!(provider.submillis(), 500);
    }

    #[test]
    fn test_addition_with_us_precision_u16_days_with_subsec_millis() {
        let mut provider = CdsTime::new_with_u16_days(0, 0);
        provider.set_submillis(SubmillisPrecision::Microseconds, 0);
        let duration = Duration::from_micros(1200);
        provider += duration;
        assert_eq!(
            provider.submillis_precision(),
            SubmillisPrecision::Microseconds
        );
        assert_eq!(provider.submillis(), 200);
        assert_eq!(provider.ms_of_day(), 1);
    }

    #[test]
    fn test_addition_with_us_precision_u16_days_carry_over() {
        let mut provider = CdsTime::new_with_u16_days(0, 0);
        provider.set_submillis(SubmillisPrecision::Microseconds, 800);
        let duration = Duration::from_micros(400);
        provider += duration;

        assert_eq!(
            provider.submillis_precision(),
            SubmillisPrecision::Microseconds
        );
        assert_eq!(provider.submillis(), 200);
        assert_eq!(provider.ms_of_day(), 1);
    }

    #[test]
    fn test_addition_with_ps_precision_u16_days() {
        let mut provider = CdsTime::new_with_u16_days(0, 0);
        provider.set_submillis(SubmillisPrecision::Picoseconds, 0);
        // 500 us as ns
        let duration = Duration::from_nanos(500 * 10u32.pow(3) as u64);
        provider += duration;

        assert_eq!(
            provider.submillis_precision(),
            SubmillisPrecision::Picoseconds
        );
        assert_eq!(provider.submillis(), 500 * 10u32.pow(6));
    }

    #[test]
    fn test_addition_on_ref() {
        // This test case also tests the case where there is no submillis precision but subsecond
        // milliseconds.
        let provider_ref = &CdsTime::new_with_u16_days(2, 500);
        let new_stamp = provider_ref + Duration::from_millis(2 * 24 * 60 * 60 * 1000 + 500);
        assert_eq!(new_stamp.ccsds_days_as_u32(), 4);
        assert_eq!(new_stamp.ms_of_day, 1000);
    }

    fn check_ps_and_carryover(prec: SubmillisPrecision, submillis: u32, ms_of_day: u32, val: u32) {
        if prec == SubmillisPrecision::Picoseconds {
            assert_eq!(submillis, val);
            assert_eq!(ms_of_day, 1);
        } else {
            panic!("invalid precision {:?}", prec)
        }
    }
    #[test]
    fn test_addition_with_ps_precision_u16_days_with_subsec_millis() {
        let mut provider = CdsTime::new_with_u16_days(0, 0);
        provider.set_submillis(SubmillisPrecision::Picoseconds, 0);
        // 1200 us as ns
        let duration = Duration::from_nanos(1200 * 10u32.pow(3) as u64);
        provider += duration;
        check_ps_and_carryover(
            provider.submillis_precision(),
            provider.submillis(),
            provider.ms_of_day,
            200 * 10_u32.pow(6),
        );
    }

    #[test]
    fn test_addition_with_ps_precision_u16_days_carryover() {
        let mut provider = CdsTime::new_with_u16_days(0, 0);
        // 800 us as ps
        provider.set_submillis(SubmillisPrecision::Picoseconds, 800 * 10_u32.pow(6));
        // 400 us as ns
        let duration = Duration::from_nanos(400 * 10u32.pow(3) as u64);
        provider += duration;
        check_ps_and_carryover(
            provider.submillis_precision(),
            provider.submillis(),
            provider.ms_of_day,
            200 * 10_u32.pow(6),
        );
    }

    #[test]
    fn test_dyn_creation_u16_days_with_precision() {
        let mut stamp = CdsTime::new_with_u16_days(24, 24);
        stamp.set_submillis(SubmillisPrecision::Microseconds, 666);
        let mut buf: [u8; 32] = [0; 32];
        stamp.write_to_bytes(&mut buf).unwrap();
        let dyn_provider = get_dyn_time_provider_from_bytes(&buf);
        assert!(dyn_provider.is_ok());
        let dyn_provider = dyn_provider.unwrap();
        assert_eq!(dyn_provider.ccdsd_time_code(), CcsdsTimeCode::Cds);
        assert_eq!(dyn_provider.ccsds_days_as_u32(), 24);
        assert_eq!(dyn_provider.ms_of_day(), 24);
        assert_eq!(
            dyn_provider.len_of_day_seg(),
            LengthOfDaySegment::Short16Bits
        );
        assert_eq!(
            dyn_provider.submillis_precision(),
            SubmillisPrecision::Microseconds
        );
        assert_eq!(dyn_provider.submillis(), 666);
    }

    #[test]
    fn test_new_u24_days_too_large() {
        let time_provider = CdsTime::new_with_u24_days(2_u32.pow(24), 0);
        assert!(time_provider.is_err());
        let e = time_provider.unwrap_err();
        if let CdsError::InvalidCcsdsDays(days) = e {
            assert_eq!(days, 2_u32.pow(24) as i64);
        } else {
            panic!("unexpected error {}", e)
        }
    }

    #[test]
    fn test_from_dt_invalid_time() {
        // Date before CCSDS epoch
        let datetime_utc = NaiveDate::from_ymd_opt(1957, 12, 31)
            .unwrap()
            .and_hms_milli_opt(23, 59, 59, 999)
            .unwrap()
            .and_local_timezone(chrono::Utc)
            .unwrap();
        let time_provider = CdsTime::from_dt_with_u24_days(&datetime_utc);
        assert!(time_provider.is_err());
        if let CdsError::DateBeforeCcsdsEpoch(DateBeforeCcsdsEpochError(dt)) =
            time_provider.unwrap_err()
        {
            assert_eq!(dt, datetime_utc.into());
        }
    }

    #[test]
    fn test_eq() {
        let stamp0 = CdsTime::new_with_u16_days(0, 0);
        let mut buf: [u8; 7] = [0; 7];
        stamp0.write_to_bytes(&mut buf).unwrap();
        let stamp1 = CdsTime::from_bytes_with_u16_days(&buf).unwrap();
        assert_eq!(stamp0, stamp1);
        assert!(stamp0 >= stamp1);
        assert!(stamp1 <= stamp0);
    }

    #[test]
    fn test_ord() {
        let stamp0 = CdsTime::new_with_u24_days(0, 0).unwrap();
        let stamp1 = CdsTime::new_with_u24_days(0, 50000).unwrap();
        let mut stamp2 = CdsTime::new_with_u24_days(0, 50000).unwrap();
        stamp2.set_submillis(SubmillisPrecision::Microseconds, 500);
        let stamp3 = CdsTime::new_with_u24_days(1, 0).unwrap();
        assert!(stamp1 > stamp0);
        assert!(stamp2 > stamp0);
        assert!(stamp2 > stamp1);
        assert!(stamp3 > stamp0);
        assert!(stamp3 > stamp1);
        assert!(stamp3 > stamp2);
    }

    #[test]
    fn test_conversion() {
        let mut stamp_small = CdsTime::new_with_u16_days(u16::MAX, 500);
        let stamp_larger: CdsTime<DaysLen24Bits> = stamp_small.into();
        assert_eq!(stamp_larger.ccsds_days_as_u32(), u16::MAX as u32);
        assert_eq!(stamp_larger.ms_of_day(), 500);
        stamp_small = stamp_larger.try_into().unwrap();
        assert_eq!(stamp_small.ccsds_days_as_u32(), u16::MAX as u32);
        assert_eq!(stamp_small.ms_of_day(), 500);
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_update_from_now() {
        let mut stamp = CdsTime::new_with_u16_days(0, 0);
        let _ = stamp.update_from_now();
        let dt = stamp.unix_time().chrono_date_time().unwrap();
        assert!(dt.year() > 2020);
    }

    #[test]
    fn test_setting_submillis_precision() {
        let mut provider = CdsTime::new_with_u16_days(0, 15);
        provider.set_submillis(SubmillisPrecision::Microseconds, 500);
    }

    #[test]
    #[cfg(feature = "serde")]
    #[cfg_attr(miri, ignore)]
    fn test_serialization() {
        let stamp_now = CdsTime::now_with_u16_days().expect("Error retrieving time");
        let val = to_allocvec(&stamp_now).expect("Serializing timestamp failed");
        assert!(!val.is_empty());
        let stamp_deser: CdsTime = from_bytes(&val).expect("Stamp deserialization failed");
        assert_eq!(stamp_deser, stamp_now);
    }

    fn generic_dt_property_equality_check(first: u32, second: u32, start: u32, end: u32) {
        if second < first {
            assert_eq!(second, start);
            assert_eq!(first, end);
        } else if second > first {
            assert_eq!(second - first, 1);
        } else {
            assert_eq!(first, second);
        }
    }

    #[test]
    fn test_stamp_to_vec_u16() {
        let stamp = CdsTime::new_with_u16_days(1, 1);
        let stamp_vec = stamp.to_vec().unwrap();
        let mut buf: [u8; 7] = [0; 7];
        stamp.write_to_bytes(&mut buf).unwrap();
        assert_eq!(stamp_vec, buf);
    }

    #[test]
    fn test_stamp_to_vec_u24() {
        let stamp = CdsTime::new_with_u24_days(1, 1).unwrap();
        let stamp_vec = stamp.to_vec().unwrap();
        let mut buf: [u8; 10] = [0; 10];
        stamp.write_to_bytes(&mut buf).unwrap();
        assert_eq!(stamp_vec, buf[..stamp.len_written()]);
    }

    #[test]
    #[cfg(feature = "timelib")]
    fn test_timelib_stamp() {
        let stamp = CdsTime::new_with_u16_days(0, 0);
        let timelib_dt = stamp.timelib_date_time().unwrap();
        assert_eq!(timelib_dt.year(), 1958);
        assert_eq!(timelib_dt.month(), time::Month::January);
        assert_eq!(timelib_dt.day(), 1);
        assert_eq!(timelib_dt.hour(), 0);
        assert_eq!(timelib_dt.minute(), 0);
        assert_eq!(timelib_dt.second(), 0);
    }
}