diff --git a/Cargo.toml b/Cargo.toml index 6058e82..184d8f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -37,7 +37,7 @@ version = "1.0" default = ["std", "dep:serde"] std = ["chrono/std", "chrono/clock", "alloc"] serde = ["chrono/serde"] -alloc = ["postcard/alloc"] +alloc = ["postcard/alloc", "chrono/alloc"] [package.metadata.docs.rs] all-features = true diff --git a/src/time.rs b/src/time.rs index 0ae8141..47c9655 100644 --- a/src/time.rs +++ b/src/time.rs @@ -268,6 +268,28 @@ impl TimeReader for CdsShortTimeProvider { } } +pub mod ascii { + use chrono::format::{DelayedFormat, StrftimeItems}; + use chrono::{DateTime, Utc}; + + pub const FMT_STR_CODE_A_WITH_SIZE: (&str, usize) = ("%FT%T%.3f", 23); + pub const FMT_STR_CODE_A_TERMINATED_WITH_SIZE: (&str, usize) = ("%FT%T%.3fZ", 24); + + #[cfg(feature = "alloc")] + #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] + pub fn generate_time_code_a(date: &DateTime) -> DelayedFormat> { + date.format(FMT_STR_CODE_A_WITH_SIZE.0) + } + + #[cfg(feature = "alloc")] + #[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))] + pub fn generate_time_code_a_terminated( + date: &DateTime, + ) -> DelayedFormat> { + date.format(FMT_STR_CODE_A_TERMINATED_WITH_SIZE.0) + } +} + #[cfg(all(test, feature = "std"))] mod tests { use super::*; @@ -278,6 +300,33 @@ mod tests { #[cfg(feature = "serde")] use postcard::{from_bytes, to_allocvec}; + #[test] + fn test_ascii_timestamp_unterminated() { + let date = Utc::now(); + let stamp_formatter = ascii::generate_time_code_a(&date); + let stamp = format!("{}", stamp_formatter); + let t_sep = stamp.find("T"); + assert!(t_sep.is_some()); + assert_eq!(t_sep.unwrap(), 10); + assert_eq!(stamp.len(), 23); + assert_eq!(stamp.len(), ascii::FMT_STR_CODE_A_WITH_SIZE.1); + } + + #[test] + fn test_ascii_timestamp_terminated() { + let date = Utc::now(); + let stamp_formatter = ascii::generate_time_code_a_terminated(&date); + let stamp = format!("{}", stamp_formatter); + let t_sep = stamp.find("T"); + assert!(t_sep.is_some()); + assert_eq!(t_sep.unwrap(), 10); + let z_terminator = stamp.find("Z"); + assert!(z_terminator.is_some()); + assert_eq!(z_terminator.unwrap(), 23); + assert_eq!(stamp.len(), 24); + assert_eq!(stamp.len(), ascii::FMT_STR_CODE_A_TERMINATED_WITH_SIZE.1); + } + #[test] fn test_creation() { assert_eq!(unix_to_ccsds_days(DAYS_CCSDS_TO_UNIX), 0);