varous fixes
All checks were successful
Rust/spacepackets/pipeline/head This commit looks good
Rust/spacepackets/pipeline/pr-main This commit looks good

This commit is contained in:
Robin Müller 2024-03-14 13:09:59 +01:00
parent e0bd127d2f
commit 739b810ed3
Signed by: muellerr
GPG Key ID: A649FB78196E3849
3 changed files with 38 additions and 21 deletions

View File

@ -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<DaysLen24Bits> {}
///
/// ```
/// 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);
/// }
/// {

View File

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

View File

@ -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<DateTime<Utc>> {
pub fn chrono_date_time(&self) -> chrono::LocalResult<chrono::DateTime<chrono::Utc>> {
Utc.timestamp_opt(self.secs, self.subsec_nanos)
}
@ -384,12 +384,22 @@ impl UnixTime {
}
}
impl From<DateTime<Utc>> for UnixTime {
fn from(value: DateTime<Utc>) -> Self {
#[cfg(feature = "chrono")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "chrono")))]
impl From<chrono::DateTime<chrono::Utc>> for UnixTime {
fn from(value: chrono::DateTime<chrono::Utc>) -> Self {
Self::new(value.timestamp(), value.timestamp_subsec_nanos())
}
}
#[cfg(feature = "timelib")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "timelib")))]
impl From<time::OffsetDateTime> 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<Ordering> {
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);
}
}