From 739b810ed309d4df639012d01c2620d9040090dd Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Thu, 14 Mar 2024 13:09:59 +0100 Subject: [PATCH] varous fixes --- src/time/cds.rs | 18 +++++++++--------- src/time/cuc.rs | 14 ++++++-------- src/time/mod.rs | 27 +++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 21 deletions(-) diff --git a/src/time/cds.rs b/src/time/cds.rs index 227eab8..e2ce4fb 100644 --- a/src/time/cds.rs +++ b/src/time/cds.rs @@ -156,19 +156,19 @@ pub fn precision_from_pfield(pfield: u8) -> SubmillisPrecision { /// /// ``` /// use core::time::Duration; -/// use spacepackets::time::cds::{TimeProvider, length_of_day_segment_from_pfield, LengthOfDaySegment}; -/// use spacepackets::time::{TimeWriter, CcsdsTimeCodes, CcsdsTimeProvider}; +/// use spacepackets::time::cds::{CdsTime, length_of_day_segment_from_pfield, LengthOfDaySegment}; +/// use spacepackets::time::{TimeWriter, CcsdsTimeCode, CcsdsTimeProvider}; /// -/// let timestamp_now = TimeProvider::from_now_with_u16_days().unwrap(); +/// let timestamp_now = CdsTime::from_now_with_u16_days().unwrap(); /// let mut raw_stamp = [0; 7]; /// { /// let written = timestamp_now.write_to_bytes(&mut raw_stamp).unwrap(); -/// assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCodes::Cds as u8); +/// assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCode::Cds as u8); /// assert_eq!(written, 7); /// } /// { /// assert_eq!(length_of_day_segment_from_pfield(raw_stamp[0]), LengthOfDaySegment::Short16Bits); -/// let read_result = TimeProvider::from_bytes_with_u16_days(&raw_stamp); +/// let read_result = CdsTime::from_bytes_with_u16_days(&raw_stamp); /// assert!(read_result.is_ok()); /// let stamp_deserialized = read_result.unwrap(); /// assert_eq!(stamp_deserialized.len_as_bytes(), 7); @@ -450,15 +450,15 @@ impl DynCdsTimeProvider for CdsTime {} /// /// ``` /// use spacepackets::time::cds::{ -/// TimeProvider, LengthOfDaySegment, get_dyn_time_provider_from_bytes, SubmillisPrecision, +/// CdsTime, LengthOfDaySegment, get_dyn_time_provider_from_bytes, SubmillisPrecision, /// }; -/// use spacepackets::time::{TimeWriter, CcsdsTimeCodes, CcsdsTimeProvider}; +/// use spacepackets::time::{TimeWriter, CcsdsTimeCode, CcsdsTimeProvider}; /// -/// let timestamp_now = TimeProvider::new_with_u16_days(24, 24); +/// let timestamp_now = CdsTime::new_with_u16_days(24, 24); /// let mut raw_stamp = [0; 7]; /// { /// let written = timestamp_now.write_to_bytes(&mut raw_stamp).unwrap(); -/// assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCodes::Cds as u8); +/// assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCode::Cds as u8); /// assert_eq!(written, 7); /// } /// { diff --git a/src/time/cuc.rs b/src/time/cuc.rs index 66f4cf6..6aa6c27 100644 --- a/src/time/cuc.rs +++ b/src/time/cuc.rs @@ -15,8 +15,8 @@ use crate::ByteConversionError; #[cfg(feature = "std")] use super::StdTimestampError; use super::{ - ccsds_epoch_to_unix_epoch, unix_epoch_to_ccsds_epoch, CcsdsTimeCode, CcsdsTimeProvider, - TimeReader, TimeWriter, TimestampError, UnixTime, + ccsds_epoch_to_unix_epoch, ccsds_time_code_from_p_field, unix_epoch_to_ccsds_epoch, + CcsdsTimeCode, CcsdsTimeProvider, TimeReader, TimeWriter, TimestampError, UnixTime, }; #[cfg(feature = "std")] use std::error::Error; @@ -26,9 +26,6 @@ use std::time::SystemTime; #[cfg(feature = "chrono")] use chrono::Datelike; -#[cfg(feature = "alloc")] -use super::ccsds_time_code_from_p_field; - const MIN_CUC_LEN: usize = 2; /// Base value for the preamble field for a time field parser to determine the time field type. @@ -179,21 +176,22 @@ pub struct FractionalPart(FractionalResolution, u32); /// /// ``` /// use spacepackets::time::cuc::{FractionalResolution, CucTime}; -/// use spacepackets::time::{TimeWriter, CcsdsTimeCodes, TimeReader, CcsdsTimeProvider}; +/// use spacepackets::time::{TimeWriter, CcsdsTimeCode, TimeReader, CcsdsTimeProvider}; /// /// const LEAP_SECONDS: u32 = 37; +/// /// // Highest fractional resolution /// let timestamp_now = CucTime::from_now(FractionalResolution::SixtyNs, LEAP_SECONDS) /// .expect("creating cuc stamp failed"); /// let mut raw_stamp = [0; 16]; /// { /// let written = timestamp_now.write_to_bytes(&mut raw_stamp).expect("writing timestamp failed"); -/// assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCodes::CucCcsdsEpoch as u8); +/// assert_eq!((raw_stamp[0] >> 4) & 0b111, CcsdsTimeCode::CucCcsdsEpoch as u8); /// // 1 byte preamble + 4 byte counter + 3 byte fractional part /// assert_eq!(written, 8); /// } /// { -/// let read_result = TimeProviderCcsdsEpoch::from_bytes_with_leap_seconds(&raw_stamp, LEAP_SECONDS); +/// let read_result = CucTime::from_bytes(&raw_stamp); /// assert!(read_result.is_ok()); /// let stamp_deserialized = read_result.unwrap(); /// assert_eq!(stamp_deserialized, timestamp_now); diff --git a/src/time/mod.rs b/src/time/mod.rs index b9deea9..d971885 100644 --- a/src/time/mod.rs +++ b/src/time/mod.rs @@ -1,7 +1,7 @@ //! CCSDS Time Code Formats according to [CCSDS 301.0-B-4](https://public.ccsds.org/Pubs/301x0b4e1.pdf) use crate::ByteConversionError; #[cfg(feature = "chrono")] -use chrono::{DateTime, LocalResult, TimeZone, Utc}; +use chrono::{TimeZone, Utc}; use core::cmp::Ordering; use core::fmt::{Display, Formatter}; use core::ops::{Add, AddAssign, Sub}; @@ -359,7 +359,7 @@ impl UnixTime { #[cfg(feature = "chrono")] #[cfg_attr(doc_cfg, doc(cfg(feature = "chrono")))] - pub fn chrono_date_time(&self) -> LocalResult> { + pub fn chrono_date_time(&self) -> chrono::LocalResult> { Utc.timestamp_opt(self.secs, self.subsec_nanos) } @@ -384,12 +384,22 @@ impl UnixTime { } } -impl From> for UnixTime { - fn from(value: DateTime) -> Self { +#[cfg(feature = "chrono")] +#[cfg_attr(doc_cfg, doc(cfg(feature = "chrono")))] +impl From> for UnixTime { + fn from(value: chrono::DateTime) -> Self { Self::new(value.timestamp(), value.timestamp_subsec_nanos()) } } +#[cfg(feature = "timelib")] +#[cfg_attr(doc_cfg, doc(cfg(feature = "timelib")))] +impl From for UnixTime { + fn from(value: time::OffsetDateTime) -> Self { + Self::new(value.unix_timestamp(), value.nanosecond()) + } +} + impl PartialOrd for UnixTime { fn partial_cmp(&self, other: &Self) -> Option { Some(self.cmp(other)) @@ -739,4 +749,13 @@ mod tests { assert_eq!(timelib_dt.minute(), 0); assert_eq!(timelib_dt.second(), 0); } + + #[test] + #[cfg(feature = "timelib")] + fn test_unix_stamp_from_timelib_datetime() { + let timelib_dt = time::OffsetDateTime::UNIX_EPOCH; + let unix_time = UnixTime::from(timelib_dt); + let timelib_converted_back = unix_time.timelib_date_time().unwrap(); + assert_eq!(timelib_dt, timelib_converted_back); + } }