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`](../struct.Dt.html#method.from_str_iso) /
11 /// [`Parts::from_str_iso`](../civil_parts/struct.Parts.html#method.from_str_iso).
12 #[inline(always)]
13 pub fn to_str_ccsds(&self) -> Result<String, DtErr> {
14 self.to_str_ccsds_nf(18)
15 }
16
17 /// Same as [`to_str_ccsds`](../struct.Dt.html#method.to_str_ccsds) but lets you control the maximum number of fractional digits (0–18).
18 pub fn to_str_ccsds_nf(&self, 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_in_offset(&fmt, 0, Lang::En)
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(always)]
28 pub fn to_ccsds_doy_str(&self) -> Result<String, DtErr> {
29 self.to_ccsds_doy_str_nf(18)
30 }
31
32 /// Same as [`to_ccsds_doy_str`](../struct.Dt.html#method.to_ccsds_doy_str) but with configurable fractional precision.
33 pub fn to_ccsds_doy_str_nf(&self, max_precision: usize) -> Result<String, DtErr> {
34 let prec = max_precision.min(18);
35 let fmt = alloc::format!("%Y-%jT%H:%M:%S%.{}~fZ", prec);
36 self.to_str_in_offset(&fmt, 0, Lang::En)
37 }
38}