1
//! Module to generate or read CCSDS Day Segmented (CDS) timestamps as specified in
2
//! [CCSDS 301.0-B-4](https://public.ccsds.org/Pubs/301x0b4e1.pdf) section 3.3 .
3
//!
4
//! The core data structure to do this is the [CdsTime] struct and the
5
//! [get_dyn_time_provider_from_bytes] function to retrieve correct instances of the
6
//! struct from a bytestream.
7
use crate::private::Sealed;
8
use crate::ByteConversionError;
9
use core::cmp::Ordering;
10
use core::fmt::{Debug, Display, Formatter};
11
use core::ops::{Add, AddAssign};
12
use core::time::Duration;
13

            
14
use delegate::delegate;
15

            
16
#[cfg(feature = "std")]
17
use super::StdTimestampError;
18
#[cfg(feature = "std")]
19
use std::error::Error;
20
#[cfg(feature = "std")]
21
use std::time::{SystemTime, SystemTimeError};
22

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

            
26
#[cfg(feature = "alloc")]
27
use super::ccsds_time_code_from_p_field;
28
#[cfg(feature = "alloc")]
29
use alloc::boxed::Box;
30
#[cfg(feature = "alloc")]
31
use core::any::Any;
32

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

            
36
use super::{
37
    ccsds_to_unix_days, unix_to_ccsds_days, CcsdsTimeCode, CcsdsTimeProvider,
38
    DateBeforeCcsdsEpochError, TimeReader, TimeWriter, TimestampError, UnixTime, MS_PER_DAY,
39
    SECONDS_PER_DAY,
40
};
41

            
42
/// Base value for the preamble field for a time field parser to determine the time field type.
43
pub const P_FIELD_BASE: u8 = (CcsdsTimeCode::Cds as u8) << 4;
44
pub const MIN_CDS_FIELD_LEN: usize = 7;
45
pub const MAX_DAYS_24_BITS: u32 = 2_u32.pow(24) - 1;
46

            
47
/// Generic trait implemented by token structs to specify the length of day field at type
48
/// level. This trait is only meant to be implemented in this crate and therefore sealed.
49
pub trait ProvidesDaysLength: Sealed + Clone {
50
    type FieldType: Debug
51
        + Copy
52
        + Clone
53
        + PartialEq
54
        + Eq
55
        + TryFrom<i32>
56
        + TryFrom<u32>
57
        + From<u16>
58
        + Into<u32>
59
        + Into<i64>;
60
}
61

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

            
66
impl Sealed for DaysLen16Bits {}
67
impl ProvidesDaysLength for DaysLen16Bits {
68
    type FieldType = u16;
69
}
70

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

            
79
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
80
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
81
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
82
pub enum LengthOfDaySegment {
83
    Short16Bits = 0,
84
    Long24Bits = 1,
85
}
86

            
87
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
88
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
89
pub enum SubmillisPrecision {
90
    Absent = 0b00,
91
    Microseconds = 0b01,
92
    Picoseconds = 0b10,
93
    Reserved = 0b11,
94
}
95

            
96
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
97
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
98
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
99
pub enum CdsError {
100
    /// CCSDS days value exceeds maximum allowed size or is negative
101
    InvalidCcsdsDays(i64),
102
    /// There are distinct constructors depending on the days field width detected in the preamble
103
    /// field. This error will be returned if there is a missmatch.
104
    InvalidCtorForDaysOfLenInPreamble(LengthOfDaySegment),
105
    DateBeforeCcsdsEpoch(DateBeforeCcsdsEpochError),
106
}
107

            
108
impl Display for CdsError {
109
2
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
110
2
        match self {
111
2
            CdsError::InvalidCcsdsDays(days) => {
112
2
                write!(f, "invalid ccsds days {days}")
113
            }
114
            CdsError::InvalidCtorForDaysOfLenInPreamble(length_of_day) => {
115
                write!(
116
                    f,
117
                    "wrong constructor for length of day {length_of_day:?} detected in preamble",
118
                )
119
            }
120
            CdsError::DateBeforeCcsdsEpoch(e) => write!(f, "date before CCSDS epoch: {e}"),
121
        }
122
2
    }
123
}
124

            
125
#[cfg(feature = "std")]
126
impl Error for CdsError {
127
    fn source(&self) -> Option<&(dyn Error + 'static)> {
128
        match self {
129
            CdsError::DateBeforeCcsdsEpoch(e) => Some(e),
130
            _ => None,
131
        }
132
    }
133
}
134

            
135
impl From<DateBeforeCcsdsEpochError> for CdsError {
136
4
    fn from(value: DateBeforeCcsdsEpochError) -> Self {
137
4
        Self::DateBeforeCcsdsEpoch(value)
138
4
    }
139
}
140

            
141
164
pub fn length_of_day_segment_from_pfield(pfield: u8) -> LengthOfDaySegment {
142
164
    if (pfield >> 2) & 0b1 == 1 {
143
62
        return LengthOfDaySegment::Long24Bits;
144
102
    }
145
102
    LengthOfDaySegment::Short16Bits
146
164
}
147

            
148
#[inline]
149
322
pub fn precision_from_pfield(pfield: u8) -> SubmillisPrecision {
150
322
    match pfield & 0b11 {
151
50
        0b01 => SubmillisPrecision::Microseconds,
152
40
        0b10 => SubmillisPrecision::Picoseconds,
153
232
        0b00 => SubmillisPrecision::Absent,
154
        0b11 => SubmillisPrecision::Reserved,
155
        _ => panic!("pfield to SubmillisPrecision failed"),
156
    }
157
322
}
158

            
159
/// This object is the abstraction for the CCSDS Day Segmented Time Code (CDS).
160
///
161
/// It has the capability to generate and read timestamps as specified in the CCSDS 301.0-B-4
162
/// section 3.3 . The width of the days field is configured at compile time via the generic
163
/// [ProvidesDaysLength] trait which is implemented by [DaysLen16Bits] and [DaysLen24Bits].
164
///
165
/// If you do not want to perform a forward check of the days length field with
166
/// [length_of_day_segment_from_pfield] and you have [alloc] support, you can also
167
/// use [get_dyn_time_provider_from_bytes] to retrieve the correct instance as a [DynCdsTimeProvider]
168
/// trait object.
169
///
170
/// Custom epochs are not supported yet.
171
/// Furthermore, the preamble field (p-field) is explicitly conveyed.
172
/// That means it will always be present when writing the time stamp to a raw buffer, and it
173
/// must be present when reading a CDS timestamp from a raw buffer.
174
///
175
/// # Example
176
///
177
/// ```
178
/// use core::time::Duration;
179
/// use spacepackets::time::cds::{CdsTime, length_of_day_segment_from_pfield, LengthOfDaySegment};
180
/// use spacepackets::time::{TimeWriter, CcsdsTimeCode, CcsdsTimeProvider};
181
///
182
/// let timestamp_now = CdsTime::now_with_u16_days().unwrap();
183
/// let mut raw_stamp = [0; 7];
184
/// {
185
///     let written = timestamp_now.write_to_bytes(&mut raw_stamp).unwrap();
186
///     assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCode::Cds as u8);
187
///     assert_eq!(written, 7);
188
/// }
189
/// {
190
///     assert_eq!(length_of_day_segment_from_pfield(raw_stamp[0]), LengthOfDaySegment::Short16Bits);
191
///     let read_result = CdsTime::from_bytes_with_u16_days(&raw_stamp);
192
///     assert!(read_result.is_ok());
193
///     let stamp_deserialized = read_result.unwrap();
194
///     assert_eq!(stamp_deserialized.len_as_bytes(), 7);
195
/// }
196
/// // It is possible to add a  Duration offset to a timestamp provider. Add 5 minutes offset here
197
/// let offset = Duration::from_secs(60 * 5);
198
/// let former_unix_seconds = timestamp_now.unix_secs();
199
/// let timestamp_in_5_minutes = timestamp_now + offset;
200
/// assert_eq!(timestamp_in_5_minutes.unix_secs(), former_unix_seconds + 5 * 60);
201
/// ```
202
#[derive(Debug, Copy, Clone, Eq)]
203
2
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
204
pub struct CdsTime<DaysLen: ProvidesDaysLength = DaysLen16Bits> {
205
    pfield: u8,
206
    ccsds_days: DaysLen::FieldType,
207
    ms_of_day: u32,
208
    submillis: u32,
209
    /// This is not strictly necessary but still cached because it significantly simplifies the
210
    /// calculation of [`DateTime<Utc>`].
211
    unix_time: UnixTime,
212
}
213

            
214
/// Common properties for all CDS time providers.
215
///
216
/// Also exists to encapsulate properties used by private converters.
217
pub trait CdsCommon {
218
    fn submillis_precision(&self) -> SubmillisPrecision;
219
    fn submillis(&self) -> u32;
220
    fn ms_of_day(&self) -> u32;
221
    fn ccsds_days_as_u32(&self) -> u32;
222
}
223

            
224
/// Generic properties for all CDS time providers.
225
pub trait CdsTimestamp: CdsCommon {
226
    fn len_of_day_seg(&self) -> LengthOfDaySegment;
227
}
228

            
229
/// Private trait which serves as an abstraction for different converters.
230
trait CdsConverter: CdsCommon {
231
    fn unix_days_seconds(&self) -> i64;
232
}
233

            
234
struct ConversionFromUnix {
235
    ccsds_days: u32,
236
    ms_of_day: u32,
237
    submilis_prec: SubmillisPrecision,
238
    submillis: u32,
239
    /// This is a side-product of the calculation of the CCSDS days. It is useful for
240
    /// re-calculating the datetime at a later point and therefore supplied as well.
241
    unix_days_seconds: i64,
242
}
243

            
244
impl ConversionFromUnix {
245
42
    fn new(
246
42
        unix_seconds: i64,
247
42
        subsec_nanos: u32,
248
42
        precision: SubmillisPrecision,
249
42
    ) -> Result<Self, DateBeforeCcsdsEpochError> {
250
42
        let (unix_days, secs_of_day) = calc_unix_days_and_secs_of_day(unix_seconds);
251
42
        let ccsds_days = unix_to_ccsds_days(unix_days);
252
42
        if ccsds_days == 0 && (secs_of_day > 0 || subsec_nanos > 0) || ccsds_days < 0 {
253
2
            return Err(DateBeforeCcsdsEpochError(
254
2
                UnixTime::new_checked(unix_seconds, subsec_nanos)
255
2
                    .expect("unix timestamp creation failed"),
256
2
            ));
257
40
        }
258
40
        let ms_of_day = secs_of_day * 1000 + subsec_nanos / 10_u32.pow(6);
259

            
260
40
        let submillis = match precision {
261
8
            SubmillisPrecision::Microseconds => (subsec_nanos / 1_000) % 1000,
262
8
            SubmillisPrecision::Picoseconds => (subsec_nanos % 10_u32.pow(6)) * 1000,
263
24
            _ => 0,
264
        };
265
40
        Ok(Self {
266
40
            ccsds_days: unix_to_ccsds_days(unix_days) as u32,
267
40
            ms_of_day,
268
40
            unix_days_seconds: unix_days * SECONDS_PER_DAY as i64,
269
40
            submilis_prec: precision,
270
40
            submillis,
271
40
        })
272
42
    }
273
}
274

            
275
impl CdsCommon for ConversionFromUnix {
276
    #[inline]
277
8
    fn submillis_precision(&self) -> SubmillisPrecision {
278
8
        self.submilis_prec
279
8
    }
280

            
281
    #[inline]
282
72
    fn ms_of_day(&self) -> u32 {
283
72
        self.ms_of_day
284
72
    }
285

            
286
    #[inline]
287
40
    fn ccsds_days_as_u32(&self) -> u32 {
288
40
        self.ccsds_days
289
40
    }
290

            
291
    #[inline]
292
8
    fn submillis(&self) -> u32 {
293
8
        self.submillis
294
8
    }
295
}
296

            
297
impl CdsConverter for ConversionFromUnix {
298
    #[inline]
299
36
    fn unix_days_seconds(&self) -> i64 {
300
36
        self.unix_days_seconds
301
36
    }
302
}
303
/// Helper struct which generates fields for the CDS time provider from a datetime.
304
struct ConversionFromChronoDatetime {
305
    unix_conversion: ConversionFromUnix,
306
    submillis_prec: SubmillisPrecision,
307
    submillis: u32,
308
}
309

            
310
impl CdsCommon for ConversionFromChronoDatetime {
311
    #[inline]
312
16
    fn submillis_precision(&self) -> SubmillisPrecision {
313
16
        self.submillis_prec
314
16
    }
315

            
316
    delegate! {
317
        to self.unix_conversion {
318
            #[inline]
319
32
            fn ms_of_day(&self) -> u32;
320
            #[inline]
321
16
            fn ccsds_days_as_u32(&self) -> u32;
322
        }
323
    }
324

            
325
    #[inline]
326
16
    fn submillis(&self) -> u32 {
327
16
        self.submillis
328
16
    }
329
}
330

            
331
impl CdsConverter for ConversionFromChronoDatetime {
332
    delegate! {to self.unix_conversion {
333
        #[inline]
334
16
        fn unix_days_seconds(&self) -> i64;
335
    }}
336
}
337

            
338
#[inline]
339
42
fn calc_unix_days_and_secs_of_day(unix_seconds: i64) -> (i64, u32) {
340
42
    let mut secs_of_day = unix_seconds % SECONDS_PER_DAY as i64;
341
42
    let mut unix_days = (unix_seconds - secs_of_day) / SECONDS_PER_DAY as i64;
342
42
    // Imagine the CCSDS epoch time minus 5 seconds: We now have the last day in the year
343
42
    // 1969 (-1 unix days) shortly before midnight (SECONDS_PER_DAY - 5).
344
42
    if secs_of_day < 0 {
345
2
        unix_days -= 1;
346
2
        secs_of_day += SECONDS_PER_DAY as i64
347
40
    }
348
42
    (unix_days, secs_of_day as u32)
349
42
}
350

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

            
357
4
    fn new_with_submillis_us_prec(
358
4
        dt: &chrono::DateTime<chrono::Utc>,
359
4
    ) -> Result<Self, DateBeforeCcsdsEpochError> {
360
4
        Self::new_generic(dt, SubmillisPrecision::Microseconds)
361
4
    }
362

            
363
4
    fn new_with_submillis_ps_prec(
364
4
        dt: &chrono::DateTime<chrono::Utc>,
365
4
    ) -> Result<Self, DateBeforeCcsdsEpochError> {
366
4
        Self::new_generic(dt, SubmillisPrecision::Picoseconds)
367
4
    }
368

            
369
    #[cfg(feature = "chrono")]
370
18
    fn new_generic(
371
18
        dt: &chrono::DateTime<chrono::Utc>,
372
18
        prec: SubmillisPrecision,
373
18
    ) -> Result<Self, DateBeforeCcsdsEpochError> {
374
18
        // The CDS timestamp does not support timestamps before the CCSDS epoch.
375
18
        if dt.year() < 1958 {
376
2
            return Err(DateBeforeCcsdsEpochError(UnixTime::from(*dt)));
377
16
        }
378
        // The contained values in the conversion should be all positive now
379
16
        let unix_conversion =
380
16
            ConversionFromUnix::new(dt.timestamp(), dt.timestamp_subsec_nanos(), prec)?;
381
16
        let mut submillis = 0;
382
16
        match prec {
383
4
            SubmillisPrecision::Microseconds => {
384
4
                submillis = dt.timestamp_subsec_micros() % 1000;
385
4
            }
386
4
            SubmillisPrecision::Picoseconds => {
387
4
                submillis = (dt.timestamp_subsec_nanos() % 10_u32.pow(6)) * 1000;
388
4
            }
389
8
            _ => (),
390
        }
391
16
        Ok(Self {
392
16
            unix_conversion,
393
16
            submillis_prec: prec,
394
16
            submillis,
395
16
        })
396
18
    }
397
}
398

            
399
#[cfg(feature = "std")]
400
struct ConversionFromNow {
401
    unix_conversion: ConversionFromUnix,
402
    submillis_prec: SubmillisPrecision,
403
    submillis: u32,
404
}
405

            
406
#[cfg(feature = "std")]
407
impl ConversionFromNow {
408
6
    fn new() -> Result<Self, SystemTimeError> {
409
6
        Self::new_generic(SubmillisPrecision::Absent)
410
6
    }
411

            
412
4
    fn new_with_submillis_us_prec() -> Result<Self, SystemTimeError> {
413
4
        Self::new_generic(SubmillisPrecision::Microseconds)
414
4
    }
415

            
416
4
    fn new_with_submillis_ps_prec() -> Result<Self, SystemTimeError> {
417
4
        Self::new_generic(SubmillisPrecision::Picoseconds)
418
4
    }
419

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

            
429
14
        match prec {
430
4
            SubmillisPrecision::Microseconds => {
431
4
                submillis = now.subsec_micros() % 1000;
432
4
            }
433
4
            SubmillisPrecision::Picoseconds => {
434
4
                submillis = (now.subsec_nanos() % 10_u32.pow(6)) * 1000;
435
4
            }
436
6
            _ => (),
437
        }
438
14
        Ok(Self {
439
14
            unix_conversion,
440
14
            submillis_prec: prec,
441
14
            submillis,
442
14
        })
443
14
    }
444
}
445

            
446
#[cfg(feature = "std")]
447
impl CdsCommon for ConversionFromNow {
448
12
    fn submillis_precision(&self) -> SubmillisPrecision {
449
12
        self.submillis_prec
450
12
    }
451
    delegate! {
452
        to self.unix_conversion {
453
24
            fn ms_of_day(&self) -> u32;
454
12
            fn ccsds_days_as_u32(&self) -> u32;
455
        }
456
    }
457

            
458
12
    fn submillis(&self) -> u32 {
459
12
        self.submillis
460
12
    }
461
}
462

            
463
#[cfg(feature = "std")]
464
impl CdsConverter for ConversionFromNow {
465
12
    delegate! {to self.unix_conversion { fn unix_days_seconds(&self) -> i64; }}
466
}
467

            
468
#[cfg(feature = "alloc")]
469
pub trait DynCdsTimeProvider: CcsdsTimeProvider + CdsTimestamp + TimeWriter + Any {}
470
#[cfg(feature = "alloc")]
471
impl DynCdsTimeProvider for CdsTime<DaysLen16Bits> {}
472
#[cfg(feature = "alloc")]
473
impl DynCdsTimeProvider for CdsTime<DaysLen24Bits> {}
474

            
475
/// This function returns the correct [CdsTime] instance from a raw byte array
476
/// by checking the length of days field. It also checks the CCSDS time code for correctness.
477
///
478
/// # Example
479
///
480
/// ```
481
/// use spacepackets::time::cds::{
482
///     CdsTime, LengthOfDaySegment, get_dyn_time_provider_from_bytes, SubmillisPrecision,
483
/// };
484
/// use spacepackets::time::{TimeWriter, CcsdsTimeCode, CcsdsTimeProvider};
485
///
486
/// let timestamp_now = CdsTime::new_with_u16_days(24, 24);
487
/// let mut raw_stamp = [0; 7];
488
/// {
489
///     let written = timestamp_now.write_to_bytes(&mut raw_stamp).unwrap();
490
///     assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCode::Cds as u8);
491
///     assert_eq!(written, 7);
492
/// }
493
/// {
494
///     let dyn_provider = get_dyn_time_provider_from_bytes(&raw_stamp).unwrap();
495
///     assert_eq!(dyn_provider.len_of_day_seg(), LengthOfDaySegment::Short16Bits);
496
///     assert_eq!(dyn_provider.ccsds_days_as_u32(), 24);
497
///     assert_eq!(dyn_provider.ms_of_day(), 24);
498
///     assert_eq!(dyn_provider.submillis_precision(), SubmillisPrecision::Absent);
499
/// }
500
/// ```
501
#[cfg(feature = "alloc")]
502
4
pub fn get_dyn_time_provider_from_bytes(
503
4
    buf: &[u8],
504
4
) -> Result<Box<dyn DynCdsTimeProvider>, TimestampError> {
505
4
    let time_code = ccsds_time_code_from_p_field(buf[0]);
506
4
    if let Err(e) = time_code {
507
        return Err(TimestampError::InvalidTimeCode {
508
            expected: CcsdsTimeCode::Cds,
509
            found: e,
510
        });
511
4
    }
512
4
    let time_code = time_code.unwrap();
513
4
    if time_code != CcsdsTimeCode::Cds {
514
        return Err(TimestampError::InvalidTimeCode {
515
            expected: CcsdsTimeCode::Cds,
516
            found: time_code as u8,
517
        });
518
4
    }
519
4
    if length_of_day_segment_from_pfield(buf[0]) == LengthOfDaySegment::Short16Bits {
520
2
        Ok(Box::new(CdsTime::from_bytes_with_u16_days(buf)?))
521
    } else {
522
2
        Ok(Box::new(CdsTime::from_bytes_with_u24_days(buf)?))
523
    }
524
4
}
525

            
526
impl<ProvidesDaysLen: ProvidesDaysLength> CdsCommon for CdsTime<ProvidesDaysLen> {
527
304
    fn submillis_precision(&self) -> SubmillisPrecision {
528
304
        precision_from_pfield(self.pfield)
529
304
    }
530

            
531
32
    fn ms_of_day(&self) -> u32 {
532
32
        self.ms_of_day
533
32
    }
534

            
535
62
    fn ccsds_days_as_u32(&self) -> u32 {
536
62
        self.ccsds_days.into()
537
62
    }
538

            
539
72
    fn submillis(&self) -> u32 {
540
72
        self.submillis
541
72
    }
542
}
543

            
544
impl<ProvidesDaysLen: ProvidesDaysLength> CdsTime<ProvidesDaysLen> {
545
    /// Please note that a precision value of 0 will be converted to [None] (no precision).
546
46
    pub fn set_submillis(&mut self, prec: SubmillisPrecision, value: u32) -> bool {
547
46
        self.pfield &= !(0b11);
548
46
        if let SubmillisPrecision::Absent = prec {
549
            // self.submillis_precision = prec;
550
6
            self.submillis = 0;
551
6
            return true;
552
40
        }
553
40
        // self.submillis_precision = prec;
554
40
        match prec {
555
            SubmillisPrecision::Microseconds => {
556
24
                if value > u16::MAX as u32 {
557
                    return false;
558
24
                }
559
24
                self.pfield |= SubmillisPrecision::Microseconds as u8;
560
24
                self.submillis = value;
561
            }
562
16
            SubmillisPrecision::Picoseconds => {
563
16
                self.pfield |= SubmillisPrecision::Picoseconds as u8;
564
16
                self.submillis = value;
565
16
            }
566
            _ => (),
567
        }
568
40
        true
569
46
    }
570

            
571
    pub fn clear_submillis(&mut self) {
572
        self.pfield &= !(0b11);
573
        self.submillis = 0;
574
    }
575

            
576
8
    pub fn ccsds_days(&self) -> ProvidesDaysLen::FieldType {
577
8
        self.ccsds_days
578
8
    }
579

            
580
    /// Maps the submillisecond precision to a nanosecond value. This will reduce precision when
581
    /// using picosecond resolution, but significantly simplifies comparison of timestamps.
582
186
    pub fn precision_as_ns(&self) -> Option<u32> {
583
186
        match self.submillis_precision() {
584
12
            SubmillisPrecision::Microseconds => Some(self.submillis * 1000),
585
8
            SubmillisPrecision::Picoseconds => Some(self.submillis / 1000),
586
166
            _ => None,
587
        }
588
186
    }
589

            
590
34
    fn generic_raw_read_checks(
591
34
        buf: &[u8],
592
34
        days_len: LengthOfDaySegment,
593
34
    ) -> Result<SubmillisPrecision, TimestampError> {
594
34
        if buf.len() < MIN_CDS_FIELD_LEN {
595
12
            return Err(TimestampError::ByteConversion(
596
12
                ByteConversionError::FromSliceTooSmall {
597
12
                    expected: MIN_CDS_FIELD_LEN,
598
12
                    found: buf.len(),
599
12
                },
600
12
            ));
601
22
        }
602
22
        let pfield = buf[0];
603
22
        match CcsdsTimeCode::try_from(pfield >> 4 & 0b111) {
604
20
            Ok(cds_type) => match cds_type {
605
20
                CcsdsTimeCode::Cds => (),
606
                _ => {
607
                    return Err(TimestampError::InvalidTimeCode {
608
                        expected: CcsdsTimeCode::Cds,
609
                        found: cds_type as u8,
610
                    })
611
                }
612
            },
613
            _ => {
614
2
                return Err(TimestampError::InvalidTimeCode {
615
2
                    expected: CcsdsTimeCode::Cds,
616
2
                    found: pfield >> 4 & 0b111,
617
2
                });
618
            }
619
        };
620
20
        if ((pfield >> 3) & 0b1) == 1 {
621
            return Err(TimestampError::CustomEpochNotSupported);
622
20
        }
623
20
        let days_len_from_pfield = length_of_day_segment_from_pfield(pfield);
624
20
        if days_len_from_pfield != days_len {
625
2
            return Err(CdsError::InvalidCtorForDaysOfLenInPreamble(days_len_from_pfield).into());
626
18
        }
627
18
        let stamp_len = Self::calc_stamp_len(pfield);
628
18
        if buf.len() < stamp_len {
629
            return Err(TimestampError::ByteConversion(
630
                ByteConversionError::FromSliceTooSmall {
631
                    expected: stamp_len,
632
                    found: buf.len(),
633
                },
634
            ));
635
18
        }
636
18
        Ok(precision_from_pfield(pfield))
637
34
    }
638

            
639
140
    fn calc_stamp_len(pfield: u8) -> usize {
640
140
        let mut init_len = 7;
641
140
        if length_of_day_segment_from_pfield(pfield) == LengthOfDaySegment::Long24Bits {
642
50
            init_len += 1
643
90
        }
644
140
        match pfield & 0b11 {
645
26
            0b01 => {
646
26
                init_len += 2;
647
26
            }
648
20
            0b10 => {
649
20
                init_len += 4;
650
20
            }
651
94
            _ => (),
652
        }
653
140
        init_len
654
140
    }
655

            
656
154
    fn setup(&mut self, unix_days_seconds: i64, ms_of_day: u32) {
657
154
        self.calc_unix_seconds(unix_days_seconds, ms_of_day);
658
154
    }
659

            
660
    #[inline]
661
154
    fn calc_unix_seconds(&mut self, mut unix_days_seconds: i64, ms_of_day: u32) {
662
154
        let seconds_of_day = (ms_of_day / 1000) as i64;
663
154
        if unix_days_seconds < 0 {
664
74
            unix_days_seconds -= seconds_of_day;
665
80
        } else {
666
80
            unix_days_seconds += seconds_of_day;
667
80
        }
668
154
        let mut subsec_nanos = (ms_of_day % 1000) * 10_u32.pow(6);
669
154
        if let Some(precision) = self.precision_as_ns() {
670
16
            subsec_nanos += precision;
671
138
        }
672
154
        self.unix_time = UnixTime::new(unix_days_seconds, subsec_nanos);
673
154
    }
674

            
675
58
    fn length_check(&self, buf: &[u8], len_as_bytes: usize) -> Result<(), TimestampError> {
676
58
        if buf.len() < len_as_bytes {
677
12
            return Err(TimestampError::ByteConversion(
678
12
                ByteConversionError::ToSliceTooSmall {
679
12
                    expected: len_as_bytes,
680
12
                    found: buf.len(),
681
12
                },
682
12
            ));
683
46
        }
684
46
        Ok(())
685
58
    }
686

            
687
116
    fn generic_new(
688
116
        days_len: LengthOfDaySegment,
689
116
        ccsds_days: ProvidesDaysLen::FieldType,
690
116
        ms_of_day: u32,
691
116
    ) -> Result<Self, CdsError>
692
116
    where
693
116
        i64: From<ProvidesDaysLen::FieldType>,
694
116
    {
695
116
        let mut provider = Self {
696
116
            pfield: Self::generate_p_field(days_len, SubmillisPrecision::Absent),
697
116
            ccsds_days,
698
116
            ms_of_day,
699
116
            unix_time: Default::default(),
700
116
            submillis: 0,
701
116
        };
702
116
        let unix_days_seconds = ccsds_to_unix_days(i64::from(ccsds_days)) * SECONDS_PER_DAY as i64;
703
116
        provider.setup(unix_days_seconds, ms_of_day);
704
116
        Ok(provider)
705
116
    }
706

            
707
    #[cfg(feature = "chrono")]
708
6
    fn from_dt_generic(
709
6
        dt: &chrono::DateTime<chrono::Utc>,
710
6
        days_len: LengthOfDaySegment,
711
6
    ) -> Result<Self, CdsError> {
712
6
        let conv_from_dt = ConversionFromChronoDatetime::new(dt)?;
713
4
        Self::generic_from_conversion(days_len, conv_from_dt)
714
6
    }
715

            
716
    #[cfg(feature = "chrono")]
717
4
    fn from_dt_generic_us_prec(
718
4
        dt: &chrono::DateTime<chrono::Utc>,
719
4
        days_len: LengthOfDaySegment,
720
4
    ) -> Result<Self, CdsError> {
721
4
        let conv_from_dt = ConversionFromChronoDatetime::new_with_submillis_us_prec(dt)?;
722
4
        Self::generic_from_conversion(days_len, conv_from_dt)
723
4
    }
724

            
725
    #[cfg(feature = "chrono")]
726
4
    fn from_dt_generic_ps_prec(
727
4
        dt: &chrono::DateTime<chrono::Utc>,
728
4
        days_len: LengthOfDaySegment,
729
4
    ) -> Result<Self, CdsError> {
730
4
        let conv_from_dt = ConversionFromChronoDatetime::new_with_submillis_ps_prec(dt)?;
731
4
        Self::generic_from_conversion(days_len, conv_from_dt)
732
4
    }
733

            
734
12
    fn from_unix_generic(
735
12
        unix_stamp: &UnixTime,
736
12
        days_len: LengthOfDaySegment,
737
12
        submillis_prec: SubmillisPrecision,
738
12
    ) -> Result<Self, CdsError> {
739
10
        let conv_from_dt =
740
12
            ConversionFromUnix::new(unix_stamp.secs, unix_stamp.subsec_nanos, submillis_prec)?;
741
10
        Self::generic_from_conversion(days_len, conv_from_dt)
742
12
    }
743

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

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

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

            
765
38
    fn generic_from_conversion<C: CdsConverter>(
766
38
        days_len: LengthOfDaySegment,
767
38
        converter: C,
768
38
    ) -> Result<Self, CdsError> {
769
38
        let ccsds_days: ProvidesDaysLen::FieldType = converter
770
38
            .ccsds_days_as_u32()
771
38
            .try_into()
772
38
            .map_err(|_| CdsError::InvalidCcsdsDays(converter.ccsds_days_as_u32().into()))?;
773
36
        let mut provider = Self {
774
36
            pfield: Self::generate_p_field(days_len, converter.submillis_precision()),
775
36
            ccsds_days,
776
36
            ms_of_day: converter.ms_of_day(),
777
36
            unix_time: Default::default(),
778
36
            submillis: converter.submillis(),
779
36
        };
780
36
        provider.setup(converter.unix_days_seconds(), converter.ms_of_day());
781
36
        Ok(provider)
782
38
    }
783

            
784
    #[cfg(feature = "std")]
785
2
    fn generic_conversion_from_now(&self) -> Result<ConversionFromNow, SystemTimeError> {
786
2
        Ok(match self.submillis_precision() {
787
            SubmillisPrecision::Microseconds => ConversionFromNow::new_with_submillis_us_prec()?,
788
            SubmillisPrecision::Picoseconds => ConversionFromNow::new_with_submillis_ps_prec()?,
789
2
            _ => ConversionFromNow::new()?,
790
        })
791
2
    }
792

            
793
152
    fn generate_p_field(day_seg_len: LengthOfDaySegment, submillis_prec: SubmillisPrecision) -> u8 {
794
152
        let mut pfield = P_FIELD_BASE | ((day_seg_len as u8) << 2);
795
152
        match submillis_prec {
796
8
            SubmillisPrecision::Microseconds => pfield |= SubmillisPrecision::Microseconds as u8,
797
8
            SubmillisPrecision::Picoseconds => pfield |= SubmillisPrecision::Picoseconds as u8,
798
            SubmillisPrecision::Reserved => pfield |= SubmillisPrecision::Reserved as u8,
799
136
            _ => (),
800
        }
801
152
        pfield
802
152
    }
803

            
804
    #[cfg(feature = "std")]
805
2
    pub fn update_from_now(&mut self) -> Result<(), StdTimestampError> {
806
2
        let conversion_from_now = self.generic_conversion_from_now()?;
807
2
        let ccsds_days: ProvidesDaysLen::FieldType = conversion_from_now
808
2
            .unix_conversion
809
2
            .ccsds_days
810
2
            .try_into()
811
2
            .map_err(|_| {
812
                StdTimestampError::Timestamp(
813
                    CdsError::InvalidCcsdsDays(
814
                        conversion_from_now.unix_conversion.ccsds_days as i64,
815
                    )
816
                    .into(),
817
                )
818
2
            })?;
819
2
        self.ccsds_days = ccsds_days;
820
2
        self.ms_of_day = conversion_from_now.unix_conversion.ms_of_day;
821
2
        self.setup(
822
2
            conversion_from_now.unix_conversion.unix_days_seconds,
823
2
            conversion_from_now.unix_conversion.ms_of_day,
824
2
        );
825
2
        Ok(())
826
2
    }
827
}
828

            
829
impl CdsTime<DaysLen24Bits> {
830
    /// Generate a new timestamp provider with the days field width set to 24 bits
831
38
    pub fn new_with_u24_days(ccsds_days: u32, ms_of_day: u32) -> Result<Self, CdsError> {
832
38
        if ccsds_days > MAX_DAYS_24_BITS {
833
2
            return Err(CdsError::InvalidCcsdsDays(ccsds_days.into()));
834
36
        }
835
36
        Self::generic_new(LengthOfDaySegment::Long24Bits, ccsds_days, ms_of_day)
836
38
    }
837

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

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

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

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

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

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

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

            
898
8
    pub fn from_bytes_with_u24_days(buf: &[u8]) -> Result<Self, TimestampError> {
899
8
        let submillis_precision =
900
8
            Self::generic_raw_read_checks(buf, LengthOfDaySegment::Long24Bits)?;
901
8
        let mut temp_buf: [u8; 4] = [0; 4];
902
8
        temp_buf[1..4].copy_from_slice(&buf[1..4]);
903
8
        let cccsds_days: u32 = u32::from_be_bytes(temp_buf);
904
8
        let ms_of_day: u32 = u32::from_be_bytes(buf[4..8].try_into().unwrap());
905
8
        let mut provider = Self::new_with_u24_days(cccsds_days, ms_of_day)?;
906
8
        match submillis_precision {
907
2
            SubmillisPrecision::Microseconds => {
908
2
                provider.set_submillis(
909
2
                    SubmillisPrecision::Microseconds,
910
2
                    u16::from_be_bytes(buf[8..10].try_into().unwrap()) as u32,
911
2
                );
912
2
            }
913
2
            SubmillisPrecision::Picoseconds => {
914
2
                provider.set_submillis(
915
2
                    SubmillisPrecision::Picoseconds,
916
2
                    u32::from_be_bytes(buf[8..12].try_into().unwrap()),
917
2
                );
918
2
            }
919
4
            _ => (),
920
        }
921
8
        Ok(provider)
922
8
    }
923
}
924

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

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

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

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

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

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

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

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

            
990
26
    pub fn from_bytes_with_u16_days(buf: &[u8]) -> Result<Self, TimestampError> {
991
10
        let submillis_precision =
992
26
            Self::generic_raw_read_checks(buf, LengthOfDaySegment::Short16Bits)?;
993
10
        let ccsds_days: u16 = u16::from_be_bytes(buf[1..3].try_into().unwrap());
994
10
        let ms_of_day: u32 = u32::from_be_bytes(buf[3..7].try_into().unwrap());
995
10
        let mut provider = Self::new_with_u16_days(ccsds_days, ms_of_day);
996
10
        provider.pfield = buf[0];
997
10

            
998
10
        match submillis_precision {
999
4
            SubmillisPrecision::Microseconds => {
4
                provider.set_submillis(
4
                    SubmillisPrecision::Microseconds,
4
                    u16::from_be_bytes(buf[7..9].try_into().unwrap()) as u32,
4
                );
4
            }
2
            SubmillisPrecision::Picoseconds => {
2
                provider.set_submillis(
2
                    SubmillisPrecision::Picoseconds,
2
                    u32::from_be_bytes(buf[7..11].try_into().unwrap()),
2
                );
2
            }
4
            _ => (),
        }
10
        Ok(provider)
26
    }
}
24
fn add_for_max_ccsds_days_val<T: ProvidesDaysLength>(
24
    time_provider: &CdsTime<T>,
24
    max_days_val: u32,
24
    duration: Duration,
24
) -> (u32, u32, u32) {
24
    let mut next_ccsds_days = time_provider.ccsds_days_as_u32();
24
    let mut next_ms_of_day = time_provider.ms_of_day;
24
    // Increment CCSDS days by a certain amount while also accounting for overflow.
26
    let increment_days = |ccsds_days: &mut u32, days_inc: u32| {
26
        let days_addition: u64 = *ccsds_days as u64 + days_inc as u64;
26
        if days_addition > max_days_val as u64 {
            *ccsds_days = (days_addition - max_days_val as u64) as u32;
26
        } else {
26
            *ccsds_days += days_inc;
26
        }
26
    };
    // Increment MS of day by a certain amount while also accounting for overflow, where
    // the new value exceeds the MS of a day.
52
    let increment_ms_of_day = |ms_of_day: &mut u32, ms_inc: u32, ccsds_days: &mut u32| {
52
        *ms_of_day += ms_inc;
52
        if *ms_of_day >= MS_PER_DAY {
2
            *ms_of_day -= MS_PER_DAY;
2
            // Re-use existing closure to always amount for overflow.
2
            increment_days(ccsds_days, 1);
50
        }
52
    };
24
    let mut submillis = time_provider.submillis();
24
    match time_provider.submillis_precision() {
        SubmillisPrecision::Microseconds => {
6
            let subsec_micros = duration.subsec_micros();
6
            let subsec_millis = subsec_micros / 1000;
6
            let submilli_micros = subsec_micros % 1000;
6
            submillis += submilli_micros;
6
            if submillis >= 1000 {
2
                let carryover_us = submillis - 1000;
2
                increment_ms_of_day(&mut next_ms_of_day, 1, &mut next_ccsds_days);
2
                submillis = carryover_us;
4
            }
6
            increment_ms_of_day(&mut next_ms_of_day, subsec_millis, &mut next_ccsds_days);
        }
        SubmillisPrecision::Picoseconds => {
6
            let subsec_nanos = duration.subsec_nanos();
6
            let subsec_millis = subsec_nanos / 10_u32.pow(6);
6
            // 1 ms as ns is 1e6.
6
            let submilli_nanos = subsec_nanos % 10_u32.pow(6);
6
            // No overflow risk: The maximum value of an u32 is ~4.294e9, and one ms as ps
6
            // is 1e9. The amount ps can now have is always less than 2e9.
6
            submillis += submilli_nanos * 1000;
6
            if submillis >= 10_u32.pow(9) {
2
                let carry_over_ps = submillis - 10_u32.pow(9);
2
                increment_ms_of_day(&mut next_ms_of_day, 1, &mut next_ccsds_days);
2
                submillis = carry_over_ps;
4
            }
6
            increment_ms_of_day(&mut next_ms_of_day, subsec_millis, &mut next_ccsds_days);
        }
12
        _ => {
12
            increment_ms_of_day(
12
                &mut next_ms_of_day,
12
                duration.subsec_millis(),
12
                &mut next_ccsds_days,
12
            );
12
        }
    }
    // The subsecond millisecond were already handled.
24
    let full_seconds = duration.as_secs();
24
    let secs_of_day = (full_seconds % SECONDS_PER_DAY as u64) as u32;
24
    let ms_of_day = secs_of_day * 1000;
24
    increment_ms_of_day(&mut next_ms_of_day, ms_of_day, &mut next_ccsds_days);
24
    increment_days(
24
        &mut next_ccsds_days,
24
        (full_seconds as u32 - secs_of_day) / SECONDS_PER_DAY,
24
    );
24
    (next_ccsds_days, next_ms_of_day, submillis)
24
}
impl CdsTimestamp for CdsTime<DaysLen16Bits> {
2
    fn len_of_day_seg(&self) -> LengthOfDaySegment {
2
        LengthOfDaySegment::Short16Bits
2
    }
}
impl CdsTimestamp for CdsTime<DaysLen24Bits> {
2
    fn len_of_day_seg(&self) -> LengthOfDaySegment {
2
        LengthOfDaySegment::Long24Bits
2
    }
}
/// 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;
2
    fn add(self, duration: Duration) -> Self::Output {
2
        let (next_ccsds_days, next_ms_of_day, precision) =
2
            add_for_max_ccsds_days_val(&self, u16::MAX as u32, duration);
2
        let mut provider = Self::new_with_u16_days(next_ccsds_days as u16, next_ms_of_day);
2
        provider.set_submillis(self.submillis_precision(), precision);
2
        provider
2
    }
}
impl Add<Duration> for &CdsTime<DaysLen16Bits> {
    type Output = CdsTime<DaysLen16Bits>;
2
    fn add(self, duration: Duration) -> Self::Output {
2
        let (next_ccsds_days, next_ms_of_day, precision) =
2
            add_for_max_ccsds_days_val(self, u16::MAX as u32, duration);
2
        let mut provider = Self::Output::new_with_u16_days(next_ccsds_days as u16, next_ms_of_day);
2
        provider.set_submillis(self.submillis_precision(), precision);
2
        provider
2
    }
}
/// 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;
2
    fn add(self, duration: Duration) -> Self::Output {
2
        let (next_ccsds_days, next_ms_of_day, precision) =
2
            add_for_max_ccsds_days_val(&self, MAX_DAYS_24_BITS, duration);
2
        let mut provider = Self::new_with_u24_days(next_ccsds_days, next_ms_of_day).unwrap();
2
        provider.set_submillis(self.submillis_precision(), precision);
2
        provider
2
    }
}
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> {
16
    fn add_assign(&mut self, duration: Duration) {
16
        let (next_ccsds_days, next_ms_of_day, submillis) =
16
            add_for_max_ccsds_days_val(self, u16::MAX as u32, duration);
16
        self.ccsds_days = next_ccsds_days as u16;
16
        self.ms_of_day = next_ms_of_day;
16
        self.submillis = submillis;
16
    }
}
/// 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> {
2
    fn add_assign(&mut self, duration: Duration) {
2
        let (next_ccsds_days, next_ms_of_day, submillis) =
2
            add_for_max_ccsds_days_val(self, MAX_DAYS_24_BITS, duration);
2
        self.ccsds_days = next_ccsds_days;
2
        self.ms_of_day = next_ms_of_day;
2
        self.submillis = submillis;
2
    }
}
#[cfg(feature = "chrono")]
impl TryFrom<chrono::DateTime<chrono::Utc>> for CdsTime<DaysLen16Bits> {
    type Error = CdsError;
2
    fn try_from(dt: chrono::DateTime<chrono::Utc>) -> Result<Self, Self::Error> {
2
        let conversion = ConversionFromChronoDatetime::new(&dt)?;
2
        Self::generic_from_conversion(LengthOfDaySegment::Short16Bits, conversion)
2
    }
}
#[cfg(feature = "chrono")]
impl TryFrom<chrono::DateTime<chrono::Utc>> for CdsTime<DaysLen24Bits> {
    type Error = CdsError;
2
    fn try_from(dt: chrono::DateTime<chrono::Utc>) -> Result<Self, Self::Error> {
2
        let conversion = ConversionFromChronoDatetime::new(&dt)?;
2
        Self::generic_from_conversion(LengthOfDaySegment::Long24Bits, conversion)
2
    }
}
impl<ProvidesDaysLen: ProvidesDaysLength> CcsdsTimeProvider for CdsTime<ProvidesDaysLen> {
122
    fn len_as_bytes(&self) -> usize {
122
        Self::calc_stamp_len(self.pfield)
122
    }
2
    fn p_field(&self) -> (usize, [u8; 2]) {
2
        (1, [self.pfield, 0])
2
    }
6
    fn ccdsd_time_code(&self) -> CcsdsTimeCode {
6
        CcsdsTimeCode::Cds
6
    }
    #[inline]
30
    fn unix_secs(&self) -> i64 {
30
        self.unix_time.secs
30
    }
    #[inline]
40
    fn subsec_nanos(&self) -> u32 {
40
        self.unix_time.subsec_nanos
40
    }
    #[inline]
8
    fn unix_time(&self) -> UnixTime {
8
        self.unix_time
8
    }
}
impl TimeReader for CdsTime<DaysLen16Bits> {
22
    fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError> {
22
        Self::from_bytes_with_u16_days(buf)
22
    }
}
impl TimeReader for CdsTime<DaysLen24Bits> {
2
    fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError> {
2
        Self::from_bytes_with_u24_days(buf)
2
    }
}
impl TimeWriter for CdsTime<DaysLen16Bits> {
42
    fn write_to_bytes(&self, buf: &mut [u8]) -> Result<usize, TimestampError> {
42
        self.length_check(buf, self.len_as_bytes())?;
30
        buf[0] = self.pfield;
30
        buf[1..3].copy_from_slice(self.ccsds_days.to_be_bytes().as_slice());
30
        buf[3..7].copy_from_slice(self.ms_of_day.to_be_bytes().as_slice());
30
        match self.submillis_precision() {
6
            SubmillisPrecision::Microseconds => {
6
                buf[7..9].copy_from_slice((self.submillis() as u16).to_be_bytes().as_slice());
6
            }
4
            SubmillisPrecision::Picoseconds => {
4
                buf[7..11].copy_from_slice(self.submillis().to_be_bytes().as_slice());
4
            }
20
            _ => (),
        }
30
        Ok(self.len_as_bytes())
42
    }
2
    fn len_written(&self) -> usize {
2
        self.len_as_bytes()
2
    }
}
impl TimeWriter for CdsTime<DaysLen24Bits> {
16
    fn write_to_bytes(&self, buf: &mut [u8]) -> Result<usize, TimestampError> {
16
        self.length_check(buf, self.len_as_bytes())?;
16
        buf[0] = self.pfield;
16
        let be_days = self.ccsds_days.to_be_bytes();
16
        buf[1..4].copy_from_slice(&be_days[1..4]);
16
        buf[4..8].copy_from_slice(self.ms_of_day.to_be_bytes().as_slice());
16
        match self.submillis_precision() {
2
            SubmillisPrecision::Microseconds => {
2
                buf[8..10].copy_from_slice((self.submillis() as u16).to_be_bytes().as_slice());
2
            }
2
            SubmillisPrecision::Picoseconds => {
2
                buf[8..12].copy_from_slice(self.submillis().to_be_bytes().as_slice());
2
            }
12
            _ => (),
        }
16
        Ok(self.len_as_bytes())
16
    }
4
    fn len_written(&self) -> usize {
4
        self.len_as_bytes()
4
    }
}
impl<DaysLenProvider: ProvidesDaysLength> PartialEq for CdsTime<DaysLenProvider> {
24
    fn eq(&self, other: &Self) -> bool {
24
        if self.ccsds_days == other.ccsds_days
18
            && self.ms_of_day == other.ms_of_day
14
            && self.precision_as_ns().unwrap_or(0) == other.precision_as_ns().unwrap_or(0)
        {
12
            return true;
12
        }
12
        false
24
    }
}
impl<DaysLenProvider: ProvidesDaysLength> PartialOrd for CdsTime<DaysLenProvider> {
16
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
16
        if self == other {
4
            return Some(Ordering::Equal);
12
        }
12
        match self.ccsds_days_as_u32().cmp(&other.ccsds_days_as_u32()) {
            Ordering::Less => return Some(Ordering::Less),
6
            Ordering::Greater => return Some(Ordering::Greater),
6
            _ => (),
6
        }
6
        match self.ms_of_day().cmp(&other.ms_of_day()) {
            Ordering::Less => return Some(Ordering::Less),
4
            Ordering::Greater => return Some(Ordering::Greater),
2
            _ => (),
2
        }
2
        match self
2
            .precision_as_ns()
2
            .unwrap_or(0)
2
            .cmp(&other.precision_as_ns().unwrap_or(0))
        {
            Ordering::Less => return Some(Ordering::Less),
2
            Ordering::Greater => return Some(Ordering::Greater),
            _ => (),
        }
        Some(Ordering::Equal)
16
    }
}
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> {
2
    fn from(value: CdsTime<DaysLen16Bits>) -> Self {
2
        // This function only fails if the days value exceeds 24 bits, which is not possible here,
2
        // so it is okay to unwrap.
2
        Self::new_with_u24_days(value.ccsds_days_as_u32(), value.ms_of_day()).unwrap()
2
    }
}
/// This conversion can fail if the days value exceeds 16 bits.
impl TryFrom<CdsTime<DaysLen24Bits>> for CdsTime<DaysLen16Bits> {
    type Error = CdsError;
2
    fn try_from(value: CdsTime<DaysLen24Bits>) -> Result<Self, CdsError> {
2
        let ccsds_days = value.ccsds_days_as_u32();
2
        if ccsds_days > u16::MAX as u32 {
            return Err(CdsError::InvalidCcsdsDays(ccsds_days as i64));
2
        }
2
        Ok(Self::new_with_u16_days(
2
            ccsds_days as u16,
2
            value.ms_of_day(),
2
        ))
2
    }
}
#[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, ToSliceTooSmall};
    use alloc::string::ToString;
    use chrono::{Datelike, NaiveDate, Timelike};
    #[cfg(feature = "serde")]
    use postcard::{from_bytes, to_allocvec};
    use std::format;
    #[test]
2
    fn test_time_stamp_zero_args() {
2
        let time_stamper = CdsTime::new_with_u16_days(0, 0);
2
        let unix_stamp = time_stamper.unix_time();
2
        assert_eq!(
2
            unix_stamp.secs,
2
            (DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32) as i64
2
        );
2
        let subsecond_millis = unix_stamp.subsec_nanos;
2
        assert_eq!(subsecond_millis, 0);
2
        assert_eq!(
2
            time_stamper.submillis_precision(),
2
            SubmillisPrecision::Absent
2
        );
2
        assert_eq!(time_stamper.subsec_nanos(), 0);
2
        assert_eq!(time_stamper.ccdsd_time_code(), CcsdsTimeCode::Cds);
2
        assert_eq!(
2
            time_stamper.p_field(),
2
            (1, [(CcsdsTimeCode::Cds as u8) << 4, 0])
2
        );
2
        let date_time = time_stamper.chrono_date_time().unwrap();
2
        assert_eq!(date_time.year(), 1958);
2
        assert_eq!(date_time.month(), 1);
2
        assert_eq!(date_time.day(), 1);
2
        assert_eq!(date_time.hour(), 0);
2
        assert_eq!(date_time.minute(), 0);
2
        assert_eq!(date_time.second(), 0);
2
    }
    #[test]
2
    fn test_time_stamp_unix_epoch() {
2
        let time_stamper = CdsTime::new_with_u16_days((-DAYS_CCSDS_TO_UNIX) as u16, 0);
2
        assert_eq!(time_stamper.unix_time().secs, 0);
2
        assert_eq!(
2
            time_stamper.submillis_precision(),
2
            SubmillisPrecision::Absent
2
        );
2
        let date_time = time_stamper.chrono_date_time().unwrap();
2
        assert_eq!(date_time.year(), 1970);
2
        assert_eq!(date_time.month(), 1);
2
        assert_eq!(date_time.day(), 1);
2
        assert_eq!(date_time.hour(), 0);
2
        assert_eq!(date_time.minute(), 0);
2
        assert_eq!(date_time.second(), 0);
2
        let time_stamper = CdsTime::new_with_u16_days((-DAYS_CCSDS_TO_UNIX) as u16, 40);
2
        assert_eq!(time_stamper.subsec_nanos(), 40 * 10_u32.pow(6));
2
        assert_eq!(time_stamper.subsec_millis(), 40);
2
        let time_stamper = CdsTime::new_with_u16_days((-DAYS_CCSDS_TO_UNIX) as u16, 1040);
2
        assert_eq!(time_stamper.subsec_nanos(), 40 * 10_u32.pow(6));
2
        assert_eq!(time_stamper.subsec_millis(), 40);
2
    }
    #[test]
2
    fn test_large_days_field_write() {
2
        let time_stamper = CdsTime::new_with_u24_days(0x108020_u32, 0x10203040);
2
        assert!(time_stamper.is_ok());
2
        let time_stamper = time_stamper.unwrap();
2
        assert_eq!(time_stamper.len_as_bytes(), 8);
2
        let mut buf = [0; 16];
2
        let written = time_stamper.write_to_bytes(&mut buf);
2
        assert!(written.is_ok());
2
        let written = written.unwrap();
2
        assert_eq!(written, 8);
2
        assert_eq!(buf[1], 0x10);
2
        assert_eq!(buf[2], 0x80);
2
        assert_eq!(buf[3], 0x20);
2
        let ms = u32::from_be_bytes(buf[4..8].try_into().unwrap());
2
        assert_eq!(ms, 0x10203040);
2
        assert_eq!((buf[0] >> 2) & 0b1, 1);
2
    }
    #[test]
2
    fn test_large_days_field_read() {
2
        let time_stamper = CdsTime::new_with_u24_days(0x108020_u32, 0);
2
        assert!(time_stamper.is_ok());
2
        let time_stamper = time_stamper.unwrap();
2
        let mut buf = [0; 16];
2
        let written = time_stamper.write_to_bytes(&mut buf);
2
        assert!(written.is_ok());
2
        let provider = CdsTime::<DaysLen24Bits>::from_bytes(&buf);
2
        assert!(provider.is_ok());
2
        let provider = provider.unwrap();
2
        assert_eq!(provider.ccsds_days(), 0x108020);
2
        assert_eq!(provider.ms_of_day(), 0);
2
    }
    #[test]
2
    fn test_large_days_field_read_invalid_ctor() {
2
        let time_stamper = CdsTime::new_with_u24_days(0x108020, 0);
2
        assert!(time_stamper.is_ok());
2
        let time_stamper = time_stamper.unwrap();
2
        let mut buf = [0; 16];
2
        let written = time_stamper.write_to_bytes(&mut buf);
2
        assert!(written.is_ok());
2
        let faulty_ctor = CdsTime::<DaysLen16Bits>::from_bytes(&buf);
2
        assert!(faulty_ctor.is_err());
2
        let error = faulty_ctor.unwrap_err();
2
        if let TimestampError::Cds(CdsError::InvalidCtorForDaysOfLenInPreamble(len_of_day)) = error
        {
2
            assert_eq!(len_of_day, LengthOfDaySegment::Long24Bits);
        } else {
            panic!("Wrong error type");
        }
2
    }
    #[test]
2
    fn test_write() {
2
        let mut buf = [0; 16];
2
        let time_stamper_0 = CdsTime::new_with_u16_days(0, 0);
2
        let unix_stamp = time_stamper_0.unix_time();
2
        assert_eq!(
2
            unix_stamp.secs,
2
            (DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32).into()
2
        );
2
        let mut res = time_stamper_0.write_to_bytes(&mut buf);
2
        assert!(res.is_ok());
2
        assert_eq!(buf[0], (CcsdsTimeCode::Cds as u8) << 4);
2
        assert_eq!(
2
            u16::from_be_bytes(buf[1..3].try_into().expect("Byte conversion failed")),
2
            0
2
        );
2
        assert_eq!(
2
            u32::from_be_bytes(buf[3..7].try_into().expect("Byte conversion failed")),
2
            0
2
        );
2
        let time_stamper_1 = CdsTime::new_with_u16_days(u16::MAX - 1, u32::MAX - 1);
2
        res = time_stamper_1.write_to_bytes(&mut buf);
2
        assert!(res.is_ok());
2
        assert_eq!(buf[0], (CcsdsTimeCode::Cds as u8) << 4);
2
        assert_eq!(
2
            u16::from_be_bytes(buf[1..3].try_into().expect("Byte conversion failed")),
2
            u16::MAX - 1
2
        );
2
        assert_eq!(
2
            u32::from_be_bytes(buf[3..7].try_into().expect("Byte conversion failed")),
2
            u32::MAX - 1
2
        );
2
    }
    #[test]
2
    fn test_faulty_write_buf_too_small() {
2
        let mut buf = [0; 7];
2
        let time_stamper = CdsTime::new_with_u16_days(u16::MAX - 1, u32::MAX - 1);
14
        for i in 0..6 {
12
            let res = time_stamper.write_to_bytes(&mut buf[0..i]);
12
            assert!(res.is_err());
12
            let error = res.unwrap_err();
12
            match error {
12
                ByteConversion(ToSliceTooSmall { found, expected }) => {
12
                    assert_eq!(found, i);
12
                    assert_eq!(expected, 7);
12
                    assert_eq!(
12
                        error.to_string(),
12
                        format!("time stamp: target slice with size {i} is too small, expected size of at least 7")
12
                    );
                }
                _ => panic!(
                    "{}",
                    format!("Invalid error {:?} detected", res.unwrap_err())
                ),
            }
        }
2
    }
    #[test]
2
    fn test_faulty_read_buf_too_small() {
2
        let buf = [0; 7];
14
        for i in 0..6 {
12
            let res = CdsTime::<DaysLen16Bits>::from_bytes(&buf[0..i]);
12
            assert!(res.is_err());
12
            let err = res.unwrap_err();
12
            match err {
12
                ByteConversion(e) => match e {
12
                    FromSliceTooSmall { found, expected } => {
12
                        assert_eq!(found, i);
12
                        assert_eq!(expected, 7);
                    }
                    _ => panic!("{}", format!("Invalid error {:?} detected", e)),
                },
                _ => {
                    panic!("Unexpected error {:?}", err);
                }
            }
        }
2
    }
    #[test]
2
    fn test_faulty_invalid_pfield() {
2
        let mut buf = [0; 16];
2
        let time_stamper_0 = CdsTime::new_with_u16_days(0, 0);
2
        let res = time_stamper_0.write_to_bytes(&mut buf);
2
        assert!(res.is_ok());
2
        buf[0] = 0;
2
        let res = CdsTime::<DaysLen16Bits>::from_bytes(&buf);
2
        assert!(res.is_err());
2
        let err = res.unwrap_err();
2
        if let InvalidTimeCode { expected, found } = err {
2
            assert_eq!(expected, CcsdsTimeCode::Cds);
2
            assert_eq!(found, 0);
2
            assert_eq!(
2
                err.to_string(),
2
                "invalid raw time code value 0 for time code Cds"
2
            );
        }
2
    }
    #[test]
2
    fn test_reading() {
2
        let mut buf = [0; 16];
2
        let time_stamper = CdsTime::new_with_u16_days(u16::MAX - 1, u32::MAX - 1);
2
        let res = time_stamper.write_to_bytes(&mut buf);
2
        assert!(res.is_ok());
2
        assert_eq!(buf[0], (CcsdsTimeCode::Cds as u8) << 4);
2
        assert_eq!(
2
            u16::from_be_bytes(buf[1..3].try_into().expect("Byte conversion failed")),
2
            u16::MAX - 1
2
        );
2
        assert_eq!(
2
            u32::from_be_bytes(buf[3..7].try_into().expect("Byte conversion failed")),
2
            u32::MAX - 1
2
        );
2
        let read_stamp: CdsTime<DaysLen16Bits> =
2
            CdsTime::from_bytes(&buf).expect("Reading timestamp failed");
2
        assert_eq!(read_stamp.ccsds_days(), u16::MAX - 1);
2
        assert_eq!(read_stamp.ms_of_day(), u32::MAX - 1);
2
    }
10
    fn generic_now_test<T: ProvidesDaysLength>(
10
        timestamp_now: CdsTime<T>,
10
        compare_stamp: chrono::DateTime<chrono::Utc>,
10
    ) {
10
        let dt = timestamp_now.chrono_date_time().unwrap();
10
        if compare_stamp.year() > dt.year() {
            assert_eq!(compare_stamp.year() - dt.year(), 1);
        } else {
10
            assert_eq!(dt.year(), compare_stamp.year());
        }
10
        generic_dt_property_equality_check(dt.month(), compare_stamp.month(), 1, 12);
10

            
10
        assert_eq!(dt.day(), compare_stamp.day());
10
        if compare_stamp.day() < dt.day() {
            assert!(dt.day() >= 28);
            assert_eq!(compare_stamp.day(), 1);
10
        } else if compare_stamp.day() > dt.day() {
            assert_eq!(compare_stamp.day() - dt.day(), 1);
        } else {
10
            assert_eq!(compare_stamp.day(), dt.day());
        }
10
        generic_dt_property_equality_check(dt.hour(), compare_stamp.hour(), 0, 23);
10
        generic_dt_property_equality_check(dt.minute(), compare_stamp.minute(), 0, 59);
10
    }
    #[test]
    #[cfg_attr(miri, ignore)]
2
    fn test_time_now() {
2
        let timestamp_now = CdsTime::now_with_u16_days().unwrap();
2
        let compare_stamp = chrono::Utc::now();
2
        generic_now_test(timestamp_now, compare_stamp);
2
    }
    #[test]
    #[cfg_attr(miri, ignore)]
2
    fn test_time_now_us_prec() {
2
        let timestamp_now = CdsTime::now_with_u16_days_us_precision().unwrap();
2
        let compare_stamp = chrono::Utc::now();
2
        generic_now_test(timestamp_now, compare_stamp);
2
    }
    #[test]
    #[cfg_attr(miri, ignore)]
2
    fn test_time_now_ps_prec() {
2
        let timestamp_now = CdsTime::from_now_with_u16_days_ps_precision().unwrap();
2
        let compare_stamp = chrono::Utc::now();
2
        generic_now_test(timestamp_now, compare_stamp);
2
    }
    #[test]
    #[cfg_attr(miri, ignore)]
2
    fn test_time_now_ps_prec_u16_days() {
2
        let timestamp_now = CdsTime::from_now_with_u16_days_ps_precision().unwrap();
2
        let compare_stamp = chrono::Utc::now();
2
        generic_now_test(timestamp_now, compare_stamp);
2
    }
    #[test]
    #[cfg_attr(miri, ignore)]
2
    fn test_time_now_ps_prec_u24_days() {
2
        let timestamp_now = CdsTime::now_with_u24_days_ps_precision().unwrap();
2
        let compare_stamp = chrono::Utc::now();
2
        generic_now_test(timestamp_now, compare_stamp);
2
    }
    #[test]
2
    fn test_submillis_precision_micros() {
2
        let mut time_stamper = CdsTime::new_with_u16_days(0, 0);
2
        time_stamper.set_submillis(SubmillisPrecision::Microseconds, 500);
2
        assert_eq!(
2
            time_stamper.submillis_precision(),
2
            SubmillisPrecision::Microseconds
2
        );
2
        assert_eq!(time_stamper.submillis(), 500);
2
        let mut write_buf: [u8; 16] = [0; 16];
2
        let written = time_stamper
2
            .write_to_bytes(&mut write_buf)
2
            .expect("Writing timestamp failed");
2
        assert_eq!(written, 9);
2
        let cross_check: u16 = 500;
2
        assert_eq!(write_buf[7..9], cross_check.to_be_bytes());
2
    }
    #[test]
2
    fn test_submillis_precision_picos() {
2
        let mut time_stamper = CdsTime::new_with_u16_days(0, 0);
2
        time_stamper.set_submillis(SubmillisPrecision::Picoseconds, 5e8 as u32);
2
        assert_eq!(
2
            time_stamper.submillis_precision(),
2
            SubmillisPrecision::Picoseconds
2
        );
2
        assert_eq!(time_stamper.submillis(), 5e8 as u32);
2
        let mut write_buf: [u8; 16] = [0; 16];
2
        let written = time_stamper
2
            .write_to_bytes(&mut write_buf)
2
            .expect("Writing timestamp failed");
2
        assert_eq!(written, 11);
2
        let cross_check: u32 = 5e8 as u32;
2
        assert_eq!(write_buf[7..11], cross_check.to_be_bytes());
2
    }
    #[test]
2
    fn read_stamp_with_ps_submillis_precision() {
2
        let mut time_stamper = CdsTime::new_with_u16_days(0, 0);
2
        time_stamper.set_submillis(SubmillisPrecision::Picoseconds, 5e8 as u32);
2
        let mut write_buf: [u8; 16] = [0; 16];
2
        let written = time_stamper
2
            .write_to_bytes(&mut write_buf)
2
            .expect("Writing timestamp failed");
2
        assert_eq!(written, 11);
2
        let stamp_deserialized = CdsTime::<DaysLen16Bits>::from_bytes(&write_buf);
2
        assert!(stamp_deserialized.is_ok());
2
        let stamp_deserialized = stamp_deserialized.unwrap();
2
        assert_eq!(stamp_deserialized.len_as_bytes(), 11);
2
        assert_eq!(
2
            stamp_deserialized.submillis_precision(),
2
            SubmillisPrecision::Picoseconds
2
        );
2
        assert_eq!(stamp_deserialized.submillis(), 5e8 as u32);
2
    }
    #[test]
2
    fn read_stamp_with_us_submillis_precision() {
2
        let mut time_stamper = CdsTime::new_with_u16_days(0, 0);
2
        time_stamper.set_submillis(SubmillisPrecision::Microseconds, 500);
2
        let mut write_buf: [u8; 16] = [0; 16];
2
        let written = time_stamper
2
            .write_to_bytes(&mut write_buf)
2
            .expect("Writing timestamp failed");
2
        assert_eq!(written, 9);
2
        let stamp_deserialized = CdsTime::<DaysLen16Bits>::from_bytes(&write_buf);
2
        assert!(stamp_deserialized.is_ok());
2
        let stamp_deserialized = stamp_deserialized.unwrap();
2
        assert_eq!(stamp_deserialized.len_as_bytes(), 9);
2
        assert_eq!(
2
            stamp_deserialized.submillis_precision(),
2
            SubmillisPrecision::Microseconds
2
        );
2
        assert_eq!(stamp_deserialized.submillis(), 500);
2
    }
    #[test]
2
    fn read_u24_stamp_with_us_submillis_precision() {
2
        let mut time_stamper = CdsTime::new_with_u24_days(u16::MAX as u32 + 1, 0).unwrap();
2
        time_stamper.set_submillis(SubmillisPrecision::Microseconds, 500);
2
        let mut write_buf: [u8; 16] = [0; 16];
2
        let written = time_stamper
2
            .write_to_bytes(&mut write_buf)
2
            .expect("Writing timestamp failed");
2
        // 1 byte pfield + 3 bytes days + 4 bytes ms of day + 2 bytes us precision
2
        assert_eq!(written, 10);
2
        let stamp_deserialized = CdsTime::from_bytes_with_u24_days(&write_buf);
2
        assert!(stamp_deserialized.is_ok());
2
        let stamp_deserialized = stamp_deserialized.unwrap();
2
        assert_eq!(stamp_deserialized.len_as_bytes(), 10);
2
        assert_eq!(stamp_deserialized.ccsds_days(), u16::MAX as u32 + 1);
2
        assert_eq!(
2
            stamp_deserialized.submillis_precision(),
2
            SubmillisPrecision::Microseconds
2
        );
2
        assert_eq!(stamp_deserialized.submillis(), 500);
2
    }
    #[test]
2
    fn read_u24_stamp_with_ps_submillis_precision() {
2
        let mut time_stamper = CdsTime::new_with_u24_days(u16::MAX as u32 + 1, 0).unwrap();
2
        time_stamper.set_submillis(SubmillisPrecision::Picoseconds, 5e8 as u32);
2
        let mut write_buf: [u8; 16] = [0; 16];
2
        let written = time_stamper
2
            .write_to_bytes(&mut write_buf)
2
            .expect("Writing timestamp failed");
2
        // 1 byte pfield + 3 bytes days + 4 bytes ms of day + 4 bytes us precision
2
        assert_eq!(written, 12);
2
        let stamp_deserialized = CdsTime::from_bytes_with_u24_days(&write_buf);
2
        assert!(stamp_deserialized.is_ok());
2
        let stamp_deserialized = stamp_deserialized.unwrap();
2
        assert_eq!(stamp_deserialized.len_as_bytes(), 12);
2
        assert_eq!(stamp_deserialized.ccsds_days(), u16::MAX as u32 + 1);
2
        assert_eq!(
2
            stamp_deserialized.submillis_precision(),
2
            SubmillisPrecision::Picoseconds
2
        );
2
        assert_eq!(stamp_deserialized.submillis(), 5e8 as u32);
2
    }
4
    fn generic_dt_case_0_no_prec(subsec_millis: u32) -> chrono::DateTime<chrono::Utc> {
4
        NaiveDate::from_ymd_opt(2023, 1, 14)
4
            .unwrap()
4
            .and_hms_milli_opt(16, 49, 30, subsec_millis)
4
            .unwrap()
4
            .and_local_timezone(chrono::Utc)
4
            .unwrap()
4
    }
4
    fn generic_check_dt_case_0<DaysLen: ProvidesDaysLength>(
4
        time_provider: &CdsTime<DaysLen>,
4
        subsec_millis: u32,
4
        datetime_utc: chrono::DateTime<chrono::Utc>,
4
    ) {
4
        // https://www.timeanddate.com/date/durationresult.html?d1=01&m1=01&y1=1958&d2=14&m2=01&y2=2023
4
        // Leap years need to be accounted for as well.
4
        assert_eq!(time_provider.ccsds_days, 23754.into());
4
        assert_eq!(
4
            time_provider.ms_of_day,
4
            30 * 1000 + 49 * 60 * 1000 + 16 * 60 * 60 * 1000 + subsec_millis
4
        );
4
        assert_eq!(time_provider.chrono_date_time().unwrap(), datetime_utc);
4
    }
    #[test]
2
    fn test_creation_from_dt_u16_days() {
2
        let subsec_millis = 250;
2
        let datetime_utc = generic_dt_case_0_no_prec(subsec_millis);
2
        let time_provider = CdsTime::from_dt_with_u16_days(&datetime_utc).unwrap();
2
        generic_check_dt_case_0(&time_provider, subsec_millis, datetime_utc);
2
        let time_provider_2: CdsTime<DaysLen16Bits> =
2
            datetime_utc.try_into().expect("conversion failed");
2
        // Test the TryInto trait impl
2
        assert_eq!(time_provider, time_provider_2);
2
    }
    #[test]
2
    fn test_creation_from_dt_u24_days() {
2
        let subsec_millis = 250;
2
        let datetime_utc = generic_dt_case_0_no_prec(subsec_millis);
2
        let time_provider = CdsTime::from_dt_with_u24_days(&datetime_utc).unwrap();
2
        generic_check_dt_case_0(&time_provider, subsec_millis, datetime_utc);
2
        let time_provider_2: CdsTime<DaysLen24Bits> =
2
            datetime_utc.try_into().expect("conversion failed");
2
        // Test the TryInto trait impl
2
        assert_eq!(time_provider, time_provider_2);
2
    }
4
    fn generic_dt_case_1_us_prec(subsec_millis: u32) -> chrono::DateTime<chrono::Utc> {
4
        // 250 ms + 500 us
4
        let subsec_micros = subsec_millis * 1000 + 500;
4
        NaiveDate::from_ymd_opt(2023, 1, 14)
4
            .unwrap()
4
            .and_hms_micro_opt(16, 49, 30, subsec_micros)
4
            .unwrap()
4
            .and_local_timezone(chrono::Utc)
4
            .unwrap()
4
    }
4
    fn generic_check_dt_case_1_us_prec<DaysLen: ProvidesDaysLength>(
4
        time_provider: &CdsTime<DaysLen>,
4
        subsec_millis: u32,
4
        datetime_utc: chrono::DateTime<chrono::Utc>,
4
    ) {
4
        // https://www.timeanddate.com/date/durationresult.html?d1=01&m1=01&y1=1958&d2=14&m2=01&y2=2023
4
        // Leap years need to be accounted for as well.
4
        assert_eq!(time_provider.ccsds_days, 23754.into());
4
        assert_eq!(
4
            time_provider.ms_of_day,
4
            30 * 1000 + 49 * 60 * 1000 + 16 * 60 * 60 * 1000 + subsec_millis
4
        );
4
        assert_eq!(
4
            time_provider.submillis_precision(),
4
            SubmillisPrecision::Microseconds
4
        );
4
        assert_eq!(time_provider.submillis(), 500);
4
        assert_eq!(time_provider.chrono_date_time().unwrap(), datetime_utc);
4
    }
    #[test]
2
    fn test_creation_from_dt_u16_days_us_prec() {
2
        let subsec_millis = 250;
2
        let datetime_utc = generic_dt_case_1_us_prec(subsec_millis);
2
        let time_provider = CdsTime::from_dt_with_u16_days_us_precision(&datetime_utc).unwrap();
2
        generic_check_dt_case_1_us_prec(&time_provider, subsec_millis, datetime_utc);
2
    }
    #[test]
2
    fn test_creation_from_dt_u24_days_us_prec() {
2
        let subsec_millis = 250;
2
        let datetime_utc = generic_dt_case_1_us_prec(subsec_millis);
2
        let time_provider = CdsTime::from_dt_with_u24_days_us_precision(&datetime_utc).unwrap();
2
        generic_check_dt_case_1_us_prec(&time_provider, subsec_millis, datetime_utc);
2
    }
4
    fn generic_dt_case_2_ps_prec(subsec_millis: u32) -> (chrono::DateTime<chrono::Utc>, u32) {
4
        // 250 ms + 500 us
4
        let subsec_nanos = subsec_millis * 1000 * 1000 + 500 * 1000;
4
        let submilli_nanos = subsec_nanos % 10_u32.pow(6);
4
        (
4
            NaiveDate::from_ymd_opt(2023, 1, 14)
4
                .unwrap()
4
                .and_hms_nano_opt(16, 49, 30, subsec_nanos)
4
                .unwrap()
4
                .and_local_timezone(chrono::Utc)
4
                .unwrap(),
4
            submilli_nanos,
4
        )
4
    }
4
    fn generic_check_dt_case_2_ps_prec<DaysLen: ProvidesDaysLength>(
4
        time_provider: &CdsTime<DaysLen>,
4
        subsec_millis: u32,
4
        submilli_nanos: u32,
4
        datetime_utc: chrono::DateTime<chrono::Utc>,
4
    ) {
4
        // https://www.timeanddate.com/date/durationresult.html?d1=01&m1=01&y1=1958&d2=14&m2=01&y2=2023
4
        // Leap years need to be accounted for as well.
4
        assert_eq!(time_provider.ccsds_days, 23754.into());
4
        assert_eq!(
4
            time_provider.ms_of_day,
4
            30 * 1000 + 49 * 60 * 1000 + 16 * 60 * 60 * 1000 + subsec_millis
4
        );
4
        assert_eq!(
4
            time_provider.submillis_precision(),
4
            SubmillisPrecision::Picoseconds
4
        );
4
        assert_eq!(time_provider.submillis(), submilli_nanos * 1000);
4
        assert_eq!(time_provider.chrono_date_time().unwrap(), datetime_utc);
4
    }
    #[test]
2
    fn test_creation_from_dt_u16_days_ps_prec() {
2
        let subsec_millis = 250;
2
        let (datetime_utc, submilli_nanos) = generic_dt_case_2_ps_prec(subsec_millis);
2
        let time_provider = CdsTime::from_dt_with_u16_days_ps_precision(&datetime_utc).unwrap();
2
        generic_check_dt_case_2_ps_prec(
2
            &time_provider,
2
            subsec_millis,
2
            submilli_nanos,
2
            datetime_utc,
2
        );
2
    }
    #[test]
2
    fn test_creation_from_dt_u24_days_ps_prec() {
2
        let subsec_millis = 250;
2
        let (datetime_utc, submilli_nanos) = generic_dt_case_2_ps_prec(subsec_millis);
2
        let time_provider = CdsTime::from_dt_with_u24_days_ps_precision(&datetime_utc).unwrap();
2
        generic_check_dt_case_2_ps_prec(
2
            &time_provider,
2
            subsec_millis,
2
            submilli_nanos,
2
            datetime_utc,
2
        );
2
    }
    #[test]
2
    fn test_creation_from_unix_stamp_0_u16_days() {
2
        let unix_secs = 0;
2
        let subsec_millis = 0;
2
        let time_provider = CdsTime::from_unix_time_with_u16_days(
2
            &UnixTime::new(unix_secs, subsec_millis),
2
            SubmillisPrecision::Absent,
2
        )
2
        .expect("creating provider from unix stamp failed");
2
        assert_eq!(time_provider.ccsds_days, -DAYS_CCSDS_TO_UNIX as u16)
2
    }
    #[test]
2
    fn test_creation_from_unix_stamp_0_u24_days() {
2
        let unix_secs = 0;
2
        let subsec_millis = 0;
2
        let time_provider = CdsTime::from_unix_time_with_u24_day(
2
            &UnixTime::new(unix_secs, subsec_millis),
2
            SubmillisPrecision::Absent,
2
        )
2
        .expect("creating provider from unix stamp failed");
2
        assert_eq!(time_provider.ccsds_days, (-DAYS_CCSDS_TO_UNIX) as u32)
2
    }
    #[test]
2
    fn test_creation_from_unix_stamp_1() {
2
        let subsec_millis = 250;
2
        let datetime_utc = NaiveDate::from_ymd_opt(2023, 1, 14)
2
            .unwrap()
2
            .and_hms_milli_opt(16, 49, 30, subsec_millis)
2
            .unwrap()
2
            .and_local_timezone(chrono::Utc)
2
            .unwrap();
2
        let time_provider =
2
            CdsTime::from_unix_time_with_u16_days(&datetime_utc.into(), SubmillisPrecision::Absent)
2
                .expect("creating provider from unix stamp failed");
2
        // https://www.timeanddate.com/date/durationresult.html?d1=01&m1=01&y1=1958&d2=14&m2=01&y2=2023
2
        // Leap years need to be accounted for as well.
2
        assert_eq!(time_provider.ccsds_days, 23754);
2
        assert_eq!(
2
            time_provider.ms_of_day,
2
            30 * 1000 + 49 * 60 * 1000 + 16 * 60 * 60 * 1000 + subsec_millis
2
        );
2
        let dt_back = time_provider.chrono_date_time().unwrap();
2
        assert_eq!(datetime_utc, dt_back);
2
    }
    #[test]
2
    fn test_creation_0_ccsds_days() {
2
        let unix_secs = DAYS_CCSDS_TO_UNIX as i64 * SECONDS_PER_DAY as i64;
2
        let subsec_millis = 0;
2
        let time_provider = CdsTime::from_unix_time_with_u16_days(
2
            &UnixTime::new(unix_secs, subsec_millis),
2
            SubmillisPrecision::Absent,
2
        )
2
        .expect("creating provider from unix stamp failed");
2
        assert_eq!(time_provider.ccsds_days, 0)
2
    }
    #[test]
2
    fn test_invalid_creation_from_unix_stamp_days_too_large() {
2
        let invalid_unix_secs: i64 = (u16::MAX as i64 + 1) * SECONDS_PER_DAY as i64;
2
        let subsec_millis = 0;
2
        match CdsTime::from_unix_time_with_u16_days(
2
            &UnixTime::new(invalid_unix_secs, subsec_millis),
2
            SubmillisPrecision::Absent,
2
        ) {
            Ok(_) => {
                panic!("creation should not succeed")
            }
2
            Err(e) => {
2
                if let CdsError::InvalidCcsdsDays(days) = e {
2
                    assert_eq!(
2
                        days,
2
                        unix_to_ccsds_days(invalid_unix_secs / SECONDS_PER_DAY as i64)
2
                    );
2
                    assert_eq!(e.to_string(), "invalid ccsds days 69919");
                } else {
                    panic!("unexpected error {}", e)
                }
            }
        }
2
    }
    #[test]
2
    fn test_invalid_creation_from_unix_stamp_before_ccsds_epoch() {
2
        // This is a unix stamp before the CCSDS epoch (01-01-1958 00:00:00), this should be
2
        // precisely 31-12-1957 23:59:55
2
        let unix_secs = DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32 - 5;
2
        let subsec_millis = 0;
2
        match CdsTime::from_unix_time_with_u16_days(
2
            &UnixTime::new(unix_secs as i64, subsec_millis),
2
            SubmillisPrecision::Absent,
2
        ) {
            Ok(_) => {
                panic!("creation should not succeed")
            }
2
            Err(e) => {
2
                if let CdsError::DateBeforeCcsdsEpoch(DateBeforeCcsdsEpochError(unix_dt)) = e {
2
                    let dt = unix_dt.chrono_date_time();
2
                    if let chrono::LocalResult::Single(dt) = dt {
2
                        assert_eq!(dt.year(), 1957);
2
                        assert_eq!(dt.month(), 12);
2
                        assert_eq!(dt.day(), 31);
2
                        assert_eq!(dt.hour(), 23);
2
                        assert_eq!(dt.minute(), 59);
2
                        assert_eq!(dt.second(), 55);
                    } else {
                        panic!("unexpected error {}", e)
                    }
                } else {
                    panic!("unexpected error {}", e)
                }
            }
        }
2
    }
    #[test]
2
    fn test_addition_u16_days_day_increment() {
2
        let mut provider = CdsTime::new_with_u16_days(0, MS_PER_DAY - 5 * 1000);
2
        let seconds_offset = Duration::from_secs(10);
2
        assert_eq!(provider.ccsds_days, 0);
2
        assert_eq!(provider.ms_of_day, MS_PER_DAY - 5 * 1000);
2
        provider += seconds_offset;
2
        assert_eq!(provider.ccsds_days, 1);
2
        assert_eq!(provider.ms_of_day, 5000);
2
    }
    #[test]
2
    fn test_addition_u16_days() {
2
        let mut provider = CdsTime::new_with_u16_days(0, 0);
2
        let seconds_offset = Duration::from_secs(5);
2
        assert_eq!(provider.ccsds_days, 0);
2
        assert_eq!(provider.ms_of_day, 0);
2
        provider += seconds_offset;
2
        assert_eq!(provider.ms_of_day, 5000);
        // Add one day and test Add operator
2
        let provider2 = provider + Duration::from_secs(60 * 60 * 24);
2
        assert_eq!(provider2.ccsds_days, 1);
2
        assert_eq!(provider2.ms_of_day, 5000);
2
    }
    #[test]
2
    fn test_addition_u24_days() {
2
        let mut provider = CdsTime::new_with_u24_days(u16::MAX as u32, 0).unwrap();
2
        let seconds_offset = Duration::from_secs(5);
2
        assert_eq!(provider.ccsds_days, u16::MAX as u32);
2
        assert_eq!(provider.ms_of_day, 0);
2
        provider += seconds_offset;
2
        assert_eq!(provider.ms_of_day, 5000);
        // Add one day and test Add operator
2
        let provider2 = provider + Duration::from_secs(60 * 60 * 24);
2
        assert_eq!(provider2.ccsds_days, u16::MAX as u32 + 1);
2
        assert_eq!(provider2.ms_of_day, 5000);
2
    }
    #[test]
2
    fn test_dyn_creation_u24_days() {
2
        let stamp = CdsTime::new_with_u24_days(u16::MAX as u32 + 1, 24).unwrap();
2
        let mut buf: [u8; 32] = [0; 32];
2
        stamp.write_to_bytes(&mut buf).unwrap();
2
        let dyn_provider = get_dyn_time_provider_from_bytes(&buf);
2
        assert!(dyn_provider.is_ok());
2
        let dyn_provider = dyn_provider.unwrap();
2
        assert_eq!(dyn_provider.ccdsd_time_code(), CcsdsTimeCode::Cds);
2
        assert_eq!(dyn_provider.ccsds_days_as_u32(), u16::MAX as u32 + 1);
2
        assert_eq!(dyn_provider.ms_of_day(), 24);
2
        assert_eq!(
2
            dyn_provider.submillis_precision(),
2
            SubmillisPrecision::Absent
2
        );
2
        assert_eq!(
2
            dyn_provider.len_of_day_seg(),
2
            LengthOfDaySegment::Long24Bits
2
        );
2
    }
    #[test]
2
    fn test_addition_with_us_precision_u16_days() {
2
        let mut provider = CdsTime::new_with_u16_days(0, 0);
2
        provider.set_submillis(SubmillisPrecision::Microseconds, 0);
2
        let duration = Duration::from_micros(500);
2
        provider += duration;
2
        assert_eq!(
2
            provider.submillis_precision(),
2
            SubmillisPrecision::Microseconds
2
        );
2
        assert_eq!(provider.submillis(), 500);
2
    }
    #[test]
2
    fn test_addition_with_us_precision_u16_days_with_subsec_millis() {
2
        let mut provider = CdsTime::new_with_u16_days(0, 0);
2
        provider.set_submillis(SubmillisPrecision::Microseconds, 0);
2
        let duration = Duration::from_micros(1200);
2
        provider += duration;
2
        assert_eq!(
2
            provider.submillis_precision(),
2
            SubmillisPrecision::Microseconds
2
        );
2
        assert_eq!(provider.submillis(), 200);
2
        assert_eq!(provider.ms_of_day(), 1);
2
    }
    #[test]
2
    fn test_addition_with_us_precision_u16_days_carry_over() {
2
        let mut provider = CdsTime::new_with_u16_days(0, 0);
2
        provider.set_submillis(SubmillisPrecision::Microseconds, 800);
2
        let duration = Duration::from_micros(400);
2
        provider += duration;
2

            
2
        assert_eq!(
2
            provider.submillis_precision(),
2
            SubmillisPrecision::Microseconds
2
        );
2
        assert_eq!(provider.submillis(), 200);
2
        assert_eq!(provider.ms_of_day(), 1);
2
    }
    #[test]
2
    fn test_addition_with_ps_precision_u16_days() {
2
        let mut provider = CdsTime::new_with_u16_days(0, 0);
2
        provider.set_submillis(SubmillisPrecision::Picoseconds, 0);
2
        // 500 us as ns
2
        let duration = Duration::from_nanos(500 * 10u32.pow(3) as u64);
2
        provider += duration;
2

            
2
        assert_eq!(
2
            provider.submillis_precision(),
2
            SubmillisPrecision::Picoseconds
2
        );
2
        assert_eq!(provider.submillis(), 500 * 10u32.pow(6));
2
    }
    #[test]
2
    fn test_addition_on_ref() {
2
        // This test case also tests the case where there is no submillis precision but subsecond
2
        // milliseconds.
2
        let provider_ref = &CdsTime::new_with_u16_days(2, 500);
2
        let new_stamp = provider_ref + Duration::from_millis(2 * 24 * 60 * 60 * 1000 + 500);
2
        assert_eq!(new_stamp.ccsds_days_as_u32(), 4);
2
        assert_eq!(new_stamp.ms_of_day, 1000);
2
    }
4
    fn check_ps_and_carryover(prec: SubmillisPrecision, submillis: u32, ms_of_day: u32, val: u32) {
4
        if prec == SubmillisPrecision::Picoseconds {
4
            assert_eq!(submillis, val);
4
            assert_eq!(ms_of_day, 1);
        } else {
            panic!("invalid precision {:?}", prec)
        }
4
    }
    #[test]
2
    fn test_addition_with_ps_precision_u16_days_with_subsec_millis() {
2
        let mut provider = CdsTime::new_with_u16_days(0, 0);
2
        provider.set_submillis(SubmillisPrecision::Picoseconds, 0);
2
        // 1200 us as ns
2
        let duration = Duration::from_nanos(1200 * 10u32.pow(3) as u64);
2
        provider += duration;
2
        check_ps_and_carryover(
2
            provider.submillis_precision(),
2
            provider.submillis(),
2
            provider.ms_of_day,
2
            200 * 10_u32.pow(6),
2
        );
2
    }
    #[test]
2
    fn test_addition_with_ps_precision_u16_days_carryover() {
2
        let mut provider = CdsTime::new_with_u16_days(0, 0);
2
        // 800 us as ps
2
        provider.set_submillis(SubmillisPrecision::Picoseconds, 800 * 10_u32.pow(6));
2
        // 400 us as ns
2
        let duration = Duration::from_nanos(400 * 10u32.pow(3) as u64);
2
        provider += duration;
2
        check_ps_and_carryover(
2
            provider.submillis_precision(),
2
            provider.submillis(),
2
            provider.ms_of_day,
2
            200 * 10_u32.pow(6),
2
        );
2
    }
    #[test]
2
    fn test_dyn_creation_u16_days_with_precision() {
2
        let mut stamp = CdsTime::new_with_u16_days(24, 24);
2
        stamp.set_submillis(SubmillisPrecision::Microseconds, 666);
2
        let mut buf: [u8; 32] = [0; 32];
2
        stamp.write_to_bytes(&mut buf).unwrap();
2
        let dyn_provider = get_dyn_time_provider_from_bytes(&buf);
2
        assert!(dyn_provider.is_ok());
2
        let dyn_provider = dyn_provider.unwrap();
2
        assert_eq!(dyn_provider.ccdsd_time_code(), CcsdsTimeCode::Cds);
2
        assert_eq!(dyn_provider.ccsds_days_as_u32(), 24);
2
        assert_eq!(dyn_provider.ms_of_day(), 24);
2
        assert_eq!(
2
            dyn_provider.len_of_day_seg(),
2
            LengthOfDaySegment::Short16Bits
2
        );
2
        assert_eq!(
2
            dyn_provider.submillis_precision(),
2
            SubmillisPrecision::Microseconds
2
        );
2
        assert_eq!(dyn_provider.submillis(), 666);
2
    }
    #[test]
2
    fn test_new_u24_days_too_large() {
2
        let time_provider = CdsTime::new_with_u24_days(2_u32.pow(24), 0);
2
        assert!(time_provider.is_err());
2
        let e = time_provider.unwrap_err();
2
        if let CdsError::InvalidCcsdsDays(days) = e {
2
            assert_eq!(days, 2_u32.pow(24) as i64);
        } else {
            panic!("unexpected error {}", e)
        }
2
    }
    #[test]
2
    fn test_from_dt_invalid_time() {
2
        // Date before CCSDS epoch
2
        let datetime_utc = NaiveDate::from_ymd_opt(1957, 12, 31)
2
            .unwrap()
2
            .and_hms_milli_opt(23, 59, 59, 999)
2
            .unwrap()
2
            .and_local_timezone(chrono::Utc)
2
            .unwrap();
2
        let time_provider = CdsTime::from_dt_with_u24_days(&datetime_utc);
2
        assert!(time_provider.is_err());
2
        if let CdsError::DateBeforeCcsdsEpoch(DateBeforeCcsdsEpochError(dt)) =
2
            time_provider.unwrap_err()
        {
2
            assert_eq!(dt, datetime_utc.into());
        }
2
    }
    #[test]
2
    fn test_eq() {
2
        let stamp0 = CdsTime::new_with_u16_days(0, 0);
2
        let mut buf: [u8; 7] = [0; 7];
2
        stamp0.write_to_bytes(&mut buf).unwrap();
2
        let stamp1 = CdsTime::from_bytes_with_u16_days(&buf).unwrap();
2
        assert_eq!(stamp0, stamp1);
2
        assert!(stamp0 >= stamp1);
2
        assert!(stamp1 <= stamp0);
2
    }
    #[test]
2
    fn test_ord() {
2
        let stamp0 = CdsTime::new_with_u24_days(0, 0).unwrap();
2
        let stamp1 = CdsTime::new_with_u24_days(0, 50000).unwrap();
2
        let mut stamp2 = CdsTime::new_with_u24_days(0, 50000).unwrap();
2
        stamp2.set_submillis(SubmillisPrecision::Microseconds, 500);
2
        let stamp3 = CdsTime::new_with_u24_days(1, 0).unwrap();
2
        assert!(stamp1 > stamp0);
2
        assert!(stamp2 > stamp0);
2
        assert!(stamp2 > stamp1);
2
        assert!(stamp3 > stamp0);
2
        assert!(stamp3 > stamp1);
2
        assert!(stamp3 > stamp2);
2
    }
    #[test]
2
    fn test_conversion() {
2
        let mut stamp_small = CdsTime::new_with_u16_days(u16::MAX, 500);
2
        let stamp_larger: CdsTime<DaysLen24Bits> = stamp_small.into();
2
        assert_eq!(stamp_larger.ccsds_days_as_u32(), u16::MAX as u32);
2
        assert_eq!(stamp_larger.ms_of_day(), 500);
2
        stamp_small = stamp_larger.try_into().unwrap();
2
        assert_eq!(stamp_small.ccsds_days_as_u32(), u16::MAX as u32);
2
        assert_eq!(stamp_small.ms_of_day(), 500);
2
    }
    #[test]
    #[cfg_attr(miri, ignore)]
2
    fn test_update_from_now() {
2
        let mut stamp = CdsTime::new_with_u16_days(0, 0);
2
        let _ = stamp.update_from_now();
2
        let dt = stamp.unix_time().chrono_date_time().unwrap();
2
        assert!(dt.year() > 2020);
2
    }
    #[test]
2
    fn test_setting_submillis_precision() {
2
        let mut provider = CdsTime::new_with_u16_days(0, 15);
2
        provider.set_submillis(SubmillisPrecision::Microseconds, 500);
2
    }
    #[test]
    #[cfg(feature = "serde")]
    #[cfg_attr(miri, ignore)]
2
    fn test_serialization() {
2
        let stamp_now = CdsTime::now_with_u16_days().expect("Error retrieving time");
2
        let val = to_allocvec(&stamp_now).expect("Serializing timestamp failed");
2
        assert!(val.len() > 0);
2
        let stamp_deser: CdsTime = from_bytes(&val).expect("Stamp deserialization failed");
2
        assert_eq!(stamp_deser, stamp_now);
2
    }
30
    fn generic_dt_property_equality_check(first: u32, second: u32, start: u32, end: u32) {
30
        if second < first {
            assert_eq!(second, start);
            assert_eq!(first, end);
30
        } else if second > first {
            assert_eq!(second - first, 1);
        } else {
30
            assert_eq!(first, second);
        }
30
    }
    #[test]
2
    fn test_stamp_to_vec_u16() {
2
        let stamp = CdsTime::new_with_u16_days(1, 1);
2
        let stamp_vec = stamp.to_vec().unwrap();
2
        let mut buf: [u8; 7] = [0; 7];
2
        stamp.write_to_bytes(&mut buf).unwrap();
2
        assert_eq!(stamp_vec, buf);
2
    }
    #[test]
2
    fn test_stamp_to_vec_u24() {
2
        let stamp = CdsTime::new_with_u24_days(1, 1).unwrap();
2
        let stamp_vec = stamp.to_vec().unwrap();
2
        let mut buf: [u8; 10] = [0; 10];
2
        stamp.write_to_bytes(&mut buf).unwrap();
2
        assert_eq!(stamp_vec, buf[..stamp.len_written()]);
2
    }
    #[test]
    #[cfg(feature = "timelib")]
2
    fn test_timelib_stamp() {
2
        let stamp = CdsTime::new_with_u16_days(0, 0);
2
        let timelib_dt = stamp.timelib_date_time().unwrap();
2
        assert_eq!(timelib_dt.year(), 1958);
2
        assert_eq!(timelib_dt.month(), time::Month::January);
2
        assert_eq!(timelib_dt.day(), 1);
2
        assert_eq!(timelib_dt.hour(), 0);
2
        assert_eq!(timelib_dt.minute(), 0);
2
        assert_eq!(timelib_dt.second(), 0);
2
    }
}