spacepackets 0.17.0

Generic implementations for various CCSDS and ECSS packet standards
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
//! CCSDS Time Code Formats according to [CCSDS 301.0-B-4](https://public.ccsds.org/Pubs/301x0b4e1.pdf)
use crate::ByteConversionError;
#[cfg(feature = "chrono")]
use chrono::{TimeZone, Utc};
use core::cmp::Ordering;
use core::ops::{Add, AddAssign, Sub};
use core::time::Duration;

#[allow(unused_imports)]
#[cfg(not(feature = "std"))]
use num_traits::float::FloatCore;

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

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

pub mod ascii;
pub mod cds;
pub mod cuc;

/// Conversion constant for converting CCSDS days to UNIX days.
pub const DAYS_CCSDS_TO_UNIX: i32 = -4383;
/// Seconds per day.
pub const SECONDS_PER_DAY: u32 = 86400;
/// Milliseconds per day.
pub const MS_PER_DAY: u32 = SECONDS_PER_DAY * 1000;
/// Nanoseconds per second.
pub const NANOS_PER_SECOND: u32 = 1_000_000_000;

/// CCSDS time code identifiers.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum CcsdsTimeCode {
    /// CUC with a CCSDS epoch (1958-01-01T00:00:00+00:00).
    CucCcsdsEpoch = 0b001,
    /// CUC with a custom agency epoch.
    CucAgencyEpoch = 0b010,
    /// CDS time code.
    Cds = 0b100,
    /// CCS time code.
    Ccs = 0b101,
    /// Agency defined time code.
    AgencyDefined = 0b110,
}

impl TryFrom<u8> for CcsdsTimeCode {
    type Error = ();

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            x if x == CcsdsTimeCode::CucCcsdsEpoch as u8 => Ok(CcsdsTimeCode::CucCcsdsEpoch),
            x if x == CcsdsTimeCode::CucAgencyEpoch as u8 => Ok(CcsdsTimeCode::CucAgencyEpoch),
            x if x == CcsdsTimeCode::Cds as u8 => Ok(CcsdsTimeCode::Cds),
            x if x == CcsdsTimeCode::Ccs as u8 => Ok(CcsdsTimeCode::Ccs),
            x if x == CcsdsTimeCode::AgencyDefined as u8 => Ok(CcsdsTimeCode::AgencyDefined),
            _ => Err(()),
        }
    }
}

/// Retrieve the CCSDS time code from the p-field. If no valid time code identifier is found, the
/// value of the raw time code identification field is returned.
pub fn ccsds_time_code_from_p_field(pfield: u8) -> Result<CcsdsTimeCode, u8> {
    let raw_bits = (pfield >> 4) & 0b111;
    CcsdsTimeCode::try_from(raw_bits).map_err(|_| raw_bits)
}

/// Date is before the CCSDS epoch.
#[derive(Debug, PartialEq, Eq, Copy, Clone, thiserror::Error)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[error("date before ccsds epoch: {0:?}")]
pub struct DateBeforeCcsdsEpochError(UnixTime);

/// Generic timestamp error.
#[derive(Debug, PartialEq, Eq, Copy, Clone, thiserror::Error)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[non_exhaustive]
pub enum TimestampError {
    /// Invalid time code.
    #[error("invalid time code, expected {expected:?}, found {found}")]
    InvalidTimeCode {
        /// Expected time code.
        expected: CcsdsTimeCode,
        /// Found raw time code.
        found: u8,
    },
    /// Byte conversion error.
    #[error("time stamp: byte conversion error: {0}")]
    ByteConversion(#[from] ByteConversionError),
    /// CDS timestamp error.
    #[error("CDS error: {0}")]
    Cds(#[from] cds::CdsError),
    /// CUC timestamp error.
    #[error("CUC error: {0}")]
    Cuc(#[from] cuc::CucError),
    /// Custom epoch is not supported.
    #[error("custom epoch not supported")]
    CustomEpochNotSupported,
}

/// [std] module.
#[cfg(feature = "std")]
pub mod std_mod {
    use crate::time::TimestampError;
    use std::time::SystemTimeError;
    use thiserror::Error;

    /// [std] timestamp error.
    #[derive(Debug, Clone, Error)]
    pub enum StdTimestampError {
        /// System time error.
        #[error("system time error: {0:?}")]
        SystemTime(#[from] SystemTimeError),
        /// Generic timestamp error.
        #[error("timestamp error: {0}")]
        Timestamp(#[from] TimestampError),
    }
}

/// Seconds since epoch for the current system time.
#[cfg(feature = "std")]
pub fn seconds_since_epoch() -> f64 {
    SystemTime::now()
        .duration_since(SystemTime::UNIX_EPOCH)
        .expect("System time generation failed")
        .as_secs_f64()
}

/// Convert UNIX days to CCSDS days
///
///  - CCSDS epoch: 1958-01-01T00:00:00+00:00
///  - UNIX Epoch: 1970-01-01T00:00:00+00:00
#[inline]
pub const fn unix_to_ccsds_days(unix_days: i64) -> i64 {
    unix_days - DAYS_CCSDS_TO_UNIX as i64
}

/// Convert CCSDS days to UNIX days
///
///  - CCSDS epoch: 1958-01-01T00:00:00+00:00
///  - UNIX Epoch: 1970-01-01T00:00:00+00:00
#[inline]
pub const fn ccsds_to_unix_days(ccsds_days: i64) -> i64 {
    ccsds_days + DAYS_CCSDS_TO_UNIX as i64
}

/// Similar to [unix_to_ccsds_days] but converts the epoch instead, which is the number of elpased
/// seconds since the CCSDS and UNIX epoch times.
#[inline]
pub const fn unix_epoch_to_ccsds_epoch(unix_epoch: i64) -> i64 {
    unix_epoch - (DAYS_CCSDS_TO_UNIX as i64 * SECONDS_PER_DAY as i64)
}

/// Convert CCSDS epoch to UNIX epoch.
#[inline]
pub const fn ccsds_epoch_to_unix_epoch(ccsds_epoch: i64) -> i64 {
    ccsds_epoch + (DAYS_CCSDS_TO_UNIX as i64 * SECONDS_PER_DAY as i64)
}

/// Milliseconds of day for the current system time.
#[cfg(feature = "std")]
pub fn ms_of_day_using_sysclock() -> u32 {
    ms_of_day(seconds_since_epoch())
}

/// Milliseconds for the given seconds since epoch.
pub fn ms_of_day(seconds_since_epoch: f64) -> u32 {
    let fraction_ms = seconds_since_epoch - seconds_since_epoch.floor();
    let ms_of_day: u32 = (((seconds_since_epoch.floor() as u32 % SECONDS_PER_DAY) * 1000) as f64
        + fraction_ms)
        .floor() as u32;
    ms_of_day
}

/// Generic writable timestamp trait.
pub trait TimeWriter {
    /// Written length.
    fn len_written(&self) -> usize;

    /// Generic function to convert write a timestamp into a raw buffer.
    /// Returns the number of written bytes on success.
    fn write_to_bytes(&self, bytes: &mut [u8]) -> Result<usize, TimestampError>;

    /// Convert to a owned [alloc::vec::Vec].
    #[cfg(feature = "alloc")]
    fn to_vec(&self) -> Result<alloc::vec::Vec<u8>, TimestampError> {
        let mut vec = alloc::vec![0; self.len_written()];
        self.write_to_bytes(&mut vec)?;
        Ok(vec)
    }
}

/// Genmeric readable timestamp trait.
pub trait TimeReader: Sized {
    /// Create a timestamp from a raw byte buffer.
    fn from_bytes(buf: &[u8]) -> Result<Self, TimestampError>;
}

/// Trait for generic CCSDS time providers.
///
/// The UNIX helper methods and the helper method are not strictly necessary but extremely
/// practical because they are a very common and simple exchange format for time information.
/// Therefore, it was decided to keep them in this trait as well.
pub trait CcsdsTimeProvider {
    /// Length when written to bytes.
    fn len_as_bytes(&self) -> usize;

    /// Returns the pfield of the time provider. The pfield can have one or two bytes depending
    /// on the extension bit (first bit). The time provider should returns a tuple where the first
    /// entry denotes the length of the pfield and the second entry is the value of the pfield
    /// in big endian format.
    fn p_field(&self) -> (usize, [u8; 2]);

    /// CCSDS time code field.
    fn ccdsd_time_code(&self) -> CcsdsTimeCode;

    /// UNIX time as seconds.
    fn unix_secs(&self) -> i64 {
        self.unix_time().secs
    }

    /// Subsecond nanoseconds.
    fn subsec_nanos(&self) -> u32 {
        self.unix_time().subsec_nanos
    }

    /// Subsecond milliseconds.
    fn subsec_millis(&self) -> u16 {
        (self.subsec_nanos() / 1_000_000) as u16
    }

    /// UNIX time.
    fn unix_time(&self) -> UnixTime {
        UnixTime::new(self.unix_secs(), self.subsec_nanos())
    }

    /// [chrono] date time.
    #[cfg(feature = "chrono")]
    fn chrono_date_time(&self) -> chrono::LocalResult<chrono::DateTime<chrono::Utc>> {
        chrono::Utc.timestamp_opt(self.unix_secs(), self.subsec_nanos())
    }

    /// [time] library date] library date time.
    #[cfg(feature = "timelib")]
    fn timelib_date_time(&self) -> Result<time::OffsetDateTime, time::error::ComponentRange> {
        Ok(time::OffsetDateTime::from_unix_timestamp(self.unix_secs())?
            + time::Duration::nanoseconds(self.subsec_nanos().into()))
    }
}

/// UNIX time: Elapsed non-leap seconds since 1970-01-01T00:00:00+00:00 UTC.
///
/// This is a commonly used time format and can therefore also be used as a generic format to
/// convert other CCSDS time formats to and from. The subsecond precision is in nanoseconds
/// similarly to other common time formats and libraries.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct UnixTime {
    secs: i64,
    subsec_nanos: u32,
}

impl UnixTime {
    /// The UNIX epoch time: 1970-01-01T00:00:00+00:00 UTC.
    pub const EPOCH: Self = Self {
        secs: 0,
        subsec_nanos: 0,
    };

    /// The minimum possible `UnixTime`.
    pub const MIN: Self = Self {
        secs: i64::MIN,
        subsec_nanos: 0,
    };

    /// The maximum possible `UnixTime`.
    pub const MAX: Self = Self {
        secs: i64::MAX,
        subsec_nanos: NANOS_PER_SECOND - 1,
    };

    /// Returns [None] if the subsecond nanosecond value is invalid (larger than fraction of a
    /// second)
    pub fn new_checked(unix_seconds: i64, subsec_nanos: u32) -> Option<Self> {
        if subsec_nanos >= NANOS_PER_SECOND {
            return None;
        }
        Some(Self::new(unix_seconds, subsec_nanos))
    }

    /// Returns [None] if the subsecond millisecond value is invalid (larger than fraction of a
    /// second)
    pub fn new_subsec_millis_checked(unix_seconds: i64, subsec_millis: u16) -> Option<Self> {
        if subsec_millis >= 1000 {
            return None;
        }
        Self::new_checked(unix_seconds, subsec_millis as u32 * 1_000_000)
    }

    /// This function will panic if the subsecond value is larger than the fraction of a second.
    /// Use [Self::new_checked] if you want to handle this case without a panic.
    pub const fn new(unix_seconds: i64, subsecond_nanos: u32) -> Self {
        if subsecond_nanos >= NANOS_PER_SECOND {
            panic!("invalid subsecond nanos value");
        }
        Self {
            secs: unix_seconds,
            subsec_nanos: subsecond_nanos,
        }
    }

    /// This function will panic if the subsecond value is larger than the fraction of a second.
    /// Use [Self::new_subsec_millis_checked] if you want to handle this case without a panic.
    pub const fn new_subsec_millis(unix_seconds: i64, subsecond_millis: u16) -> Self {
        if subsecond_millis >= 1000 {
            panic!("invalid subsecond millisecond value");
        }
        Self {
            secs: unix_seconds,
            subsec_nanos: subsecond_millis as u32 * 1_000_000,
        }
    }

    /// New UNIX time with only seconds, subseconds set to zero.
    pub fn new_only_secs(unix_seconds: i64) -> Self {
        Self {
            secs: unix_seconds,
            subsec_nanos: 0,
        }
    }

    /// Sub-second milliseconds.
    #[inline]
    pub fn subsec_millis(&self) -> u16 {
        (self.subsec_nanos / 1_000_000) as u16
    }

    /// Sub-second nanoseconds.
    pub fn subsec_nanos(&self) -> u32 {
        self.subsec_nanos
    }

    /// Create a UNIX timestamp from the current system time.
    #[cfg(feature = "std")]
    pub fn now() -> Result<Self, SystemTimeError> {
        let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
        let epoch = now.as_secs();
        Ok(Self::new(epoch as i64, now.subsec_nanos()))
    }

    /// UNIX timestamp as a floating point number in seconds.
    #[inline]
    pub fn unix_secs_f64(&self) -> f64 {
        self.secs as f64 + (self.subsec_nanos as f64 / 1_000_000_000.0)
    }

    /// UNIX timestamp as seconds, discards the sub-second part.
    pub fn as_secs(&self) -> i64 {
        self.secs
    }

    /// UNIX timestamp as [chrono] date time.
    #[cfg(feature = "chrono")]
    pub fn chrono_date_time(&self) -> chrono::LocalResult<chrono::DateTime<chrono::Utc>> {
        Utc.timestamp_opt(self.secs, self.subsec_nanos)
    }

    /// UNIX timestamp as [time] library date time.
    #[cfg(feature = "timelib")]
    pub fn timelib_date_time(&self) -> Result<time::OffsetDateTime, time::error::ComponentRange> {
        Ok(time::OffsetDateTime::from_unix_timestamp(self.as_secs())?
            + time::Duration::nanoseconds(self.subsec_nanos().into()))
    }

    /// Calculate the difference in milliseconds between two UnixTimestamps
    pub fn diff_in_millis(&self, other: &UnixTime) -> Option<i64> {
        let seconds_difference = self.secs.checked_sub(other.secs)?;
        // Convert seconds difference to milliseconds
        let milliseconds_difference = seconds_difference.checked_mul(1000)?;

        // Calculate the difference in subsecond milliseconds directly
        let subsecond_difference_nanos = self.subsec_nanos as i64 - other.subsec_nanos as i64;

        // Combine the differences
        Some(milliseconds_difference + (subsecond_difference_nanos / 1_000_000))
    }
}

#[cfg(feature = "chrono")]
impl From<chrono::DateTime<chrono::Utc>> for UnixTime {
    fn from(value: chrono::DateTime<chrono::Utc>) -> Self {
        Self::new(value.timestamp(), value.timestamp_subsec_nanos())
    }
}

#[cfg(feature = "timelib")]
impl From<time::OffsetDateTime> for UnixTime {
    fn from(value: time::OffsetDateTime) -> Self {
        Self::new(value.unix_timestamp(), value.nanosecond())
    }
}

impl PartialOrd for UnixTime {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for UnixTime {
    fn cmp(&self, other: &Self) -> Ordering {
        if self == other {
            return Ordering::Equal;
        }
        match self.secs.cmp(&other.secs) {
            Ordering::Less => return Ordering::Less,
            Ordering::Greater => return Ordering::Greater,
            _ => (),
        }

        match self.subsec_millis().cmp(&other.subsec_millis()) {
            Ordering::Less => {
                return if self.secs < 0 {
                    Ordering::Greater
                } else {
                    Ordering::Less
                }
            }
            Ordering::Greater => {
                return if self.secs < 0 {
                    Ordering::Less
                } else {
                    Ordering::Greater
                }
            }
            Ordering::Equal => (),
        }
        Ordering::Equal
    }
}

/// Difference between two UNIX timestamps. The [Duration] type can not contain negative durations,
/// so the sign information is supplied separately.
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct StampDiff {
    /// Positive duration flag.
    pub positive_duration: bool,
    /// Absolute duration.
    pub duration_absolute: Duration,
}

impl Sub for UnixTime {
    type Output = Option<StampDiff>;

    fn sub(self, rhs: Self) -> Self::Output {
        let difference = self.diff_in_millis(&rhs)?;
        Some(if difference < 0 {
            StampDiff {
                positive_duration: false,
                duration_absolute: Duration::from_millis(-difference as u64),
            }
        } else {
            StampDiff {
                positive_duration: true,
                duration_absolute: Duration::from_millis(difference as u64),
            }
        })
    }
}

fn get_new_stamp_after_addition(current_stamp: &UnixTime, duration: Duration) -> UnixTime {
    let mut new_subsec_nanos = current_stamp.subsec_nanos() + duration.subsec_nanos();
    let mut new_unix_seconds = current_stamp.secs;
    let mut increment_seconds = |value: u32| {
        if new_unix_seconds < 0 {
            new_unix_seconds = new_unix_seconds
                .checked_sub(value.into())
                .expect("new unix seconds would exceed i64::MIN");
        } else {
            new_unix_seconds = new_unix_seconds
                .checked_add(value.into())
                .expect("new unix seconds would exceed i64::MAX");
        }
    };
    if new_subsec_nanos >= 1_000_000_000 {
        new_subsec_nanos -= 1_000_000_000;
        increment_seconds(1);
    }
    increment_seconds(
        duration
            .as_secs()
            .try_into()
            .expect("duration seconds exceeds u32::MAX"),
    );
    UnixTime::new(new_unix_seconds, new_subsec_nanos)
}

/// Please note that this operation will panic on the following conditions:
///
/// - Unix seconds after subtraction for stamps before the unix epoch exceeds [i64::MIN].
/// - Unix seconds after addition  exceeds [i64::MAX].
/// - Seconds from duration to add exceeds [u32::MAX].
impl AddAssign<Duration> for UnixTime {
    fn add_assign(&mut self, duration: Duration) {
        *self = get_new_stamp_after_addition(self, duration);
    }
}

/// Please note that this operation will panic for the following conditions:
///
/// - Unix seconds after subtraction for stamps before the unix epoch exceeds [i64::MIN].
/// - Unix seconds after addition  exceeds [i64::MAX].
/// - Unix seconds exceeds [u32::MAX].
impl Add<Duration> for UnixTime {
    type Output = Self;

    fn add(self, duration: Duration) -> Self::Output {
        get_new_stamp_after_addition(&self, duration)
    }
}

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

    fn add(self, duration: Duration) -> Self::Output {
        get_new_stamp_after_addition(self, duration)
    }
}

#[cfg(all(test, feature = "std"))]
mod tests {
    use alloc::string::ToString;
    use chrono::{Datelike, Timelike};
    use std::{format, println};

    use super::{cuc::CucError, *};

    #[allow(dead_code)]
    const UNIX_STAMP_CONST: UnixTime = UnixTime::new(5, 999_999_999);
    #[allow(dead_code)]
    const UNIX_STAMP_CONST_2: UnixTime = UnixTime::new_subsec_millis(5, 999);

    #[test]
    fn test_days_conversion() {
        assert_eq!(unix_to_ccsds_days(DAYS_CCSDS_TO_UNIX.into()), 0);
        assert_eq!(ccsds_to_unix_days(0), DAYS_CCSDS_TO_UNIX.into());
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_get_current_time() {
        let sec_floats = seconds_since_epoch();
        assert!(sec_floats > 0.0);
    }

    #[test]
    fn test_ms_of_day() {
        let ms = ms_of_day(0.0);
        assert_eq!(ms, 0);
        let ms = ms_of_day(5.0);
        assert_eq!(ms, 5000);
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_ccsds_epoch() {
        let now = SystemTime::now()
            .duration_since(SystemTime::UNIX_EPOCH)
            .unwrap();
        let unix_epoch = now.as_secs();
        let ccsds_epoch = unix_epoch_to_ccsds_epoch(now.as_secs() as i64) as u64;
        assert!(ccsds_epoch > unix_epoch);
        assert_eq!((ccsds_epoch - unix_epoch) % SECONDS_PER_DAY as u64, 0);
        let days_diff = (ccsds_epoch - unix_epoch) / SECONDS_PER_DAY as u64;
        assert_eq!(days_diff, -DAYS_CCSDS_TO_UNIX as u64);
    }

    #[test]
    fn basic_unix_stamp_test() {
        let stamp = UnixTime::new_only_secs(-200);
        assert_eq!(stamp.secs, -200);
        assert_eq!(stamp.subsec_millis(), 0);
        let stamp = UnixTime::new_only_secs(250);
        assert_eq!(stamp.secs, 250);
        assert_eq!(stamp.subsec_millis(), 0);
    }

    #[test]
    fn basic_float_unix_stamp_test() {
        let stamp = UnixTime::new_subsec_millis_checked(500, 600).unwrap();
        assert_eq!(stamp.secs, 500);
        let subsec_millis = stamp.subsec_millis();
        assert_eq!(subsec_millis, 600);
        println!("{:?}", (500.6 - stamp.unix_secs_f64()).to_string());
        assert!((500.6 - stamp.unix_secs_f64()).abs() < 0.0001);
    }

    #[test]
    fn test_ord_larger() {
        let stamp0 = UnixTime::new_only_secs(5);
        let stamp1 = UnixTime::new_subsec_millis_checked(5, 500).unwrap();
        let stamp2 = UnixTime::new_only_secs(6);
        assert!(stamp1 > stamp0);
        assert!(stamp2 > stamp0);
        assert!(stamp2 > stamp1);
    }

    #[test]
    fn test_ord_smaller() {
        let stamp0 = UnixTime::new_only_secs(5);
        let stamp1 = UnixTime::new_subsec_millis_checked(5, 500).unwrap();
        let stamp2 = UnixTime::new_only_secs(6);
        assert!(stamp0 < stamp1);
        assert!(stamp0 < stamp2);
        assert!(stamp1 < stamp2);
    }

    #[test]
    fn test_ord_larger_neg_numbers() {
        let stamp0 = UnixTime::new_only_secs(-5);
        let stamp1 = UnixTime::new_subsec_millis_checked(-5, 500).unwrap();
        let stamp2 = UnixTime::new_only_secs(-6);
        assert!(stamp0 > stamp1);
        assert!(stamp0 > stamp2);
        assert!(stamp1 > stamp2);
        assert!(stamp1 >= stamp2);
        assert!(stamp0 >= stamp1);
    }

    #[test]
    fn test_ord_smaller_neg_numbers() {
        let stamp0 = UnixTime::new_only_secs(-5);
        let stamp1 = UnixTime::new_subsec_millis_checked(-5, 500).unwrap();
        let stamp2 = UnixTime::new_only_secs(-6);
        assert!(stamp2 < stamp1);
        assert!(stamp2 < stamp0);
        assert!(stamp1 < stamp0);
        assert!(stamp1 <= stamp0);
        assert!(stamp2 <= stamp1);
    }

    #[allow(clippy::nonminimal_bool)]
    #[test]
    fn test_eq() {
        let stamp0 = UnixTime::new(5, 0);
        let stamp1 = UnixTime::new_only_secs(5);
        assert_eq!(stamp0, stamp1);
        assert!(stamp0 <= stamp1);
        assert!(stamp0 >= stamp1);
        assert!(!(stamp0 < stamp1));
        assert!(!(stamp0 > stamp1));
    }

    #[test]
    fn test_addition() {
        let mut stamp0 = UnixTime::new_only_secs(1);
        stamp0 += Duration::from_secs(5);
        assert_eq!(stamp0.as_secs(), 6);
        assert_eq!(stamp0.subsec_millis(), 0);
        let stamp1 = stamp0 + Duration::from_millis(500);
        assert_eq!(stamp1.secs, 6);
        assert_eq!(stamp1.subsec_millis(), 500);
    }

    #[test]
    fn test_addition_on_ref() {
        let stamp0 = &UnixTime::new_subsec_millis_checked(20, 500).unwrap();
        let stamp1 = stamp0 + Duration::from_millis(2500);
        assert_eq!(stamp1.secs, 23);
        assert_eq!(stamp1.subsec_millis(), 0);
    }

    #[test]
    fn test_as_dt() {
        let stamp = UnixTime::new_only_secs(0);
        let dt = stamp.chrono_date_time().unwrap();
        assert_eq!(dt.year(), 1970);
        assert_eq!(dt.month(), 1);
        assert_eq!(dt.day(), 1);
        assert_eq!(dt.hour(), 0);
        assert_eq!(dt.minute(), 0);
        assert_eq!(dt.second(), 0);
    }

    #[test]
    #[cfg_attr(miri, ignore)]
    fn test_from_now() {
        let stamp_now = UnixTime::now().unwrap();
        let dt_now = stamp_now.chrono_date_time().unwrap();
        assert!(dt_now.year() >= 2020);
    }

    #[test]
    fn test_stamp_diff_positive_0() {
        let stamp_later = UnixTime::new(2, 0);
        let StampDiff {
            positive_duration,
            duration_absolute,
        } = (stamp_later - UnixTime::new(1, 0)).expect("stamp diff error");
        assert!(positive_duration);
        assert_eq!(duration_absolute, Duration::from_secs(1));
    }

    #[test]
    fn test_stamp_diff_positive_1() {
        let stamp_later = UnixTime::new(3, 800 * 1_000_000);
        let stamp_earlier = UnixTime::new_subsec_millis_checked(1, 900).unwrap();
        let StampDiff {
            positive_duration,
            duration_absolute,
        } = (stamp_later - stamp_earlier).expect("stamp diff error");
        assert!(positive_duration);
        assert_eq!(duration_absolute, Duration::from_millis(1900));
    }

    #[test]
    fn test_stamp_diff_negative() {
        let stamp_later = UnixTime::new_subsec_millis_checked(3, 800).unwrap();
        let stamp_earlier = UnixTime::new_subsec_millis_checked(1, 900).unwrap();
        let StampDiff {
            positive_duration,
            duration_absolute,
        } = (stamp_earlier - stamp_later).expect("stamp diff error");
        assert!(!positive_duration);
        assert_eq!(duration_absolute, Duration::from_millis(1900));
    }

    #[test]
    fn test_addition_spillover() {
        let mut stamp0 = UnixTime::new_subsec_millis_checked(1, 900).unwrap();
        stamp0 += Duration::from_millis(100);
        assert_eq!(stamp0.secs, 2);
        assert_eq!(stamp0.subsec_millis(), 0);
        stamp0 += Duration::from_millis(1100);
        assert_eq!(stamp0.secs, 3);
        assert_eq!(stamp0.subsec_millis(), 100);
    }

    #[test]
    fn test_cuc_error_printout() {
        let cuc_error = CucError::InvalidCounterWidth(12);
        let stamp_error = TimestampError::from(cuc_error);
        assert_eq!(stamp_error.to_string(), format!("CUC error: {cuc_error}"));
    }

    #[test]
    #[cfg(feature = "timelib")]
    fn test_unix_stamp_as_timelib_datetime() {
        let stamp_epoch = UnixTime::EPOCH;
        let timelib_dt = stamp_epoch.timelib_date_time().unwrap();
        assert_eq!(timelib_dt.year(), 1970);
        assert_eq!(timelib_dt.month(), time::Month::January);
        assert_eq!(timelib_dt.day(), 1);
        assert_eq!(timelib_dt.hour(), 0);
        assert_eq!(timelib_dt.minute(), 0);
        assert_eq!(timelib_dt.second(), 0);
    }

    #[test]
    #[cfg(feature = "timelib")]
    fn test_unix_stamp_from_timelib_datetime() {
        let timelib_dt = time::OffsetDateTime::UNIX_EPOCH;
        let unix_time = UnixTime::from(timelib_dt);
        let timelib_converted_back = unix_time.timelib_date_time().unwrap();
        assert_eq!(timelib_dt, timelib_converted_back);
    }
}