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 A** (month/day calendar).
6    ///
7    /// Conforms to **CCSDS 301.0-B-4 §3.5.1.1** (Level 1 subset of ISO 8601):
8    ///
9    /// - Example: **`"2025-04-17T14:30:45.123456789Z"`**
10    /// - `T` separator and trailing `Z` (UTC / prime meridian)
11    /// - Leading zeros on calendar and time subfields
12    /// - Fractional seconds: up to 18 digits by default; **trailing zeros trimmed**,
13    ///   and the decimal point omitted if the fraction is zero
14    /// - Digits beyond the requested precision are **truncated** (not rounded)
15    ///
16    /// Round-trips with [`Dt::from_str`](../struct.Dt.html#method.from_str) /
17    /// [`Parts::from_str`](../civil_parts/struct.Parts.html#method.from_str) for
18    /// common ISO-like inputs.
19    ///
20    /// Uses this [`Dt`]'s `target` scale via civil formatting at offset 0.
21    #[inline(always)]
22    pub fn to_str_ccsds(&self) -> Result<String, DtErr> {
23        self.to_str_ccsds_nf(18)
24    }
25
26    /// Same as [`to_str_ccsds`](Self::to_str_ccsds) with a maximum fractional digit
27    /// count (`0`–`18`). Extra digits are truncated; trailing zeros are still trimmed.
28    pub fn to_str_ccsds_nf(&self, max_precision: usize) -> Result<String, DtErr> {
29        let prec = max_precision.min(18);
30        let fmt = alloc::format!("%Y-%m-%dT%H:%M:%S%.{}~fZ", prec);
31        self.to_str_in_offset(&fmt, 0, Lang::En)
32    }
33
34    /// Returns this instant as a **CCSDS ASCII Time Code B** (day-of-year).
35    ///
36    /// Conforms to **CCSDS 301.0-B-4 §3.5.1.2**.
37    ///
38    /// Example: **`"2025-107T14:30:45.123456789Z"`**
39    ///
40    /// Same fractional-digit rules as [`to_str_ccsds`](Self::to_str_ccsds).
41    #[inline(always)]
42    pub fn to_ccsds_doy_str(&self) -> Result<String, DtErr> {
43        self.to_ccsds_doy_str_nf(18)
44    }
45
46    /// Same as [`to_ccsds_doy_str`](Self::to_ccsds_doy_str) with configurable
47    /// maximum fractional precision (`0`–`18`, truncated).
48    pub fn to_ccsds_doy_str_nf(&self, max_precision: usize) -> Result<String, DtErr> {
49        let prec = max_precision.min(18);
50        let fmt = alloc::format!("%Y-%jT%H:%M:%S%.{}~fZ", prec);
51        self.to_str_in_offset(&fmt, 0, Lang::En)
52    }
53}