Skip to main content

deep_time/time_parts/
to_ccsds_bin.rs

1use crate::{Dt, DtErr, TimeParts};
2
3impl TimeParts {
4    /// Formats this [`TimeParts`] as a **CCSDS C (CUC)** binary time code.
5    ///
6    /// - Fully configurable for round-tripping with [`from_ccsds_c`].
7    /// - Conforms to **CCSDS 301.0-B-4 §3.2 (Level 1)**, including full support
8    ///   for the extended P-field (second octet) when `n_coarse > 4` or `n_frac > 3`.
9    ///
10    /// # Parameters
11    /// - `n_coarse`: 1–7 (number of coarse-time octets)
12    /// - `n_frac`:   0–10 (number of fractional octets)
13    /// - `extension`: advisory flag (ignored when larger sizes force the second octet)
14    #[inline]
15    pub fn to_ccsds_c(
16        &self,
17        n_coarse: u8,
18        n_frac: u8,
19        extension: bool,
20    ) -> Result<([u8; Dt::CCSDS_C_AND_D_MAX_SIZE], usize), DtErr> {
21        self.to_dt()?
22            .to_ccsds_c(self.scale, n_coarse, n_frac, extension)
23    }
24
25    /// Formats this [`TimeParts`] as a **CCSDS D (CDS)** binary time code.
26    ///
27    /// - Fully configurable for round-tripping with [`from_ccsds_d`].
28    /// - Conforms to CCSDS 301.0-B-4 §3.3 (Level 1): UTC day count + ms-of-day since
29    ///   1958-01-01 UTC.
30    #[inline]
31    pub fn to_ccsds_d(
32        &self,
33        n_day: u8,
34        sub_ms_code: u8,
35        extension: bool,
36    ) -> Result<([u8; Dt::CCSDS_C_AND_D_MAX_SIZE], usize), DtErr> {
37        self.to_dt()?
38            .to_ccsds_d(self.scale, n_day, sub_ms_code, extension)
39    }
40
41    /// Formats this [`TimeParts`] as a **CCSDS CCS (Calendar Segmented Time Code)**.
42    ///
43    /// Implements **CCSDS 301.0-B-4 §3.4** (Level 1 only).
44    ///
45    /// # Parameters
46    /// - `use_doy`: `false` = Month/Day variant (most common), `true` = Day-of-Year variant
47    /// - `n_subsec`: Number of subsecond BCD octets (`0`–`6`). Each octet holds 2 decimal digits.
48    ///
49    /// # Returns
50    /// `(buffer, written_len)` — the P-field + T-field (big-endian BCD).
51    ///
52    /// # Precision & Rounding
53    /// Fractional seconds are rounded to the nearest representable value at the chosen precision
54    /// (exactly as `to_ccsds_d` does for milliseconds).
55    #[inline]
56    pub fn to_ccsds_ccs(
57        &self,
58        use_doy: bool,
59        n_subsec: u8,
60    ) -> Result<([u8; Dt::CCSDS_CCS_MAX_SIZE], usize), DtErr> {
61        self.to_dt()?.to_ccsds_ccs(self.scale, use_doy, n_subsec)
62    }
63
64    /// Convenience method that automatically selects the most appropriate
65    /// CCSDS binary time code based on this `TimeParts`’s [`Scale`].
66    ///
67    /// # Automatic selection (matches common mission practice)
68    /// - `Scale::TAI` → **CUC** (4 coarse + 4 fractional bytes)
69    /// - Any other `Scale` (UTC, TT, GPS, TCG, …) → converted to UTC and uses **CDS**
70    ///   (2 day bytes + 4 ms bytes + 2-byte sub-ms)
71    #[inline]
72    pub fn to_ccsds_bin(&self) -> Result<([u8; Dt::CCSDS_C_AND_D_MAX_SIZE], usize), DtErr> {
73        self.to_dt()?.to_ccsds_bin(self.scale)
74    }
75}