diff --git a/CHANGELOG.md b/CHANGELOG.md index f2ef33a..0c58f6d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -34,6 +34,7 @@ to check all the API changes in the **Changed** chapter. - `UnixTimestamp` renamed to `UnixTime` - `UnixTime` seconds are now private and can be retrieved using the `secs` member method. - `UnixTime::new` renamed to `UnixTime::new_checked`. +- `UnixTime::secs` renamed to `UnixTime::as_secs`. - `UnixTime` now has a nanosecond subsecond precision. The `new` constructor now expects nanoseconds as the second argument. - Added new `UnixTime::new_subsec_millis` and `UnixTime::new_subsec_millis_checked` API @@ -46,6 +47,7 @@ to check all the API changes in the **Changed** chapter. - Error handling for ECSS and time module is more granular now, with a new `DateBeforeCcsdsEpochError` error and a `DateBeforeCcsdsEpoch` enum variant for both `CdsError` and `CucError`. +- Time API `from_now*` API renamed to `now*`. # [v0.11.0-rc.0] 2024-03-04 diff --git a/src/time/cds.rs b/src/time/cds.rs index 5074b92..57edb01 100644 --- a/src/time/cds.rs +++ b/src/time/cds.rs @@ -176,7 +176,7 @@ pub fn precision_from_pfield(pfield: u8) -> SubmillisPrecision { /// use spacepackets::time::cds::{CdsTime, length_of_day_segment_from_pfield, LengthOfDaySegment}; /// use spacepackets::time::{TimeWriter, CcsdsTimeCode, CcsdsTimeProvider}; /// -/// let timestamp_now = CdsTime::from_now_with_u16_days().unwrap(); +/// let timestamp_now = CdsTime::now_with_u16_days().unwrap(); /// let mut raw_stamp = [0; 7]; /// { /// let written = timestamp_now.write_to_bytes(&mut raw_stamp).unwrap(); @@ -822,7 +822,7 @@ impl CdsTime { /// Generate a time stamp from the current time using the system clock. #[cfg(feature = "std")] - pub fn from_now_with_u24_days() -> Result { + pub fn now_with_u24_days() -> Result { Self::from_now_generic(LengthOfDaySegment::Long24Bits) } @@ -926,7 +926,7 @@ impl CdsTime { /// Generate a time stamp from the current time using the system clock. #[cfg(feature = "std")] - pub fn from_now_with_u16_days() -> Result { + pub fn now_with_u16_days() -> Result { Self::from_now_generic(LengthOfDaySegment::Short16Bits) } @@ -937,7 +937,7 @@ impl CdsTime { /// This function will return [CdsError::DateBeforeCcsdsEpoch] if the time is before the CCSDS /// epoch (1958-01-01T00:00:00+00:00) or the CCSDS days value exceeds the allowed bit width /// (24 bits). - pub fn from_unix_stamp_with_u16_days( + pub fn from_unix_time_with_u16_days( unix_stamp: &UnixTime, submillis_prec: SubmillisPrecision, ) -> Result { @@ -962,7 +962,7 @@ impl CdsTime { /// Like [Self::from_now_with_u16_days] but with microsecond sub-millisecond precision. #[cfg(feature = "std")] - pub fn from_now_with_u16_days_us_precision() -> Result { + pub fn now_with_u16_days_us_precision() -> Result { Self::from_now_generic_us_prec(LengthOfDaySegment::Short16Bits) } @@ -1608,14 +1608,14 @@ mod tests { #[test] fn test_time_now() { - let timestamp_now = CdsTime::from_now_with_u16_days().unwrap(); + let timestamp_now = CdsTime::now_with_u16_days().unwrap(); let compare_stamp = chrono::Utc::now(); generic_now_test(timestamp_now, compare_stamp); } #[test] fn test_time_now_us_prec() { - let timestamp_now = CdsTime::from_now_with_u16_days_us_precision().unwrap(); + let timestamp_now = CdsTime::now_with_u16_days_us_precision().unwrap(); let compare_stamp = chrono::Utc::now(); generic_now_test(timestamp_now, compare_stamp); } @@ -1921,7 +1921,7 @@ mod tests { fn test_creation_from_unix_stamp_0_u16_days() { let unix_secs = 0; let subsec_millis = 0; - let time_provider = CdsTime::from_unix_stamp_with_u16_days( + let time_provider = CdsTime::from_unix_time_with_u16_days( &UnixTime::new(unix_secs, subsec_millis), SubmillisPrecision::Absent, ) @@ -1950,7 +1950,7 @@ mod tests { .unwrap() .and_local_timezone(chrono::Utc) .unwrap(); - let time_provider = CdsTime::from_unix_stamp_with_u16_days( + let time_provider = CdsTime::from_unix_time_with_u16_days( &datetime_utc.into(), SubmillisPrecision::Absent, ) @@ -1970,7 +1970,7 @@ mod tests { fn test_creation_0_ccsds_days() { let unix_secs = DAYS_CCSDS_TO_UNIX as i64 * SECONDS_PER_DAY as i64; let subsec_millis = 0; - let time_provider = CdsTime::from_unix_stamp_with_u16_days( + let time_provider = CdsTime::from_unix_time_with_u16_days( &UnixTime::new(unix_secs, subsec_millis), SubmillisPrecision::Absent, ) @@ -1982,7 +1982,7 @@ mod tests { fn test_invalid_creation_from_unix_stamp_days_too_large() { let invalid_unix_secs: i64 = (u16::MAX as i64 + 1) * SECONDS_PER_DAY as i64; let subsec_millis = 0; - match CdsTime::from_unix_stamp_with_u16_days( + match CdsTime::from_unix_time_with_u16_days( &UnixTime::new(invalid_unix_secs, subsec_millis), SubmillisPrecision::Absent, ) { @@ -2009,7 +2009,7 @@ mod tests { // precisely 31-12-1957 23:59:55 let unix_secs = DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32 - 5; let subsec_millis = 0; - match CdsTime::from_unix_stamp_with_u16_days( + match CdsTime::from_unix_time_with_u16_days( &UnixTime::new(unix_secs as i64, subsec_millis), SubmillisPrecision::Absent, ) { @@ -2309,7 +2309,7 @@ mod tests { #[test] #[cfg(feature = "serde")] fn test_serialization() { - let stamp_now = CdsTime::from_now_with_u16_days().expect("Error retrieving time"); + let stamp_now = CdsTime::now_with_u16_days().expect("Error retrieving time"); let val = to_allocvec(&stamp_now).expect("Serializing timestamp failed"); assert!(val.len() > 0); let stamp_deser: CdsTime = from_bytes(&val).expect("Stamp deserialization failed"); diff --git a/src/time/cuc.rs b/src/time/cuc.rs index a14aeb3..31ca341 100644 --- a/src/time/cuc.rs +++ b/src/time/cuc.rs @@ -249,7 +249,7 @@ impl FractionalPart { /// const LEAP_SECONDS: u32 = 37; /// /// // Highest fractional resolution -/// let timestamp_now = CucTime::from_now(FractionalResolution::SixtyNs, LEAP_SECONDS) +/// let timestamp_now = CucTime::now(FractionalResolution::SixtyNs, LEAP_SECONDS) /// .expect("creating cuc stamp failed"); /// let mut raw_stamp = [0; 16]; /// { @@ -359,7 +359,7 @@ impl CucTime { /// must be applied on top of the UTC based time retrieved from the system in addition to the /// conversion to the CCSDS epoch. #[cfg(feature = "std")] - pub fn from_now( + pub fn now( fraction_resolution: FractionalResolution, leap_seconds: u32, ) -> Result { @@ -913,7 +913,7 @@ mod tests { #[test] fn test_datetime_now() { let now = chrono::Utc::now(); - let cuc_now = CucTime::from_now(FractionalResolution::SixtyNs, LEAP_SECONDS); + let cuc_now = CucTime::now(FractionalResolution::SixtyNs, LEAP_SECONDS); assert!(cuc_now.is_ok()); let cuc_now = cuc_now.unwrap(); let ccsds_cuc = cuc_now.to_leap_sec_helper(LEAP_SECONDS); @@ -1251,6 +1251,7 @@ mod tests { ); assert_eq!(stamp.fractions().counter(), 0); let res = stamp.update_from_now(LEAP_SECONDS); + assert!(res.is_ok()); } diff --git a/src/time/mod.rs b/src/time/mod.rs index dc7ef42..139e816 100644 --- a/src/time/mod.rs +++ b/src/time/mod.rs @@ -345,7 +345,7 @@ impl UnixTime { } #[cfg(feature = "std")] - pub fn from_now() -> Result { + pub fn now() -> Result { let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?; let epoch = now.as_secs(); Ok(Self::new(epoch as i64, now.subsec_nanos())) @@ -356,7 +356,7 @@ impl UnixTime { self.secs as f64 + (self.subsec_nanos as f64 / 1_000_000_000.0) } - pub fn secs(&self) -> i64 { + pub fn as_secs(&self) -> i64 { self.secs } @@ -367,7 +367,7 @@ impl UnixTime { #[cfg(feature = "timelib")] pub fn timelib_date_time(&self) -> Result { - Ok(time::OffsetDateTime::from_unix_timestamp(self.secs())? + Ok(time::OffsetDateTime::from_unix_timestamp(self.as_secs())? + time::Duration::nanoseconds(self.subsec_nanos().into())) } @@ -649,7 +649,7 @@ mod tests { fn test_addition() { let mut stamp0 = UnixTime::new_only_secs(1); stamp0 += Duration::from_secs(5); - assert_eq!(stamp0.secs(), 6); + assert_eq!(stamp0.as_secs(), 6); assert_eq!(stamp0.subsec_millis(), 0); let stamp1 = stamp0 + Duration::from_millis(500); assert_eq!(stamp1.secs, 6); @@ -678,7 +678,7 @@ mod tests { #[test] fn test_from_now() { - let stamp_now = UnixTime::from_now().unwrap(); + let stamp_now = UnixTime::now().unwrap(); let dt_now = stamp_now.chrono_date_time().unwrap(); assert!(dt_now.year() >= 2020); }