Skip to main content

deep_time/dt/
to_str_ccsds.rs

1use crate::{Dt, DtErr, Lang};
2use alloc::string::String;
3
4impl Dt {
5    /// Returns this instant as a **CCSDS ASCII Time Code** (calendar variant A).
6    ///
7    /// - Example: **`"2025-04-17T14:30:45.123456789Z"`**
8    /// - Uses `T` separator and trailing `Z`.
9    /// - Fractional seconds are trimmed (no trailing zeros, no dot if zero).
10    /// - Round-trip with [`Dt::from_str_iso`] / [`TimeParts::from_str_iso`].
11    #[inline(always)]
12    pub fn to_str_ccsds(&self) -> Result<String, DtErr> {
13        self.to_str_ccsds_nf(18)
14    }
15
16    /// Same as [`to_str_ccsds`] but lets you control the maximum number of fractional digits (0–18).
17    pub fn to_str_ccsds_nf(&self, max_precision: usize) -> Result<String, DtErr> {
18        let prec = max_precision.min(18);
19        let fmt = alloc::format!("%Y-%m-%dT%H:%M:%S%.{}~fZ", prec);
20        self.to_str_in_offset(&fmt, 0, Lang::En)
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(always)]
27    pub fn to_ccsds_doy_str(&self) -> Result<String, DtErr> {
28        self.to_ccsds_doy_str_nf(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        let prec = max_precision.min(18);
34        let fmt = alloc::format!("%Y-%jT%H:%M:%S%.{}~fZ", prec);
35        self.to_str_in_offset(&fmt, 0, Lang::En)
36    }
37}