Skip to main content

deep_time/time_parts/
to_ccsds_str.rs

1use crate::{DtErr, TimeParts};
2use alloc::string::String;
3
4impl TimeParts {
5    /// Returns this instant as a **CCSDS ASCII Time Code** (calendar variant A).
6    ///
7    /// Example: `"2025-04-17T14:30:45.123456789Z"`
8    ///
9    /// - Uses `T` separator and trailing `Z`.
10    /// - Fractional seconds are trimmed (no trailing zeros, no dot if zero).
11    /// - Round-trips with `Dt::from_ccsds_str` / `TimeParts::from_ccsds_str`.
12    #[inline]
13    pub fn to_ccsds_str(&self) -> Result<String, DtErr> {
14        self.to_dt()?.to_ccsds_str(self.scale)
15    }
16
17    /// Same as [`to_ccsds_str`] but lets you control the maximum number of fractional digits (0–18).
18    pub fn to_ccsds_str_nf(&self, max_precision: usize) -> Result<String, DtErr> {
19        self.to_dt()?
20            .to_ccsds_str_nf(self.scale, max_precision)
21    }
22
23    /// Returns this instant as a **CCSDS ASCII Time Code B** (day-of-year variant).
24    ///
25    /// Example: `"2025-107T14:30:45.123456789Z"`
26    #[inline]
27    pub fn to_ccsds_doy_str(&self) -> Result<String, DtErr> {
28        self.to_dt()?.to_ccsds_doy_str_nf(self.scale, 18)
29    }
30
31    /// Same as [`to_ccsds_doy_str`] but with configurable fractional precision.
32    pub fn to_ccsds_doy_str_nf(&self, max_precision: usize) -> Result<String, DtErr> {
33        self.to_dt()?
34            .to_ccsds_doy_str_nf(self.scale, max_precision)
35    }
36}