added ASCII time code module
Rust/spacepackets/pipeline/head There was a failure building this commit Details

This commit is contained in:
Robin Müller 2022-12-05 09:48:11 +01:00
parent 8358d5ed1c
commit 62c1462930
No known key found for this signature in database
GPG Key ID: 71B58F8A3CDFA9AC
2 changed files with 50 additions and 1 deletions

View File

@ -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

View File

@ -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<Utc>) -> DelayedFormat<StrftimeItems<'static>> {
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<Utc>,
) -> DelayedFormat<StrftimeItems<'static>> {
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);