Skip to main content

deep_time/dt/
to_str_ccsds.rs

1use crate::{Dt, DtErr, Scale};
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    ///
9    /// - Uses `T` separator and trailing `Z`.
10    /// - Fractional seconds are trimmed (no trailing zeros, no dot if zero).
11    /// - **Perfect round-trip** with `Dt::from_str_ccsds` / `TimeParts::from_str_ccsds`.
12    #[inline]
13    pub fn to_str_ccsds(&self, current: Scale) -> Result<String, DtErr> {
14        self.to_str_ccsds_nf(current, 18)
15    }
16
17    /// Same as [`to_str_ccsds`] but lets you control the maximum number of fractional digits (0–18).
18    pub fn to_str_ccsds_nf(&self, current: Scale, max_precision: usize) -> Result<String, DtErr> {
19        let prec = max_precision.min(18);
20        let fmt = alloc::format!("%Y-%m-%dT%H:%M:%S%.{}~fZ", prec);
21        self.to_str_with_offset(current, &fmt, 0)
22    }
23
24    /// Returns this instant as a **CCSDS ASCII Time Code B** (day-of-year variant).
25    ///
26    /// Example: `"2025-107T14:30:45.123456789Z"`
27    #[inline]
28    pub fn to_ccsds_doy_str(&self, current: Scale) -> Result<String, DtErr> {
29        self.to_ccsds_doy_str_nf(current, 18)
30    }
31
32    /// Same as [`to_ccsds_doy_str`] but with configurable fractional precision.
33    pub fn to_ccsds_doy_str_nf(
34        &self,
35        current: Scale,
36        max_precision: usize,
37    ) -> Result<String, DtErr> {
38        let prec = max_precision.min(18);
39        let fmt = alloc::format!("%Y-%jT%H:%M:%S%.{}~fZ", prec);
40        self.to_str_with_offset(current, &fmt, 0)
41    }
42}