1
//! Module to generate or read CCSDS Unsegmented (CUC) timestamps as specified in
2
//! [CCSDS 301.0-B-4](https://public.ccsds.org/Pubs/301x0b4e1.pdf) section 3.2 .
3
//!
4
//! The core data structure to do this is the [CucTime] struct which provides a CUC time object
5
//! using the CCSDS Epoch.
6
#[cfg(feature = "serde")]
7
use serde::{Deserialize, Serialize};
8

            
9
use core::fmt::{Debug, Display, Formatter};
10
use core::ops::{Add, AddAssign};
11
use core::time::Duration;
12

            
13
use crate::ByteConversionError;
14

            
15
#[cfg(feature = "std")]
16
use super::StdTimestampError;
17
use super::{
18
    ccsds_epoch_to_unix_epoch, ccsds_time_code_from_p_field, unix_epoch_to_ccsds_epoch,
19
    CcsdsTimeCode, CcsdsTimeProvider, DateBeforeCcsdsEpochError, TimeReader, TimeWriter,
20
    TimestampError, UnixTime,
21
};
22
#[cfg(feature = "std")]
23
use std::error::Error;
24
#[cfg(feature = "std")]
25
use std::time::SystemTime;
26

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

            
30
const MIN_CUC_LEN: usize = 2;
31

            
32
/// Base value for the preamble field for a time field parser to determine the time field type.
33
pub const P_FIELD_BASE: u8 = (CcsdsTimeCode::CucCcsdsEpoch as u8) << 4;
34
/// Maximum length if the preamble field is not extended.
35
pub const MAX_CUC_LEN_SMALL_PREAMBLE: usize = 8;
36

            
37
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
38
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
39
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
40
pub enum FractionalResolution {
41
    /// No fractional part, only second resolution
42
    Seconds = 0,
43
    /// 255 fractional parts, resulting in 1/255 ~= 4 ms fractional resolution
44
    FourMs = 1,
45
    /// 65535 fractional parts, resulting in 1/65535 ~= 15 us fractional resolution
46
    FifteenUs = 2,
47
    /// 16777215 fractional parts, resulting in 1/16777215 ~= 60 ns fractional resolution
48
    SixtyNs = 3,
49
}
50

            
51
impl TryFrom<u8> for FractionalResolution {
52
    type Error = ();
53

            
54
6
    fn try_from(v: u8) -> Result<Self, Self::Error> {
55
6
        match v {
56
            0 => Ok(FractionalResolution::Seconds),
57
2
            1 => Ok(FractionalResolution::FourMs),
58
2
            2 => Ok(FractionalResolution::FifteenUs),
59
2
            3 => Ok(FractionalResolution::SixtyNs),
60
            _ => Err(()),
61
        }
62
6
    }
63
}
64

            
65
/// Please note that this function will panic if the fractional counter is not smaller than
66
/// the maximum number of fractions allowed for the particular resolution.
67
/// (e.g. passing 270 when the resolution only allows 255 values).
68
#[inline]
69
12
pub fn convert_fractional_part_to_ns(fractional_part: FractionalPart) -> u64 {
70
12
    let div = fractional_res_to_div(fractional_part.resolution);
71
12
    assert!(fractional_part.counter <= div);
72
10
    10_u64.pow(9) * fractional_part.counter as u64 / div as u64
73
10
}
74

            
75
#[inline(always)]
76
216
pub const fn fractional_res_to_div(res: FractionalResolution) -> u32 {
77
216
    // We do not use the full possible range for a given resolution. This is because if we did
78
216
    // that, the largest value would be equal to the counter being incremented by one. Thus, the
79
216
    // smallest allowed fractions value is 0 while the largest allowed fractions value is the
80
216
    // closest fractions value to the next counter increment.
81
216
    2_u32.pow(8 * res as u32) - 1
82
216
}
83

            
84
/// Calculate the fractional part for a given resolution and subsecond nanoseconds.
85
/// Please note that this function will panic if the passed nanoseconds exceeds 1 second
86
/// as a nanosecond (10 to the power of 9). Furthermore, it will return [None] if the
87
/// given resolution is [FractionalResolution::Seconds].
88
40
pub fn fractional_part_from_subsec_ns(res: FractionalResolution, ns: u64) -> FractionalPart {
89
40
    if res == FractionalResolution::Seconds {
90
10
        return FractionalPart::new_with_seconds_resolution();
91
30
    }
92
30
    let sec_as_ns = 10_u64.pow(9);
93
30
    if ns > sec_as_ns {
94
        panic!("passed nanosecond value larger than 1 second");
95
30
    }
96
30
    let resolution_divisor = fractional_res_to_div(res) as u64;
97
30
    // This is probably the cheapest way to calculate the fractional part without using expensive
98
30
    // floating point division.
99
30
    let fractional_counter = ns * (resolution_divisor + 1) / sec_as_ns;
100
30
    FractionalPart {
101
30
        resolution: res,
102
30
        counter: fractional_counter as u32,
103
30
    }
104
40
}
105

            
106
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
107
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
108
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
109
pub enum CucError {
110
    InvalidCounterWidth(u8),
111
    /// Invalid counter supplied.
112
    InvalidCounter {
113
        width: u8,
114
        counter: u64,
115
    },
116
    InvalidFractions {
117
        resolution: FractionalResolution,
118
        value: u64,
119
    },
120
    LeapSecondCorrectionError,
121
    DateBeforeCcsdsEpoch(DateBeforeCcsdsEpochError),
122
}
123

            
124
impl Display for CucError {
125
8
    fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
126
8
        match self {
127
6
            CucError::InvalidCounterWidth(w) => {
128
6
                write!(f, "invalid cuc counter byte width {w}")
129
            }
130
2
            CucError::InvalidCounter { width, counter } => {
131
2
                write!(f, "invalid cuc counter {counter} for width {width}")
132
            }
133
            CucError::InvalidFractions { resolution, value } => {
134
                write!(
135
                    f,
136
                    "invalid cuc fractional part {value} for resolution {resolution:?}"
137
                )
138
            }
139
            CucError::LeapSecondCorrectionError => {
140
                write!(f, "error while correcting for leap seconds")
141
            }
142
            CucError::DateBeforeCcsdsEpoch(e) => {
143
                write!(f, "date before ccsds epoch: {e}")
144
            }
145
        }
146
8
    }
147
}
148

            
149
#[cfg(feature = "std")]
150
impl Error for CucError {
151
    fn source(&self) -> Option<&(dyn Error + 'static)> {
152
        match self {
153
            CucError::DateBeforeCcsdsEpoch(e) => Some(e),
154
            _ => None,
155
        }
156
    }
157
}
158

            
159
impl From<DateBeforeCcsdsEpochError> for CucError {
160
    fn from(e: DateBeforeCcsdsEpochError) -> Self {
161
        Self::DateBeforeCcsdsEpoch(e)
162
    }
163
}
164

            
165
/// Tuple object where the first value is the width of the counter and the second value
166
/// is the counter value.
167
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
168
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
169
pub struct WidthCounterPair(pub u8, pub u32);
170

            
171
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
172
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
173
pub struct FractionalPart {
174
    pub resolution: FractionalResolution,
175
    pub counter: u32,
176
}
177

            
178
impl FractionalPart {
179
    #[inline]
180
90
    pub const fn new(resolution: FractionalResolution, counter: u32) -> Self {
181
90
        let div = fractional_res_to_div(resolution);
182
90
        assert!(counter <= div, "invalid counter for resolution");
183
90
        Self {
184
90
            resolution,
185
90
            counter,
186
90
        }
187
90
    }
188

            
189
    /// An empty fractional part for second resolution only.
190
    #[inline]
191
78
    pub const fn new_with_seconds_resolution() -> Self {
192
78
        Self::new(FractionalResolution::Seconds, 0)
193
78
    }
194

            
195
    /// Helper method which simply calls [Self::new_with_seconds_resolution].
196
    #[inline]
197
20
    pub const fn new_empty() -> Self {
198
20
        Self::new_with_seconds_resolution()
199
20
    }
200

            
201
    #[inline]
202
    pub fn new_checked(resolution: FractionalResolution, counter: u32) -> Option<Self> {
203
        let div = fractional_res_to_div(resolution);
204
        if counter > div {
205
            return None;
206
        }
207
        Some(Self {
208
            resolution,
209
            counter,
210
        })
211
    }
212

            
213
    #[inline]
214
66
    pub fn resolution(&self) -> FractionalResolution {
215
66
        self.resolution
216
66
    }
217

            
218
    #[inline]
219
22
    pub fn counter(&self) -> u32 {
220
22
        self.counter
221
22
    }
222

            
223
    #[inline]
224
    pub fn no_fractional_part(&self) -> bool {
225
        self.resolution == FractionalResolution::Seconds
226
    }
227
}
228

            
229
/// This object is the abstraction for the CCSDS Unsegmented Time Code (CUC) using the CCSDS epoch
230
/// and a small preamble field.
231
///
232
/// It has the capability to generate and read timestamps as specified in the CCSDS 301.0-B-4
233
/// section 3.2 . The preamble field only has one byte, which allows a time code representation
234
/// through the year 2094. The time is represented as a simple binary counter starting from the
235
/// fixed CCSDS epoch 1958-01-01T00:00:00+00:00 using the TAI reference time scale. This time
236
/// code provides the advantage of being truly monotonic.
237
/// It is possible to provide subsecond accuracy using the fractional field with various available
238
/// [resolutions][FractionalResolution].
239
///
240
/// Having a preamble field of one byte limits the width of the counter
241
/// type (generally seconds) to 4 bytes and the width of the fractions type to 3 bytes. This limits
242
/// the maximum time stamp size to [MAX_CUC_LEN_SMALL_PREAMBLE] (8 bytes).
243
///
244
/// Please note that this object does not implement the [CcsdsTimeProvider] trait by itself because
245
/// leap seconds corrections need to be applied to support the trait methods. Instead, it needs
246
/// to be converted to a [CucTimeWithLeapSecs] object using the [Self::to_leap_sec_helper] method.
247
///
248
/// This time code is not UTC based. Conversion to UTC based times, for example a UNIX timestamp,
249
/// can be performed by subtracting the current number of leap seconds.
250
///
251
/// # Example
252
///
253
/// ```
254
/// use spacepackets::time::cuc::{FractionalResolution, CucTime};
255
/// use spacepackets::time::{TimeWriter, CcsdsTimeCode, TimeReader, CcsdsTimeProvider};
256
///
257
/// const LEAP_SECONDS: u32 = 37;
258
///
259
/// // Highest fractional resolution
260
/// let timestamp_now = CucTime::now(FractionalResolution::SixtyNs, LEAP_SECONDS)
261
///     .expect("creating cuc stamp failed");
262
/// let mut raw_stamp = [0; 16];
263
/// {
264
///     let written = timestamp_now.write_to_bytes(&mut raw_stamp).expect("writing timestamp failed");
265
///     assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCode::CucCcsdsEpoch as u8);
266
///     // 1 byte preamble + 4 byte counter + 3 byte fractional part
267
///     assert_eq!(written, 8);
268
/// }
269
/// {
270
///     let read_result = CucTime::from_bytes(&raw_stamp);
271
///     assert!(read_result.is_ok());
272
///     let stamp_deserialized = read_result.unwrap();
273
///     assert_eq!(stamp_deserialized, timestamp_now);
274
/// }
275
/// ```
276
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
277
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
278
pub struct CucTime {
279
    pfield: u8,
280
    counter: WidthCounterPair,
281
    fractions: FractionalPart,
282
}
283

            
284
/// This object is a wrapper object around the [CucTime] object which also tracks
285
/// the leap seconds. This is necessary to implement the [CcsdsTimeProvider] trait.
286
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
287
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
288
pub struct CucTimeWithLeapSecs {
289
    pub time: CucTime,
290
    pub leap_seconds: u32,
291
}
292

            
293
impl CucTimeWithLeapSecs {
294
    #[inline]
295
8
    pub fn new(time: CucTime, leap_seconds: u32) -> Self {
296
8
        Self { time, leap_seconds }
297
8
    }
298
}
299

            
300
#[inline]
301
2
pub fn pfield_len(pfield: u8) -> usize {
302
2
    if ((pfield >> 7) & 0b1) == 1 {
303
        return 2;
304
2
    }
305
2
    1
306
2
}
307

            
308
impl CucTime {
309
    /// Create a time provider with a four byte counter and no fractional part.
310
    #[inline]
311
16
    pub fn new(counter: u32) -> Self {
312
16
        // These values are definitely valid, so it is okay to unwrap here.
313
16
        Self::new_generic(
314
16
            WidthCounterPair(4, counter),
315
16
            FractionalPart::new_with_seconds_resolution(),
316
16
        )
317
16
        .unwrap()
318
16
    }
319

            
320
    /// Like [CucTime::new] but allow to supply a fractional part as well.
321
    #[inline]
322
10
    pub fn new_with_fractions(counter: u32, fractions: FractionalPart) -> Result<Self, CucError> {
323
10
        Self::new_generic(WidthCounterPair(4, counter), fractions)
324
10
    }
325

            
326
    /// Fractions with a resolution of ~ 4 ms
327
    #[inline]
328
4
    pub fn new_with_coarse_fractions(counter: u32, subsec_fractions: u8) -> Self {
329
4
        // These values are definitely valid, so it is okay to unwrap here.
330
4
        Self::new_generic(
331
4
            WidthCounterPair(4, counter),
332
4
            FractionalPart {
333
4
                resolution: FractionalResolution::FourMs,
334
4
                counter: subsec_fractions as u32,
335
4
            },
336
4
        )
337
4
        .unwrap()
338
4
    }
339

            
340
    /// Fractions with a resolution of ~ 16 us
341
    #[inline]
342
4
    pub fn new_with_medium_fractions(counter: u32, subsec_fractions: u16) -> Self {
343
4
        // These values are definitely valid, so it is okay to unwrap here.
344
4
        Self::new_generic(
345
4
            WidthCounterPair(4, counter),
346
4
            FractionalPart {
347
4
                resolution: FractionalResolution::FifteenUs,
348
4
                counter: subsec_fractions as u32,
349
4
            },
350
4
        )
351
4
        .unwrap()
352
4
    }
353

            
354
    /// Fractions with a resolution of ~ 60 ns. The fractional part value is limited by the
355
    /// 24 bits of the fractional field, so this function will fail with
356
    /// [CucError::InvalidFractions] if the fractional value exceeds the value.
357
    #[inline]
358
8
    pub fn new_with_fine_fractions(counter: u32, subsec_fractions: u32) -> Result<Self, CucError> {
359
8
        Self::new_generic(
360
8
            WidthCounterPair(4, counter),
361
8
            FractionalPart {
362
8
                resolution: FractionalResolution::SixtyNs,
363
8
                counter: subsec_fractions,
364
8
            },
365
8
        )
366
8
    }
367

            
368
    /// This function will return the current time as a CUC timestamp.
369
    /// The counter width will always be set to 4 bytes because the normal CCSDS epoch will overflow
370
    /// when using less than that.
371
    ///
372
    /// The CUC timestamp uses TAI as the reference time system. Therefore, leap second corrections
373
    /// must be applied on top of the UTC based time retrieved from the system in addition to the
374
    /// conversion to the CCSDS epoch.
375
    #[cfg(feature = "std")]
376
2
    pub fn now(
377
2
        fraction_resolution: FractionalResolution,
378
2
        leap_seconds: u32,
379
2
    ) -> Result<Self, StdTimestampError> {
380
2
        let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
381
2
        let mut counter =
382
2
            u32::try_from(unix_epoch_to_ccsds_epoch(now.as_secs() as i64)).map_err(|_| {
383
                TimestampError::Cuc(CucError::InvalidCounter {
384
                    width: 4,
385
                    counter: now.as_secs(),
386
                })
387
2
            })?;
388
2
        counter = counter
389
2
            .checked_add(leap_seconds)
390
2
            .ok_or(TimestampError::Cuc(CucError::LeapSecondCorrectionError))?;
391
2
        if fraction_resolution == FractionalResolution::Seconds {
392
            return Ok(Self::new(counter));
393
2
        }
394
2
        let fractions =
395
2
            fractional_part_from_subsec_ns(fraction_resolution, now.subsec_nanos() as u64);
396
2
        Self::new_with_fractions(counter, fractions)
397
2
            .map_err(|e| StdTimestampError::Timestamp(e.into()))
398
2
    }
399

            
400
    /// Updates the current time stamp from the current time. The fractional field width remains
401
    /// the same and will be updated accordingly.
402
    ///
403
    /// The CUC timestamp uses TAI as the reference time system. Therefore, leap second corrections
404
    /// must be applied on top of the UTC based time retrieved from the system in addition to the
405
    /// conversion to the CCSDS epoch.
406
    #[cfg(feature = "std")]
407
2
    pub fn update_from_now(&mut self, leap_seconds: u32) -> Result<(), StdTimestampError> {
408
2
        let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
409
2
        self.counter.1 = unix_epoch_to_ccsds_epoch(now.as_secs() as i64) as u32;
410
2
        self.counter.1 = self
411
2
            .counter
412
2
            .1
413
2
            .checked_add(leap_seconds)
414
2
            .ok_or(TimestampError::Cuc(CucError::LeapSecondCorrectionError))?;
415
2
        if let FractionalResolution::Seconds = self.fractions.resolution {
416
            self.fractions = fractional_part_from_subsec_ns(
417
                self.fractions.resolution,
418
                now.subsec_nanos() as u64,
419
            );
420
            return Ok(());
421
2
        }
422
2
        Ok(())
423
2
    }
424

            
425
    #[cfg(feature = "chrono")]
426
2
    pub fn from_chrono_date_time(
427
2
        dt: &chrono::DateTime<chrono::Utc>,
428
2
        res: FractionalResolution,
429
2
        leap_seconds: u32,
430
2
    ) -> Result<Self, CucError> {
431
2
        // Year before CCSDS epoch is invalid.
432
2
        if dt.year() < 1958 {
433
            return Err(DateBeforeCcsdsEpochError(UnixTime::from(*dt)).into());
434
2
        }
435
2
        let counter = dt
436
2
            .timestamp()
437
2
            .checked_add(i64::from(leap_seconds))
438
2
            .ok_or(CucError::LeapSecondCorrectionError)?;
439
2
        Self::new_generic(
440
2
            WidthCounterPair(4, counter as u32),
441
2
            fractional_part_from_subsec_ns(res, dt.timestamp_subsec_nanos() as u64),
442
2
        )
443
2
    }
444

            
445
    /// Generates a CUC timestamp from a UNIX timestamp with a width of 4. This width is able
446
    /// to accomodate all possible UNIX timestamp values.
447
2
    pub fn from_unix_time(
448
2
        unix_time: &UnixTime,
449
2
        res: FractionalResolution,
450
2
        leap_seconds: u32,
451
2
    ) -> Result<Self, CucError> {
452
2
        let counter = unix_epoch_to_ccsds_epoch(unix_time.secs);
453
2
        // Negative CCSDS epoch is invalid.
454
2
        if counter < 0 {
455
            return Err(DateBeforeCcsdsEpochError(*unix_time).into());
456
2
        }
457
        // We already excluded negative values, so the conversion to u64 should always work.
458
2
        let mut counter = u32::try_from(counter).map_err(|_| CucError::InvalidCounter {
459
            width: 4,
460
            counter: counter as u64,
461
2
        })?;
462
2
        counter = counter
463
2
            .checked_add(leap_seconds)
464
2
            .ok_or(CucError::LeapSecondCorrectionError)?;
465
2
        let fractions =
466
2
            fractional_part_from_subsec_ns(res, unix_time.subsec_millis() as u64 * 10_u64.pow(6));
467
2
        Self::new_generic(WidthCounterPair(4, counter as u32), fractions)
468
2
    }
469

            
470
    /// Most generic constructor which allows full configurability for the counter and for the
471
    /// fractions.
472
    #[inline]
473
76
    pub fn new_generic(
474
76
        width_and_counter: WidthCounterPair,
475
76
        fractions: FractionalPart,
476
76
    ) -> Result<Self, CucError> {
477
76
        Self::verify_counter_width(width_and_counter.0)?;
478
74
        if width_and_counter.1 > (2u64.pow(width_and_counter.0 as u32 * 8) - 1) as u32 {
479
2
            return Err(CucError::InvalidCounter {
480
2
                width: width_and_counter.0,
481
2
                counter: width_and_counter.1.into(),
482
2
            });
483
72
        }
484
72
        Self::verify_fractions_value(fractions)?;
485
72
        Ok(Self {
486
72
            pfield: Self::build_p_field(width_and_counter.0, fractions.resolution),
487
72
            counter: width_and_counter,
488
72
            fractions,
489
72
        })
490
76
    }
491

            
492
    #[inline]
493
6
    pub fn ccsds_time_code(&self) -> CcsdsTimeCode {
494
6
        CcsdsTimeCode::CucCcsdsEpoch
495
6
    }
496

            
497
    #[inline]
498
12
    pub fn width_counter_pair(&self) -> WidthCounterPair {
499
12
        self.counter
500
12
    }
501

            
502
    #[inline]
503
2
    pub fn counter_width(&self) -> u8 {
504
2
        self.counter.0
505
2
    }
506

            
507
    #[inline]
508
10
    pub fn counter(&self) -> u32 {
509
10
        self.counter.1
510
10
    }
511

            
512
    /// Subsecond fractional part of the CUC time.
513
    #[inline]
514
56
    pub fn fractions(&self) -> FractionalPart {
515
56
        self.fractions
516
56
    }
517

            
518
    #[inline]
519
8
    pub fn to_leap_sec_helper(&self, leap_seconds: u32) -> CucTimeWithLeapSecs {
520
8
        CucTimeWithLeapSecs::new(*self, leap_seconds)
521
8
    }
522

            
523
    #[inline]
524
2
    pub fn set_fractions(&mut self, fractions: FractionalPart) -> Result<(), CucError> {
525
2
        Self::verify_fractions_value(fractions)?;
526
2
        self.fractions = fractions;
527
2
        self.update_p_field_fractions();
528
2
        Ok(())
529
2
    }
530

            
531
    /// Set a fractional resolution. Please note that this function will reset the fractional value
532
    /// to 0 if the resolution changes.
533
    #[inline]
534
6
    pub fn set_fractional_resolution(&mut self, res: FractionalResolution) {
535
6
        if res == FractionalResolution::Seconds {
536
            self.fractions = FractionalPart::new_with_seconds_resolution();
537
6
        }
538
6
        if res != self.fractions().resolution() {
539
6
            self.fractions = FractionalPart::new(res, 0);
540
6
        }
541
6
    }
542

            
543
    #[inline]
544
72
    fn build_p_field(counter_width: u8, resolution: FractionalResolution) -> u8 {
545
72
        let mut pfield = P_FIELD_BASE;
546
72
        if !(1..=4).contains(&counter_width) {
547
            // Okay to panic here, this function is private and all input values should
548
            // have been sanitized
549
            panic!("invalid counter width {} for cuc timestamp", counter_width);
550
72
        }
551
72
        pfield |= (counter_width - 1) << 2;
552
72
        if resolution != FractionalResolution::Seconds {
553
30
            if !(1..=3).contains(&(resolution as u8)) {
554
                // Okay to panic here, this function is private and all input values should
555
                // have been sanitized
556
                panic!("invalid fractions width {:?} for cuc timestamp", resolution);
557
30
            }
558
30
            pfield |= resolution as u8;
559
42
        }
560
72
        pfield
561
72
    }
562

            
563
    #[inline]
564
2
    fn update_p_field_fractions(&mut self) {
565
2
        self.pfield &= !(0b11);
566
2
        self.pfield |= self.fractions.resolution() as u8;
567
2
    }
568

            
569
    #[inline]
570
122
    pub fn len_cntr_from_pfield(pfield: u8) -> u8 {
571
122
        ((pfield >> 2) & 0b11) + 1
572
122
    }
573

            
574
    #[inline]
575
122
    pub fn len_fractions_from_pfield(pfield: u8) -> u8 {
576
122
        pfield & 0b11
577
122
    }
578

            
579
    #[inline]
580
4
    pub fn unix_secs(&self, leap_seconds: u32) -> i64 {
581
4
        ccsds_epoch_to_unix_epoch(self.counter.1 as i64)
582
4
            .checked_sub(leap_seconds as i64)
583
4
            .unwrap()
584
4
    }
585

            
586
    #[inline]
587
4
    pub fn subsec_millis(&self) -> u16 {
588
4
        (self.subsec_nanos() / 1_000_000) as u16
589
4
    }
590

            
591
    /// This returns the length of the individual components of the CUC timestamp in addition
592
    /// to the total size.
593
    ///
594
    /// This function will return a tuple where the first value is the byte width of the
595
    /// counter, the second value is the byte width of the fractional part, and the third
596
    /// components is the total size.
597
24
    pub fn len_components_and_total_from_pfield(pfield: u8) -> (u8, u8, usize) {
598
24
        let base_len: usize = 1;
599
24
        let cntr_len = Self::len_cntr_from_pfield(pfield);
600
24
        let fractions_len = Self::len_fractions_from_pfield(pfield);
601
24
        (
602
24
            cntr_len,
603
24
            fractions_len,
604
24
            base_len + cntr_len as usize + fractions_len as usize,
605
24
        )
606
24
    }
607

            
608
    #[inline]
609
98
    pub fn len_packed_from_pfield(pfield: u8) -> usize {
610
98
        let mut base_len: usize = 1;
611
98
        base_len += Self::len_cntr_from_pfield(pfield) as usize;
612
98
        base_len += Self::len_fractions_from_pfield(pfield) as usize;
613
98
        base_len
614
98
    }
615

            
616
    /// Verifies the raw width parameter.
617
    #[inline]
618
76
    fn verify_counter_width(width: u8) -> Result<(), CucError> {
619
76
        if width == 0 || width > 4 {
620
2
            return Err(CucError::InvalidCounterWidth(width));
621
74
        }
622
74
        Ok(())
623
76
    }
624

            
625
    #[inline]
626
74
    fn verify_fractions_value(val: FractionalPart) -> Result<(), CucError> {
627
74
        if val.counter > 2u32.pow((val.resolution as u32) * 8) - 1 {
628
            return Err(CucError::InvalidFractions {
629
                resolution: val.resolution,
630
                value: val.counter as u64,
631
            });
632
74
        }
633
74
        Ok(())
634
74
    }
635

            
636
    #[inline]
637
98
    fn len_as_bytes(&self) -> usize {
638
98
        Self::len_packed_from_pfield(self.pfield)
639
98
    }
640

            
641
    #[inline]
642
10
    fn subsec_nanos(&self) -> u32 {
643
10
        if self.fractions.resolution() == FractionalResolution::Seconds {
644
4
            return 0;
645
6
        }
646
6
        // Rounding down here is the correct approach.
647
6
        convert_fractional_part_to_ns(self.fractions) as u32
648
10
    }
649
}
650

            
651
impl TimeReader for CucTime {
652
30
    fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError> {
653
30
        if buf.len() < MIN_CUC_LEN {
654
4
            return Err(TimestampError::ByteConversion(
655
4
                ByteConversionError::FromSliceTooSmall {
656
4
                    expected: MIN_CUC_LEN,
657
4
                    found: buf.len(),
658
4
                },
659
4
            ));
660
26
        }
661
26
        match ccsds_time_code_from_p_field(buf[0]) {
662
26
            Ok(code) => {
663
26
                if code != CcsdsTimeCode::CucCcsdsEpoch {
664
2
                    return Err(TimestampError::InvalidTimeCode {
665
2
                        expected: CcsdsTimeCode::CucCcsdsEpoch,
666
2
                        found: code as u8,
667
2
                    });
668
24
                }
669
            }
670
            Err(raw) => {
671
                return Err(TimestampError::InvalidTimeCode {
672
                    expected: CcsdsTimeCode::CucCcsdsEpoch,
673
                    found: raw,
674
                });
675
            }
676
        }
677
24
        let (cntr_len, fractions_len, total_len) =
678
24
            Self::len_components_and_total_from_pfield(buf[0]);
679
24
        if buf.len() < total_len {
680
10
            return Err(TimestampError::ByteConversion(
681
10
                ByteConversionError::FromSliceTooSmall {
682
10
                    expected: total_len,
683
10
                    found: buf.len(),
684
10
                },
685
10
            ));
686
14
        }
687
14
        let mut current_idx = 1;
688
14
        let counter = match cntr_len {
689
2
            1 => buf[current_idx] as u32,
690
2
            2 => u16::from_be_bytes(buf[current_idx..current_idx + 2].try_into().unwrap()) as u32,
691
            3 => {
692
2
                let mut tmp_buf: [u8; 4] = [0; 4];
693
2
                tmp_buf[1..4].copy_from_slice(&buf[current_idx..current_idx + 3]);
694
2
                u32::from_be_bytes(tmp_buf)
695
            }
696
8
            4 => u32::from_be_bytes(buf[current_idx..current_idx + 4].try_into().unwrap()),
697
            _ => panic!("unreachable match arm"),
698
        };
699
14
        current_idx += cntr_len as usize;
700
14
        let mut fractions = FractionalPart::new_with_seconds_resolution();
701
14
        if fractions_len > 0 {
702
6
            match fractions_len {
703
                1 => {
704
2
                    fractions = FractionalPart::new(
705
2
                        fractions_len.try_into().unwrap(),
706
2
                        buf[current_idx] as u32,
707
2
                    )
708
                }
709
                2 => {
710
2
                    fractions = FractionalPart::new(
711
2
                        fractions_len.try_into().unwrap(),
712
2
                        u16::from_be_bytes(buf[current_idx..current_idx + 2].try_into().unwrap())
713
2
                            as u32,
714
2
                    )
715
                }
716
                3 => {
717
2
                    let mut tmp_buf: [u8; 4] = [0; 4];
718
2
                    tmp_buf[1..4].copy_from_slice(&buf[current_idx..current_idx + 3]);
719
2
                    fractions = FractionalPart::new(
720
2
                        fractions_len.try_into().unwrap(),
721
2
                        u32::from_be_bytes(tmp_buf),
722
2
                    )
723
                }
724
                _ => panic!("unreachable match arm"),
725
            }
726
8
        }
727
14
        let provider = Self::new_generic(WidthCounterPair(cntr_len, counter), fractions)?;
728
14
        Ok(provider)
729
30
    }
730
}
731

            
732
impl TimeWriter for CucTime {
733
42
    fn write_to_bytes(&self, bytes: &mut [u8]) -> Result<usize, TimestampError> {
734
42
        // Cross check the sizes of the counters against byte widths in the ctor
735
42
        if bytes.len() < self.len_as_bytes() {
736
14
            return Err(TimestampError::ByteConversion(
737
14
                ByteConversionError::ToSliceTooSmall {
738
14
                    found: bytes.len(),
739
14
                    expected: self.len_as_bytes(),
740
14
                },
741
14
            ));
742
28
        }
743
28
        bytes[0] = self.pfield;
744
28
        let mut current_idx: usize = 1;
745
28
        match self.counter.0 {
746
2
            1 => {
747
2
                bytes[current_idx] = self.counter.1 as u8;
748
2
            }
749
2
            2 => {
750
2
                bytes[current_idx..current_idx + 2]
751
2
                    .copy_from_slice(&(self.counter.1 as u16).to_be_bytes());
752
2
            }
753
2
            3 => {
754
2
                bytes[current_idx..current_idx + 3]
755
2
                    .copy_from_slice(&self.counter.1.to_be_bytes()[1..4]);
756
2
            }
757
22
            4 => {
758
22
                bytes[current_idx..current_idx + 4].copy_from_slice(&self.counter.1.to_be_bytes());
759
22
            }
760
            // Should never happen
761
            _ => panic!("invalid counter width value"),
762
        }
763
28
        current_idx += self.counter.0 as usize;
764
28
        match self.fractions.resolution() {
765
4
            FractionalResolution::FourMs => bytes[current_idx] = self.fractions.counter as u8,
766
4
            FractionalResolution::FifteenUs => bytes[current_idx..current_idx + 2]
767
4
                .copy_from_slice(&(self.fractions.counter as u16).to_be_bytes()),
768
6
            FractionalResolution::SixtyNs => bytes[current_idx..current_idx + 3]
769
6
                .copy_from_slice(&self.fractions.counter.to_be_bytes()[1..4]),
770
14
            _ => (),
771
        }
772
28
        current_idx += self.fractions.resolution as usize;
773
28
        Ok(current_idx)
774
42
    }
775

            
776
4
    fn len_written(&self) -> usize {
777
4
        self.len_as_bytes()
778
4
    }
779
}
780

            
781
impl CcsdsTimeProvider for CucTimeWithLeapSecs {
782
    #[inline]
783
    fn len_as_bytes(&self) -> usize {
784
        self.time.len_as_bytes()
785
    }
786

            
787
    #[inline]
788
    fn p_field(&self) -> (usize, [u8; 2]) {
789
        (1, [self.time.pfield, 0])
790
    }
791

            
792
    #[inline]
793
6
    fn ccdsd_time_code(&self) -> CcsdsTimeCode {
794
6
        self.time.ccsds_time_code()
795
6
    }
796

            
797
    #[inline]
798
4
    fn unix_secs(&self) -> i64 {
799
4
        self.time.unix_secs(self.leap_seconds)
800
4
    }
801

            
802
    #[inline]
803
4
    fn subsec_nanos(&self) -> u32 {
804
4
        self.time.subsec_nanos()
805
4
    }
806
}
807

            
808
// TODO: Introduce more overflow checks here.
809
14
fn get_time_values_after_duration_addition(
810
14
    time: &CucTime,
811
14
    duration: Duration,
812
14
) -> (u32, FractionalPart) {
813
14
    let mut new_counter = time.counter.1;
814
14
    let subsec_nanos = duration.subsec_nanos();
815
25
    let mut increment_counter = |amount: u32| {
816
18
        let mut sum: u64 = 0;
817
18
        let mut counter_inc_handler = |max_val: u64| {
818
18
            sum = new_counter as u64 + amount as u64;
819
18
            if sum >= max_val {
820
2
                new_counter = (sum % max_val) as u32;
821
2
                return;
822
16
            }
823
16
            new_counter = sum as u32;
824
18
        };
825
18
        match time.counter.0 {
826
2
            1 => counter_inc_handler(u8::MAX as u64),
827
            2 => counter_inc_handler(u16::MAX as u64),
828
            3 => counter_inc_handler((2_u32.pow(24) - 1) as u64),
829
16
            4 => counter_inc_handler(u32::MAX as u64),
830
            _ => {
831
                // Should never happen
832
                panic!("invalid counter width")
833
            }
834
        }
835
18
    };
836
14
    let resolution = time.fractions().resolution();
837
14
    let fractional_increment = fractional_part_from_subsec_ns(resolution, subsec_nanos as u64);
838
14
    let mut fractional_part = FractionalPart::new_with_seconds_resolution();
839
14
    if resolution != FractionalResolution::Seconds {
840
8
        let mut new_fractions = time.fractions().counter() + fractional_increment.counter;
841
8
        let max_fractions = fractional_res_to_div(resolution);
842
8
        if new_fractions > max_fractions {
843
4
            increment_counter(1);
844
4
            new_fractions -= max_fractions;
845
4
        }
846
8
        fractional_part = FractionalPart {
847
8
            resolution,
848
8
            counter: new_fractions,
849
8
        }
850
6
    }
851
14
    increment_counter(duration.as_secs() as u32);
852
14
    (new_counter, fractional_part)
853
14
}
854

            
855
impl AddAssign<Duration> for CucTime {
856
6
    fn add_assign(&mut self, duration: Duration) {
857
6
        let (new_counter, new_fractional_part) =
858
6
            get_time_values_after_duration_addition(self, duration);
859
6
        self.counter.1 = new_counter;
860
6
        self.fractions = new_fractional_part;
861
6
    }
862
}
863

            
864
impl Add<Duration> for CucTime {
865
    type Output = Self;
866

            
867
4
    fn add(self, duration: Duration) -> Self::Output {
868
4
        let (new_counter, new_fractional_part) =
869
4
            get_time_values_after_duration_addition(&self, duration);
870
4
        // The generated fractional part should always be valid, so its okay to unwrap here.
871
4
        Self::new_with_fractions(new_counter, new_fractional_part).unwrap()
872
4
    }
873
}
874

            
875
impl Add<Duration> for &CucTime {
876
    type Output = CucTime;
877

            
878
4
    fn add(self, duration: Duration) -> Self::Output {
879
4
        let (new_counter, new_fractional_part) =
880
4
            get_time_values_after_duration_addition(self, duration);
881
4
        // The generated fractional part should always be valid, so its okay to unwrap here.
882
4
        Self::Output::new_with_fractions(new_counter, new_fractional_part).unwrap()
883
4
    }
884
}
885

            
886
#[cfg(test)]
887
mod tests {
888
    use crate::time::{UnixTime, DAYS_CCSDS_TO_UNIX, SECONDS_PER_DAY};
889

            
890
    use super::*;
891
    use alloc::string::ToString;
892
    use chrono::{Datelike, TimeZone, Timelike};
893
    #[allow(unused_imports)]
894
    use std::println;
895

            
896
    const LEAP_SECONDS: u32 = 37;
897

            
898
    #[test]
899
2
    fn test_basic_zero_epoch() {
900
2
        // Do not include leap second corrections, which do not apply to dates before 1972.
901
2
        let zero_cuc = CucTime::new(0);
902
2
        assert_eq!(zero_cuc.len_as_bytes(), 5);
903
2
        assert_eq!(zero_cuc.counter_width(), zero_cuc.width_counter_pair().0);
904
2
        assert_eq!(zero_cuc.counter(), zero_cuc.width_counter_pair().1);
905
2
        let ccsds_cuc = zero_cuc.to_leap_sec_helper(0);
906
2
        assert_eq!(ccsds_cuc.ccdsd_time_code(), CcsdsTimeCode::CucCcsdsEpoch);
907
2
        let counter = zero_cuc.width_counter_pair();
908
2
        assert_eq!(counter.0, 4);
909
2
        assert_eq!(counter.1, 0);
910
2
        let fractions = zero_cuc.fractions();
911
2
        assert_eq!(fractions, FractionalPart::new_with_seconds_resolution());
912
2
        let dt = ccsds_cuc.chrono_date_time();
913
2
        if let chrono::LocalResult::Single(dt) = dt {
914
2
            assert_eq!(dt.year(), 1958);
915
2
            assert_eq!(dt.month(), 1);
916
2
            assert_eq!(dt.day(), 1);
917
2
            assert_eq!(dt.hour(), 0);
918
2
            assert_eq!(dt.minute(), 0);
919
2
            assert_eq!(dt.second(), 0);
920
        }
921
2
    }
922

            
923
    #[test]
924
2
    fn test_write_no_fractions() {
925
2
        let mut buf: [u8; 16] = [0; 16];
926
2
        let zero_cuc =
927
2
            CucTime::new_generic(WidthCounterPair(4, 0x20102030), FractionalPart::new_empty());
928
2
        assert!(zero_cuc.is_ok());
929
2
        let zero_cuc = zero_cuc.unwrap();
930
2
        let res = zero_cuc.write_to_bytes(&mut buf);
931
2
        assert!(res.is_ok());
932
2
        assert_eq!(zero_cuc.subsec_nanos(), 0);
933
2
        assert_eq!(zero_cuc.len_as_bytes(), 5);
934
2
        assert_eq!(pfield_len(buf[0]), 1);
935
2
        let written = res.unwrap();
936
2
        assert_eq!(written, 5);
937
2
        assert_eq!((buf[0] >> 7) & 0b1, 0);
938
2
        let time_code = ccsds_time_code_from_p_field(buf[0]);
939
2
        assert!(time_code.is_ok());
940
2
        assert_eq!(time_code.unwrap(), CcsdsTimeCode::CucCcsdsEpoch);
941
2
        assert_eq!((buf[0] >> 2) & 0b11, 0b11);
942
2
        assert_eq!(buf[0] & 0b11, 0);
943
2
        let raw_counter = u32::from_be_bytes(buf[1..5].try_into().unwrap());
944
2
        assert_eq!(raw_counter, 0x20102030);
945
2
        assert_eq!(buf[5], 0);
946
2
    }
947

            
948
    #[test]
949
    #[cfg_attr(miri, ignore)]
950
2
    fn test_datetime_now() {
951
2
        let now = chrono::Utc::now();
952
2
        let cuc_now = CucTime::now(FractionalResolution::SixtyNs, LEAP_SECONDS);
953
2
        assert!(cuc_now.is_ok());
954
2
        let cuc_now = cuc_now.unwrap();
955
2
        let ccsds_cuc = cuc_now.to_leap_sec_helper(LEAP_SECONDS);
956
2
        let dt_opt = ccsds_cuc.chrono_date_time();
957
2
        if let chrono::LocalResult::Single(dt) = dt_opt {
958
2
            let diff = dt - now;
959
2
            assert!(diff.num_milliseconds() < 1000);
960
2
            println!("datetime from cuc: {}", dt);
961
2
            println!("datetime now: {}", now);
962
        } else {
963
            panic!("date time creation from now failed")
964
        }
965
2
    }
966

            
967
    #[test]
968
2
    fn test_read_no_fractions() {
969
2
        let mut buf: [u8; 16] = [0; 16];
970
2
        let zero_cuc = CucTime::new_generic(
971
2
            WidthCounterPair(4, 0x20102030),
972
2
            FractionalPart::new_with_seconds_resolution(),
973
2
        )
974
2
        .unwrap();
975
2
        zero_cuc.write_to_bytes(&mut buf).unwrap();
976
2
        let cuc_read_back = CucTime::from_bytes(&buf).expect("reading cuc timestamp failed");
977
2
        assert_eq!(cuc_read_back, zero_cuc);
978
2
        assert_eq!(cuc_read_back.width_counter_pair().1, 0x20102030);
979
2
        assert_eq!(cuc_read_back.fractions(), FractionalPart::new_empty());
980
2
    }
981

            
982
    #[test]
983
2
    fn invalid_read_len() {
984
2
        let mut buf: [u8; 16] = [0; 16];
985
6
        for i in 0..2 {
986
4
            let res = CucTime::from_bytes(&buf[0..i]);
987
4
            assert!(res.is_err());
988
4
            let err = res.unwrap_err();
989
            if let TimestampError::ByteConversion(ByteConversionError::FromSliceTooSmall {
990
4
                found,
991
4
                expected,
992
4
            }) = err
993
            {
994
4
                assert_eq!(found, i);
995
4
                assert_eq!(expected, 2);
996
            }
997
        }
998
2
        let large_stamp = CucTime::new_with_fine_fractions(22, 300).unwrap();
999
2
        large_stamp.write_to_bytes(&mut buf).unwrap();
10
        for i in 2..large_stamp.len_as_bytes() - 1 {
10
            let res = CucTime::from_bytes(&buf[0..i]);
10
            assert!(res.is_err());
10
            let err = res.unwrap_err();
            if let TimestampError::ByteConversion(ByteConversionError::FromSliceTooSmall {
10
                found,
10
                expected,
10
            }) = err
            {
10
                assert_eq!(found, i);
10
                assert_eq!(expected, large_stamp.len_as_bytes());
            }
        }
2
    }
    #[test]
2
    fn write_and_read_tiny_stamp() {
2
        let mut buf = [0; 2];
2
        let cuc = CucTime::new_generic(WidthCounterPair(1, 200), FractionalPart::new_empty());
2
        assert!(cuc.is_ok());
2
        let cuc = cuc.unwrap();
2
        assert_eq!(cuc.len_as_bytes(), 2);
2
        let res = cuc.write_to_bytes(&mut buf);
2
        assert!(res.is_ok());
2
        let written = res.unwrap();
2
        assert_eq!(written, 2);
2
        assert_eq!(buf[1], 200);
2
        let cuc_read_back = CucTime::from_bytes(&buf);
2
        assert!(cuc_read_back.is_ok());
2
        let cuc_read_back = cuc_read_back.unwrap();
2
        assert_eq!(cuc_read_back, cuc);
2
    }
    #[test]
2
    fn write_slightly_larger_stamp() {
2
        let mut buf = [0; 4];
2
        let cuc = CucTime::new_generic(WidthCounterPair(2, 40000), FractionalPart::new_empty());
2
        assert!(cuc.is_ok());
2
        let cuc = cuc.unwrap();
2
        assert_eq!(cuc.len_as_bytes(), 3);
2
        let res = cuc.write_to_bytes(&mut buf);
2
        assert!(res.is_ok());
2
        let written = res.unwrap();
2
        assert_eq!(written, 3);
2
        assert_eq!(u16::from_be_bytes(buf[1..3].try_into().unwrap()), 40000);
2
        let cuc_read_back = CucTime::from_bytes(&buf);
2
        assert!(cuc_read_back.is_ok());
2
        let cuc_read_back = cuc_read_back.unwrap();
2
        assert_eq!(cuc_read_back, cuc);
2
    }
    #[test]
2
    fn invalid_buf_len_for_read() {}
    #[test]
2
    fn write_read_three_byte_cntr_stamp() {
2
        let mut buf = [0; 4];
2
        let cuc = CucTime::new_generic(
2
            WidthCounterPair(3, 2_u32.pow(24) - 2),
2
            FractionalPart::new_empty(),
2
        );
2
        assert!(cuc.is_ok());
2
        let cuc = cuc.unwrap();
2
        assert_eq!(cuc.len_as_bytes(), 4);
2
        let res = cuc.write_to_bytes(&mut buf);
2
        assert!(res.is_ok());
2
        let written = res.unwrap();
2
        assert_eq!(written, 4);
2
        let mut temp_buf = [0; 4];
2
        temp_buf[1..4].copy_from_slice(&buf[1..4]);
2
        assert_eq!(u32::from_be_bytes(temp_buf), 2_u32.pow(24) - 2);
2
        let cuc_read_back = CucTime::from_bytes(&buf);
2
        assert!(cuc_read_back.is_ok());
2
        let cuc_read_back = cuc_read_back.unwrap();
2
        assert_eq!(cuc_read_back, cuc);
2
    }
    #[test]
2
    fn test_write_invalid_buf() {
2
        let mut buf: [u8; 16] = [0; 16];
2
        let res = CucTime::new_with_fine_fractions(0, 0);
2
        let cuc = res.unwrap();
14
        for i in 0..cuc.len_as_bytes() - 1 {
14
            let err = cuc.write_to_bytes(&mut buf[0..i]);
14
            assert!(err.is_err());
14
            let err = err.unwrap_err();
            if let TimestampError::ByteConversion(ByteConversionError::ToSliceTooSmall {
14
                found,
14
                expected,
14
            }) = err
            {
14
                assert_eq!(expected, cuc.len_as_bytes());
14
                assert_eq!(found, i);
            } else {
                panic!("unexpected error: {}", err);
            }
        }
2
    }
    #[test]
2
    fn invalid_ccsds_stamp_type() {
2
        let mut buf: [u8; 16] = [0; 16];
2
        buf[0] |= (CcsdsTimeCode::CucAgencyEpoch as u8) << 4;
2
        let res = CucTime::from_bytes(&buf);
2
        assert!(res.is_err());
2
        let err = res.unwrap_err();
2
        if let TimestampError::InvalidTimeCode { expected, found } = err {
2
            assert_eq!(expected, CcsdsTimeCode::CucCcsdsEpoch);
2
            assert_eq!(found, CcsdsTimeCode::CucAgencyEpoch as u8);
        } else {
            panic!("unexpected error: {}", err);
        }
2
    }
    #[test]
2
    fn test_write_with_coarse_fractions() {
2
        let mut buf: [u8; 16] = [0; 16];
2
        let cuc = CucTime::new_with_coarse_fractions(0x30201060, 120);
2
        assert_eq!(cuc.fractions().counter(), 120);
2
        assert_eq!(cuc.fractions().resolution(), FractionalResolution::FourMs);
2
        let res = cuc.write_to_bytes(&mut buf);
2
        assert!(res.is_ok());
2
        let written = res.unwrap();
2
        assert_eq!(written, 6);
2
        assert_eq!(buf[5], 120);
2
        assert_eq!(buf[6], 0);
2
        assert_eq!(
2
            u32::from_be_bytes(buf[1..5].try_into().unwrap()),
2
            0x30201060
2
        );
2
    }
    #[test]
2
    fn test_read_with_coarse_fractions() {
2
        let mut buf: [u8; 16] = [0; 16];
2
        let cuc = CucTime::new_with_coarse_fractions(0x30201060, 120);
2
        let res = cuc.write_to_bytes(&mut buf);
2
        assert!(res.is_ok());
2
        let res = CucTime::from_bytes(&buf);
2
        assert!(res.is_ok());
2
        let read_back = res.unwrap();
2
        assert_eq!(read_back, cuc);
2
    }
    #[test]
2
    fn test_write_with_medium_fractions() {
2
        let mut buf: [u8; 16] = [0; 16];
2
        let cuc = CucTime::new_with_medium_fractions(0x30303030, 30000);
2
        let res = cuc.write_to_bytes(&mut buf);
2
        assert!(res.is_ok());
2
        let written = res.unwrap();
2
        assert_eq!(written, 7);
2
        assert_eq!(u16::from_be_bytes(buf[5..7].try_into().unwrap()), 30000);
2
        assert_eq!(buf[7], 0);
2
    }
    #[test]
2
    fn test_read_with_medium_fractions() {
2
        let mut buf: [u8; 16] = [0; 16];
2
        let cuc = CucTime::new_with_medium_fractions(0x30303030, 30000);
2
        let res = cuc.write_to_bytes(&mut buf);
2
        assert!(res.is_ok());
2
        let res = CucTime::from_bytes(&buf);
2
        assert!(res.is_ok());
2
        let cuc_read_back = res.unwrap();
2
        assert_eq!(cuc_read_back, cuc);
2
    }
    #[test]
2
    fn test_write_with_fine_fractions() {
2
        let mut buf: [u8; 16] = [0; 16];
2
        let cuc = CucTime::new_with_fine_fractions(0x30303030, u16::MAX as u32 + 60000);
2
        assert!(cuc.is_ok());
2
        let cuc = cuc.unwrap();
2
        let res = cuc.write_to_bytes(&mut buf);
2
        let written = res.unwrap();
2
        assert_eq!(written, 8);
2
        let mut dummy_buf: [u8; 4] = [0; 4];
2
        dummy_buf[1..4].copy_from_slice(&buf[5..8]);
2
        assert_eq!(u32::from_be_bytes(dummy_buf), u16::MAX as u32 + 60000);
2
        assert_eq!(buf[8], 0);
2
    }
    #[test]
2
    fn test_read_with_fine_fractions() {
2
        let mut buf: [u8; 16] = [0; 16];
2
        let cuc = CucTime::new_with_fine_fractions(0x30303030, u16::MAX as u32 + 60000);
2
        assert!(cuc.is_ok());
2
        let cuc = cuc.unwrap();
2
        let res = cuc.write_to_bytes(&mut buf);
2
        assert!(res.is_ok());
2
        let res = CucTime::from_bytes(&buf);
2
        assert!(res.is_ok());
2
        let cuc_read_back = res.unwrap();
2
        assert_eq!(cuc_read_back, cuc);
2
    }
    #[test]
2
    fn test_fractional_converter() {
2
        let ns = convert_fractional_part_to_ns(FractionalPart {
2
            resolution: FractionalResolution::FourMs,
2
            counter: 2,
2
        });
2
        // The formula for this is 2/255 * 10e9 = 7.843.137.
2
        assert_eq!(ns, 7843137);
        // This is the largest value we should be able to pass without this function panicking.
2
        let ns = convert_fractional_part_to_ns(FractionalPart {
2
            resolution: FractionalResolution::SixtyNs,
2
            counter: 2_u32.pow(24) - 2,
2
        });
2
        assert_eq!(ns, 999999940);
2
    }
    #[test]
    #[should_panic]
2
    fn test_fractional_converter_invalid_input() {
2
        convert_fractional_part_to_ns(FractionalPart {
2
            resolution: FractionalResolution::FourMs,
2
            counter: 256,
2
        });
2
    }
    #[test]
    #[should_panic]
2
    fn test_fractional_converter_invalid_input_2() {
2
        convert_fractional_part_to_ns(FractionalPart {
2
            resolution: FractionalResolution::SixtyNs,
2
            counter: 2_u32.pow(32) - 1,
2
        });
2
    }
    #[test]
2
    fn fractional_part_formula() {
2
        let fractional_part = fractional_part_from_subsec_ns(FractionalResolution::FourMs, 7843138);
2
        assert_eq!(fractional_part.counter, 2);
2
    }
    #[test]
2
    fn fractional_part_formula_2() {
2
        let fractional_part =
2
            fractional_part_from_subsec_ns(FractionalResolution::FourMs, 12000000);
2
        assert_eq!(fractional_part.counter, 3);
2
    }
    #[test]
2
    fn fractional_part_formula_3() {
2
        let one_fraction_with_width_two_in_ns =
2
            10_u64.pow(9) as f64 / (2_u32.pow(8 * 2) - 1) as f64;
2
        assert_eq!(one_fraction_with_width_two_in_ns.ceil(), 15260.0);
2
        let hundred_fractions_and_some =
2
            (100.0 * one_fraction_with_width_two_in_ns).floor() as u64 + 7000;
2
        let fractional_part = fractional_part_from_subsec_ns(
2
            FractionalResolution::FifteenUs,
2
            hundred_fractions_and_some,
2
        );
2
        assert_eq!(fractional_part.counter, 100);
        // Using exactly 101.0 can yield values which will later be rounded down to 100
2
        let hundred_and_one_fractions =
2
            (101.001 * one_fraction_with_width_two_in_ns).floor() as u64;
2
        let fractional_part = fractional_part_from_subsec_ns(
2
            FractionalResolution::FifteenUs,
2
            hundred_and_one_fractions,
2
        );
2
        assert_eq!(fractional_part.counter, 101);
2
    }
    #[test]
2
    fn update_fractions() {
2
        let mut stamp = CucTime::new(2000);
2
        let res = stamp.set_fractions(FractionalPart {
2
            resolution: FractionalResolution::SixtyNs,
2
            counter: 5000,
2
        });
2
        assert!(res.is_ok());
2
        assert_eq!(
2
            stamp.fractions().resolution(),
2
            FractionalResolution::SixtyNs
2
        );
2
        assert_eq!(stamp.fractions().counter(), 5000);
2
    }
    #[test]
    #[cfg_attr(miri, ignore)]
2
    fn set_fract_resolution() {
2
        let mut stamp = CucTime::new(2000);
2
        stamp.set_fractional_resolution(FractionalResolution::SixtyNs);
2
        assert_eq!(
2
            stamp.fractions().resolution(),
2
            FractionalResolution::SixtyNs
2
        );
2
        assert_eq!(stamp.fractions().counter(), 0);
2
        let res = stamp.update_from_now(LEAP_SECONDS);
2

            
2
        assert!(res.is_ok());
2
    }
    #[test]
2
    fn test_small_fraction_floored_to_zero() {
2
        let fractions = fractional_part_from_subsec_ns(FractionalResolution::SixtyNs, 59);
2
        assert_eq!(fractions.counter, 0);
2
    }
    #[test]
2
    fn test_small_fraction_becomes_fractional_part() {
2
        let fractions = fractional_part_from_subsec_ns(FractionalResolution::SixtyNs, 61);
2
        assert_eq!(fractions.counter, 1);
2
    }
    #[test]
2
    fn test_smallest_resolution_small_nanoseconds_floored_to_zero() {
2
        let fractions =
2
            fractional_part_from_subsec_ns(FractionalResolution::FourMs, 3800 * 1e3 as u64);
2
        assert_eq!(fractions.counter, 0);
2
    }
    #[test]
2
    fn test_smallest_resolution_small_nanoseconds_becomes_one_fraction() {
2
        let fractions =
2
            fractional_part_from_subsec_ns(FractionalResolution::FourMs, 4000 * 1e3 as u64);
2
        assert_eq!(fractions.counter, 1);
2
    }
    #[test]
2
    fn test_smallest_resolution_large_nanoseconds_becomes_largest_fraction() {
2
        let fractions =
2
            fractional_part_from_subsec_ns(FractionalResolution::FourMs, 10u64.pow(9) - 1);
2
        assert_eq!(fractions.counter, 2_u32.pow(8) - 1);
2
    }
    #[test]
2
    fn test_largest_fractions_with_largest_resolution() {
2
        let fractions =
2
            fractional_part_from_subsec_ns(FractionalResolution::SixtyNs, 10u64.pow(9) - 1);
2
        // The value can not be larger than representable by 3 bytes
2
        // Assert that the maximum resolution can be reached
2
        assert_eq!(fractions.counter, 2_u32.pow(3 * 8) - 1);
2
    }
4
    fn check_stamp_after_addition(cuc_stamp: &CucTime) {
4
        let cuc_with_leaps = cuc_stamp.to_leap_sec_helper(LEAP_SECONDS);
4
        assert_eq!(
4
            cuc_with_leaps.ccdsd_time_code(),
4
            CcsdsTimeCode::CucCcsdsEpoch
4
        );
4
        assert_eq!(cuc_stamp.width_counter_pair().1, 202);
4
        let fractions = cuc_stamp.fractions().counter();
4
        let expected_val =
4
            (0.5 * fractional_res_to_div(FractionalResolution::FifteenUs) as f64).ceil() as u32;
4
        assert_eq!(fractions, expected_val);
4
        let cuc_stamp2 = cuc_stamp + Duration::from_millis(501);
4
        // What I would roughly expect
4
        assert_eq!(cuc_stamp2.counter.1, 203);
4
        assert!(cuc_stamp2.fractions().counter() < 100);
4
        assert!(cuc_stamp2.subsec_millis() <= 1);
4
    }
    #[test]
2
    fn add_duration_basic() {
2
        let mut cuc_stamp = CucTime::new(200);
2
        cuc_stamp.set_fractional_resolution(FractionalResolution::FifteenUs);
2
        let duration = Duration::from_millis(2500);
2
        cuc_stamp += duration;
2
        check_stamp_after_addition(&cuc_stamp);
2
    }
    #[test]
2
    fn add_duration_basic_on_ref() {
2
        let mut cuc_stamp = CucTime::new(200);
2
        cuc_stamp.set_fractional_resolution(FractionalResolution::FifteenUs);
2
        let duration = Duration::from_millis(2500);
2
        let new_stamp = cuc_stamp + duration;
2
        check_stamp_after_addition(&new_stamp);
2
    }
    #[test]
2
    fn add_duration_basic_no_fractions() {
2
        let mut cuc_stamp = CucTime::new(200);
2
        let duration = Duration::from_millis(2000);
2
        cuc_stamp += duration;
2
        assert_eq!(cuc_stamp.counter(), 202);
2
        assert_eq!(cuc_stamp.fractions(), FractionalPart::new_empty());
2
    }
    #[test]
2
    fn add_duration_basic_on_ref_no_fractions() {
2
        let cuc_stamp = CucTime::new(200);
2
        let duration = Duration::from_millis(2000);
2
        let new_stamp = cuc_stamp + duration;
2
        assert_eq!(new_stamp.counter(), 202);
2
        assert_eq!(new_stamp.fractions(), FractionalPart::new_empty());
2
    }
    #[test]
2
    fn add_duration_overflow() {
2
        let mut cuc_stamp =
2
            CucTime::new_generic(WidthCounterPair(1, 255), FractionalPart::new_empty()).unwrap();
2
        let duration = Duration::from_secs(10);
2
        cuc_stamp += duration;
2
        assert_eq!(cuc_stamp.counter.1, 10);
2
    }
    #[test]
2
    fn test_invalid_width_param() {
2
        let error = CucTime::new_generic(WidthCounterPair(8, 0), FractionalPart::new_empty());
2
        assert!(error.is_err());
2
        let error = error.unwrap_err();
2
        if let CucError::InvalidCounterWidth(width) = error {
2
            assert_eq!(width, 8);
2
            assert_eq!(error.to_string(), "invalid cuc counter byte width 8");
        } else {
            panic!("unexpected error: {}", error);
        }
2
    }
    #[test]
2
    fn test_from_dt() {
2
        let dt = chrono::Utc.with_ymd_and_hms(2021, 1, 1, 0, 0, 0).unwrap();
2
        let cuc = CucTime::from_chrono_date_time(&dt, FractionalResolution::Seconds, LEAP_SECONDS)
2
            .unwrap();
2
        assert_eq!(cuc.counter(), dt.timestamp() as u32 + LEAP_SECONDS);
2
    }
    #[test]
2
    fn from_unix_stamp() {
2
        let unix_stamp = UnixTime::new(0, 0);
2
        let cuc = CucTime::from_unix_time(&unix_stamp, FractionalResolution::Seconds, LEAP_SECONDS)
2
            .expect("failed to create cuc from unix stamp");
2
        assert_eq!(
2
            cuc.counter(),
2
            (-DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32) as u32 + LEAP_SECONDS
2
        );
2
    }
    #[test]
2
    fn test_invalid_counter() {
2
        let cuc_error = CucTime::new_generic(WidthCounterPair(1, 256), FractionalPart::new_empty());
2
        assert!(cuc_error.is_err());
2
        let cuc_error = cuc_error.unwrap_err();
2
        if let CucError::InvalidCounter { width, counter } = cuc_error {
2
            assert_eq!(width, 1);
2
            assert_eq!(counter, 256);
2
            assert_eq!(cuc_error.to_string(), "invalid cuc counter 256 for width 1");
        } else {
            panic!("unexpected error: {}", cuc_error);
        }
2
    }
    #[test]
2
    fn test_stamp_to_vec() {
2
        let stamp = CucTime::new(100);
2
        let stamp_vec = stamp.to_vec().unwrap();
2
        let mut buf: [u8; 16] = [0; 16];
2
        stamp.write_to_bytes(&mut buf).unwrap();
2
        assert_eq!(stamp_vec, buf[..stamp.len_written()]);
2
    }
}