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 for the
8    /// 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_time_point()?
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 1958-01-01 UTC.
29    #[inline]
30    pub fn to_ccsds_d(
31        &self,
32        n_day: u8,
33        sub_ms_code: u8,
34        extension: bool,
35    ) -> Result<([u8; Dt::CCSDS_C_AND_D_MAX_SIZE], usize), DtErr> {
36        self.to_time_point()?
37            .to_ccsds_d(self.scale, n_day, sub_ms_code, extension)
38    }
39
40    /// Formats this [`TimeParts`] as a **CCSDS CCS (Calendar Segmented Time Code)**.
41    ///
42    /// Implements **CCSDS 301.0-B-4 §3.4** (Level 1 only).
43    ///
44    /// # Parameters
45    /// - `use_doy`: `false` = Month/Day variant (most common), `true` = Day-of-Year variant
46    /// - `n_subsec`: Number of subsecond BCD octets (`0`–`6`). Each octet holds 2 decimal digits.
47    ///
48    /// # Returns
49    /// `(buffer, written_len)` — the P-field + T-field (big-endian BCD).
50    ///
51    /// # Precision & Rounding
52    /// Fractional seconds are rounded to the nearest representable value at the chosen precision
53    /// (exactly as `to_ccsds_d` does for milliseconds).
54    #[inline]
55    pub fn to_ccsds_ccs(
56        &self,
57        use_doy: bool,
58        n_subsec: u8,
59    ) -> Result<([u8; Dt::CCSDS_CCS_MAX_SIZE], usize), DtErr> {
60        self.to_time_point()?
61            .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_time_point()?.to_ccsds_bin(self.scale)
74    }
75}