Update ECSS code
Some checks failed
Rust/spacepackets/pipeline/pr-main There was a failure building this commit
Rust/spacepackets/pipeline/head There was a failure building this commit

This commit is contained in:
Robin Müller 2024-03-29 14:03:09 +01:00
parent 1426cb514b
commit ce9ca14b8f
Signed by: muellerr
GPG Key ID: A649FB78196E3849
6 changed files with 60 additions and 60 deletions

View File

@ -34,6 +34,7 @@ to check all the API changes in the **Changed** chapter.
- `UnixTimestamp` renamed to `UnixTime` - `UnixTimestamp` renamed to `UnixTime`
- `UnixTime` seconds are now private and can be retrieved using the `secs` member method. - `UnixTime` seconds are now private and can be retrieved using the `secs` member method.
- `UnixTime::new` renamed to `UnixTime::new_checked`. - `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 - `UnixTime` now has a nanosecond subsecond precision. The `new` constructor now expects
nanoseconds as the second argument. nanoseconds as the second argument.
- Added new `UnixTime::new_subsec_millis` and `UnixTime::new_subsec_millis_checked` API - Added new `UnixTime::new_subsec_millis` and `UnixTime::new_subsec_millis_checked` API
@ -48,6 +49,7 @@ to check all the API changes in the **Changed** chapter.
`CdsError` and `CucError`. `CdsError` and `CucError`.
- `PusTmCreator` now has two lifetimes: One for the raw source data buffer and one for the - `PusTmCreator` now has two lifetimes: One for the raw source data buffer and one for the
raw timestamp. raw timestamp.
- Time API `from_now*` API renamed to `now*`.
## Removed ## Removed

View File

@ -237,7 +237,7 @@ impl<'raw_data> PusTcCreator<'raw_data> {
/// and subservice type /// and subservice type
/// * `app_data` - Custom application data /// * `app_data` - Custom application data
/// * `set_ccsds_len` - Can be used to automatically update the CCSDS space packet data length /// * `set_ccsds_len` - Can be used to automatically update the CCSDS space packet data length
/// field. If this is not set to true, [PusTc::update_ccsds_data_len] can be called to set /// field. If this is not set to true, [Self::update_ccsds_data_len] can be called to set
/// the correct value to this field manually /// the correct value to this field manually
pub fn new( pub fn new(
sp_header: &mut SpHeader, sp_header: &mut SpHeader,
@ -258,7 +258,7 @@ impl<'raw_data> PusTcCreator<'raw_data> {
pus_tc pus_tc
} }
/// Simplified version of the [PusTcCreator::new] function which allows to only specify service /// Simplified version of the [Self::new] function which allows to only specify service
/// and subservice instead of the full PUS TC secondary header. /// and subservice instead of the full PUS TC secondary header.
pub fn new_simple( pub fn new_simple(
sph: &mut SpHeader, sph: &mut SpHeader,
@ -302,7 +302,7 @@ impl<'raw_data> PusTcCreator<'raw_data> {
sp_header_impls!(); sp_header_impls!();
/// Calculate the CCSDS space packet data length field and sets it /// Calculate the CCSDS space packet data length field and sets it
/// This is called automatically if the `set_ccsds_len` argument in the [PusTc::new] call was /// This is called automatically if the `set_ccsds_len` argument in the [Self::new] call was
/// used. /// used.
/// If this was not done or the application data is set or changed after construction, /// If this was not done or the application data is set or changed after construction,
/// this function needs to be called to ensure that the data length field of the CCSDS header /// this function needs to be called to ensure that the data length field of the CCSDS header
@ -312,8 +312,7 @@ impl<'raw_data> PusTcCreator<'raw_data> {
self.len_written() as u16 - size_of::<crate::zc::SpHeader>() as u16 - 1; self.len_written() as u16 - size_of::<crate::zc::SpHeader>() as u16 - 1;
} }
/// This function should be called before the TC packet is serialized if /// This function calculates and returns the CRC16 for the current packet.
/// [PusTc::calc_crc_on_serialization] is set to False. It will calculate and cache the CRC16.
pub fn calc_own_crc16(&self) -> u16 { pub fn calc_own_crc16(&self) -> u16 {
let mut digest = CRC_CCITT_FALSE.digest(); let mut digest = CRC_CCITT_FALSE.digest();
let sph_zc = crate::zc::SpHeader::from(self.sp_header); let sph_zc = crate::zc::SpHeader::from(self.sp_header);

View File

@ -212,10 +212,11 @@ impl<'slice> TryFrom<zc::PusTmSecHeader<'slice>> for PusTmSecondaryHeader<'slice
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct PusTmCreator<'time, 'raw_data> { pub struct PusTmCreator<'time, 'raw_data> {
pub sp_header: SpHeader, pub sp_header: SpHeader,
#[cfg_attr(feature="serde", serde(borrow))]
pub sec_header: PusTmSecondaryHeader<'time>, pub sec_header: PusTmSecondaryHeader<'time>,
source_data: &'raw_data [u8], source_data: &'raw_data [u8],
/// If this is set to false, a manual call to [PusTm::calc_own_crc16] or /// If this is set to false, a manual call to [Self::calc_own_crc16] or
/// [PusTm::update_packet_fields] is necessary for the serialized or cached CRC16 to be valid. /// [Self::update_packet_fields] is necessary for the serialized or cached CRC16 to be valid.
pub calc_crc_on_serialization: bool, pub calc_crc_on_serialization: bool,
} }
@ -230,7 +231,7 @@ impl<'time, 'raw_data> PusTmCreator<'time, 'raw_data> {
/// and subservice type /// and subservice type
/// * `source_data` - Custom application data /// * `source_data` - Custom application data
/// * `set_ccsds_len` - Can be used to automatically update the CCSDS space packet data length /// * `set_ccsds_len` - Can be used to automatically update the CCSDS space packet data length
/// field. If this is not set to true, [PusTm::update_ccsds_data_len] can be called to set /// field. If this is not set to true, [Self::update_ccsds_data_len] can be called to set
/// the correct value to this field manually /// the correct value to this field manually
pub fn new( pub fn new(
sp_header: &mut SpHeader, sp_header: &mut SpHeader,
@ -302,7 +303,7 @@ impl<'time, 'raw_data> PusTmCreator<'time, 'raw_data> {
sp_header_impls!(); sp_header_impls!();
/// This is called automatically if the `set_ccsds_len` argument in the [PusTm::new] call was /// This is called automatically if the `set_ccsds_len` argument in the [Self::new] call was
/// used. /// used.
/// If this was not done or the time stamp or source data is set or changed after construction, /// If this was not done or the time stamp or source data is set or changed after construction,
/// this function needs to be called to ensure that the data length field of the CCSDS header /// this function needs to be called to ensure that the data length field of the CCSDS header
@ -313,7 +314,7 @@ impl<'time, 'raw_data> PusTmCreator<'time, 'raw_data> {
} }
/// This function should be called before the TM packet is serialized if /// This function should be called before the TM packet is serialized if
/// [PusTm.calc_crc_on_serialization] is set to False. It will calculate and cache the CRC16. /// [Self::calc_crc_on_serialization] is set to False. It will calculate and cache the CRC16.
pub fn calc_own_crc16(&self) -> u16 { pub fn calc_own_crc16(&self) -> u16 {
let mut digest = CRC_CCITT_FALSE.digest(); let mut digest = CRC_CCITT_FALSE.digest();
let sph_zc = crate::zc::SpHeader::from(self.sp_header); let sph_zc = crate::zc::SpHeader::from(self.sp_header);
@ -325,7 +326,7 @@ impl<'time, 'raw_data> PusTmCreator<'time, 'raw_data> {
digest.finalize() digest.finalize()
} }
/// This helper function calls both [PusTm.update_ccsds_data_len] and [PusTm.calc_own_crc16] /// This helper function calls both [Self::update_ccsds_data_len] and [Self::calc_own_crc16]
pub fn update_packet_fields(&mut self) { pub fn update_packet_fields(&mut self) {
self.update_ccsds_data_len(); self.update_ccsds_data_len();
} }

View File

@ -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::cds::{CdsTime, length_of_day_segment_from_pfield, LengthOfDaySegment};
/// use spacepackets::time::{TimeWriter, CcsdsTimeCode, CcsdsTimeProvider}; /// 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 mut raw_stamp = [0; 7];
/// { /// {
/// let written = timestamp_now.write_to_bytes(&mut raw_stamp).unwrap(); /// let written = timestamp_now.write_to_bytes(&mut raw_stamp).unwrap();
@ -727,14 +727,14 @@ impl<ProvidesDaysLen: ProvidesDaysLength> CdsTime<ProvidesDaysLen> {
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
fn from_now_generic(days_len: LengthOfDaySegment) -> Result<Self, StdTimestampError> { fn now_generic(days_len: LengthOfDaySegment) -> Result<Self, StdTimestampError> {
let conversion_from_now = ConversionFromNow::new()?; let conversion_from_now = ConversionFromNow::new()?;
Self::generic_from_conversion(days_len, conversion_from_now) Self::generic_from_conversion(days_len, conversion_from_now)
.map_err(|e| StdTimestampError::Timestamp(TimestampError::from(e))) .map_err(|e| StdTimestampError::Timestamp(TimestampError::from(e)))
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
fn from_now_generic_us_prec(days_len: LengthOfDaySegment) -> Result<Self, StdTimestampError> { fn now_generic_with_us_prec(days_len: LengthOfDaySegment) -> Result<Self, StdTimestampError> {
let conversion_from_now = ConversionFromNow::new_with_submillis_us_prec()?; let conversion_from_now = ConversionFromNow::new_with_submillis_us_prec()?;
Self::generic_from_conversion(days_len, conversion_from_now) Self::generic_from_conversion(days_len, conversion_from_now)
.map_err(|e| StdTimestampError::Timestamp(TimestampError::from(e))) .map_err(|e| StdTimestampError::Timestamp(TimestampError::from(e)))
@ -822,8 +822,8 @@ impl CdsTime<DaysLen24Bits> {
/// Generate a time stamp from the current time using the system clock. /// Generate a time stamp from the current time using the system clock.
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub fn from_now_with_u24_days() -> Result<Self, StdTimestampError> { pub fn now_with_u24_days() -> Result<Self, StdTimestampError> {
Self::from_now_generic(LengthOfDaySegment::Long24Bits) Self::now_generic(LengthOfDaySegment::Long24Bits)
} }
/// Create a provider from a [`chrono::DateTime<chrono::Utc>`] struct. /// Create a provider from a [`chrono::DateTime<chrono::Utc>`] struct.
@ -845,7 +845,7 @@ impl CdsTime<DaysLen24Bits> {
/// This function will return [CdsError::DateBeforeCcsdsEpoch] if the time is before the CCSDS /// 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 /// epoch (1958-01-01T00:00:00+00:00) or the CCSDS days value exceeds the allowed bit width
/// (24 bits). /// (24 bits).
pub fn from_unix_stamp_with_u24_days( pub fn from_unix_time_with_u24_day(
unix_stamp: &UnixTime, unix_stamp: &UnixTime,
submillis_prec: SubmillisPrecision, submillis_prec: SubmillisPrecision,
) -> Result<Self, CdsError> { ) -> Result<Self, CdsError> {
@ -868,16 +868,16 @@ impl CdsTime<DaysLen24Bits> {
Self::from_dt_generic_ps_prec(dt, LengthOfDaySegment::Long24Bits) Self::from_dt_generic_ps_prec(dt, LengthOfDaySegment::Long24Bits)
} }
/// Like [Self::from_now_with_u24_days] but with microsecond sub-millisecond precision. /// Like [Self::now_with_u24_days] but with microsecond sub-millisecond precision.
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub fn from_now_with_u24_days_us_precision() -> Result<Self, StdTimestampError> { pub fn now_with_u24_days_us_precision() -> Result<Self, StdTimestampError> {
Self::from_now_generic_us_prec(LengthOfDaySegment::Long24Bits) Self::now_generic_with_us_prec(LengthOfDaySegment::Long24Bits)
} }
/// Like [Self::from_now_with_u24_days] but with picoseconds sub-millisecond precision. /// Like [Self::now_with_u24_days] but with picoseconds sub-millisecond precision.
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub fn from_now_with_u24_days_ps_precision() -> Result<Self, StdTimestampError> { pub fn now_with_u24_days_ps_precision() -> Result<Self, StdTimestampError> {
Self::from_now_generic_us_prec(LengthOfDaySegment::Long24Bits) Self::now_generic_with_us_prec(LengthOfDaySegment::Long24Bits)
} }
pub fn from_bytes_with_u24_days(buf: &[u8]) -> Result<Self, TimestampError> { pub fn from_bytes_with_u24_days(buf: &[u8]) -> Result<Self, TimestampError> {
@ -926,8 +926,8 @@ impl CdsTime<DaysLen16Bits> {
/// Generate a time stamp from the current time using the system clock. /// Generate a time stamp from the current time using the system clock.
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub fn from_now_with_u16_days() -> Result<Self, StdTimestampError> { pub fn now_with_u16_days() -> Result<Self, StdTimestampError> {
Self::from_now_generic(LengthOfDaySegment::Short16Bits) Self::now_generic(LengthOfDaySegment::Short16Bits)
} }
/// Create a provider from a generic UNIX timestamp (seconds since 1970-01-01T00:00:00+00:00). /// Create a provider from a generic UNIX timestamp (seconds since 1970-01-01T00:00:00+00:00).
@ -937,7 +937,7 @@ impl CdsTime<DaysLen16Bits> {
/// This function will return [CdsError::DateBeforeCcsdsEpoch] if the time is before the CCSDS /// 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 /// epoch (1958-01-01T00:00:00+00:00) or the CCSDS days value exceeds the allowed bit width
/// (24 bits). /// (24 bits).
pub fn from_unix_stamp_with_u16_days( pub fn from_unix_time_with_u16_days(
unix_stamp: &UnixTime, unix_stamp: &UnixTime,
submillis_prec: SubmillisPrecision, submillis_prec: SubmillisPrecision,
) -> Result<Self, CdsError> { ) -> Result<Self, CdsError> {
@ -960,13 +960,13 @@ impl CdsTime<DaysLen16Bits> {
Self::from_dt_generic_ps_prec(dt, LengthOfDaySegment::Short16Bits) Self::from_dt_generic_ps_prec(dt, LengthOfDaySegment::Short16Bits)
} }
/// Like [Self::from_now_with_u16_days] but with microsecond sub-millisecond precision. /// Like [Self::now_with_u16_days] but with microsecond sub-millisecond precision.
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub fn from_now_with_u16_days_us_precision() -> Result<Self, StdTimestampError> { pub fn now_with_u16_days_us_precision() -> Result<Self, StdTimestampError> {
Self::from_now_generic_us_prec(LengthOfDaySegment::Short16Bits) Self::now_generic_with_us_prec(LengthOfDaySegment::Short16Bits)
} }
/// Like [Self::from_now_with_u16_days] but with picosecond sub-millisecond precision. /// Like [Self::now_with_u16_days] but with picosecond sub-millisecond precision.
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub fn from_now_with_u16_days_ps_precision() -> Result<Self, StdTimestampError> { pub fn from_now_with_u16_days_ps_precision() -> Result<Self, StdTimestampError> {
Self::from_now_generic_ps_prec(LengthOfDaySegment::Short16Bits) Self::from_now_generic_ps_prec(LengthOfDaySegment::Short16Bits)
@ -1608,14 +1608,14 @@ mod tests {
#[test] #[test]
fn test_time_now() { 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(); let compare_stamp = chrono::Utc::now();
generic_now_test(timestamp_now, compare_stamp); generic_now_test(timestamp_now, compare_stamp);
} }
#[test] #[test]
fn test_time_now_us_prec() { 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(); let compare_stamp = chrono::Utc::now();
generic_now_test(timestamp_now, compare_stamp); generic_now_test(timestamp_now, compare_stamp);
} }
@ -1636,7 +1636,7 @@ mod tests {
#[test] #[test]
fn test_time_now_ps_prec_u24_days() { fn test_time_now_ps_prec_u24_days() {
let timestamp_now = CdsTime::from_now_with_u24_days_ps_precision().unwrap(); let timestamp_now = CdsTime::now_with_u24_days_ps_precision().unwrap();
let compare_stamp = chrono::Utc::now(); let compare_stamp = chrono::Utc::now();
generic_now_test(timestamp_now, compare_stamp); generic_now_test(timestamp_now, compare_stamp);
} }
@ -1921,7 +1921,7 @@ mod tests {
fn test_creation_from_unix_stamp_0_u16_days() { fn test_creation_from_unix_stamp_0_u16_days() {
let unix_secs = 0; let unix_secs = 0;
let subsec_millis = 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), &UnixTime::new(unix_secs, subsec_millis),
SubmillisPrecision::Absent, SubmillisPrecision::Absent,
) )
@ -1933,7 +1933,7 @@ mod tests {
fn test_creation_from_unix_stamp_0_u24_days() { fn test_creation_from_unix_stamp_0_u24_days() {
let unix_secs = 0; let unix_secs = 0;
let subsec_millis = 0; let subsec_millis = 0;
let time_provider = CdsTime::from_unix_stamp_with_u24_days( let time_provider = CdsTime::from_unix_time_with_u24_day(
&UnixTime::new(unix_secs, subsec_millis), &UnixTime::new(unix_secs, subsec_millis),
SubmillisPrecision::Absent, SubmillisPrecision::Absent,
) )
@ -1950,11 +1950,9 @@ mod tests {
.unwrap() .unwrap()
.and_local_timezone(chrono::Utc) .and_local_timezone(chrono::Utc)
.unwrap(); .unwrap();
let time_provider = CdsTime::from_unix_stamp_with_u16_days( let time_provider =
&datetime_utc.into(), CdsTime::from_unix_time_with_u16_days(&datetime_utc.into(), SubmillisPrecision::Absent)
SubmillisPrecision::Absent, .expect("creating provider from unix stamp failed");
)
.expect("creating provider from unix stamp failed");
// https://www.timeanddate.com/date/durationresult.html?d1=01&m1=01&y1=1958&d2=14&m2=01&y2=2023 // https://www.timeanddate.com/date/durationresult.html?d1=01&m1=01&y1=1958&d2=14&m2=01&y2=2023
// Leap years need to be accounted for as well. // Leap years need to be accounted for as well.
assert_eq!(time_provider.ccsds_days, 23754); assert_eq!(time_provider.ccsds_days, 23754);
@ -1970,7 +1968,7 @@ mod tests {
fn test_creation_0_ccsds_days() { fn test_creation_0_ccsds_days() {
let unix_secs = DAYS_CCSDS_TO_UNIX as i64 * SECONDS_PER_DAY as i64; let unix_secs = DAYS_CCSDS_TO_UNIX as i64 * SECONDS_PER_DAY as i64;
let subsec_millis = 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), &UnixTime::new(unix_secs, subsec_millis),
SubmillisPrecision::Absent, SubmillisPrecision::Absent,
) )
@ -1982,7 +1980,7 @@ mod tests {
fn test_invalid_creation_from_unix_stamp_days_too_large() { 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 invalid_unix_secs: i64 = (u16::MAX as i64 + 1) * SECONDS_PER_DAY as i64;
let subsec_millis = 0; 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), &UnixTime::new(invalid_unix_secs, subsec_millis),
SubmillisPrecision::Absent, SubmillisPrecision::Absent,
) { ) {
@ -2009,7 +2007,7 @@ mod tests {
// precisely 31-12-1957 23:59:55 // precisely 31-12-1957 23:59:55
let unix_secs = DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32 - 5; let unix_secs = DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32 - 5;
let subsec_millis = 0; 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), &UnixTime::new(unix_secs as i64, subsec_millis),
SubmillisPrecision::Absent, SubmillisPrecision::Absent,
) { ) {
@ -2309,7 +2307,7 @@ mod tests {
#[test] #[test]
#[cfg(feature = "serde")] #[cfg(feature = "serde")]
fn test_serialization() { 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"); let val = to_allocvec(&stamp_now).expect("Serializing timestamp failed");
assert!(val.len() > 0); assert!(val.len() > 0);
let stamp_deser: CdsTime = from_bytes(&val).expect("Stamp deserialization failed"); let stamp_deser: CdsTime = from_bytes(&val).expect("Stamp deserialization failed");

View File

@ -249,7 +249,7 @@ impl FractionalPart {
/// const LEAP_SECONDS: u32 = 37; /// const LEAP_SECONDS: u32 = 37;
/// ///
/// // Highest fractional resolution /// // 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"); /// .expect("creating cuc stamp failed");
/// let mut raw_stamp = [0; 16]; /// 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 /// must be applied on top of the UTC based time retrieved from the system in addition to the
/// conversion to the CCSDS epoch. /// conversion to the CCSDS epoch.
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub fn from_now( pub fn now(
fraction_resolution: FractionalResolution, fraction_resolution: FractionalResolution,
leap_seconds: u32, leap_seconds: u32,
) -> Result<Self, StdTimestampError> { ) -> Result<Self, StdTimestampError> {
@ -430,15 +430,15 @@ impl CucTime {
/// Generates a CUC timestamp from a UNIX timestamp with a width of 4. This width is able /// Generates a CUC timestamp from a UNIX timestamp with a width of 4. This width is able
/// to accomodate all possible UNIX timestamp values. /// to accomodate all possible UNIX timestamp values.
pub fn from_unix_stamp( pub fn from_unix_time(
unix_stamp: &UnixTime, unix_time: &UnixTime,
res: FractionalResolution, res: FractionalResolution,
leap_seconds: u32, leap_seconds: u32,
) -> Result<Self, CucError> { ) -> Result<Self, CucError> {
let counter = unix_epoch_to_ccsds_epoch(unix_stamp.secs); let counter = unix_epoch_to_ccsds_epoch(unix_time.secs);
// Negative CCSDS epoch is invalid. // Negative CCSDS epoch is invalid.
if counter < 0 { if counter < 0 {
return Err(DateBeforeCcsdsEpochError(*unix_stamp).into()); return Err(DateBeforeCcsdsEpochError(*unix_time).into());
} }
// We already excluded negative values, so the conversion to u64 should always work. // We already excluded negative values, so the conversion to u64 should always work.
let mut counter = u32::try_from(counter).map_err(|_| CucError::InvalidCounter { let mut counter = u32::try_from(counter).map_err(|_| CucError::InvalidCounter {
@ -449,7 +449,7 @@ impl CucTime {
.checked_add(leap_seconds) .checked_add(leap_seconds)
.ok_or(CucError::LeapSecondCorrectionError)?; .ok_or(CucError::LeapSecondCorrectionError)?;
let fractions = let fractions =
fractional_part_from_subsec_ns(res, unix_stamp.subsec_millis() as u64 * 10_u64.pow(6)); fractional_part_from_subsec_ns(res, unix_time.subsec_millis() as u64 * 10_u64.pow(6));
Self::new_generic(WidthCounterPair(4, counter as u32), fractions) Self::new_generic(WidthCounterPair(4, counter as u32), fractions)
} }
@ -913,7 +913,7 @@ mod tests {
#[test] #[test]
fn test_datetime_now() { fn test_datetime_now() {
let now = chrono::Utc::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()); assert!(cuc_now.is_ok());
let cuc_now = cuc_now.unwrap(); let cuc_now = cuc_now.unwrap();
let ccsds_cuc = cuc_now.to_leap_sec_helper(LEAP_SECONDS); let ccsds_cuc = cuc_now.to_leap_sec_helper(LEAP_SECONDS);
@ -1251,6 +1251,7 @@ mod tests {
); );
assert_eq!(stamp.fractions().counter(), 0); assert_eq!(stamp.fractions().counter(), 0);
let res = stamp.update_from_now(LEAP_SECONDS); let res = stamp.update_from_now(LEAP_SECONDS);
assert!(res.is_ok()); assert!(res.is_ok());
} }
@ -1382,9 +1383,8 @@ mod tests {
#[test] #[test]
fn from_unix_stamp() { fn from_unix_stamp() {
let unix_stamp = UnixTime::new(0, 0); let unix_stamp = UnixTime::new(0, 0);
let cuc = let cuc = CucTime::from_unix_time(&unix_stamp, FractionalResolution::Seconds, LEAP_SECONDS)
CucTime::from_unix_stamp(&unix_stamp, FractionalResolution::Seconds, LEAP_SECONDS) .expect("failed to create cuc from unix stamp");
.expect("failed to create cuc from unix stamp");
assert_eq!( assert_eq!(
cuc.counter(), cuc.counter(),
(-DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32) as u32 + LEAP_SECONDS (-DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32) as u32 + LEAP_SECONDS

View File

@ -345,7 +345,7 @@ impl UnixTime {
} }
#[cfg(feature = "std")] #[cfg(feature = "std")]
pub fn from_now() -> Result<Self, SystemTimeError> { pub fn now() -> Result<Self, SystemTimeError> {
let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?; let now = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
let epoch = now.as_secs(); let epoch = now.as_secs();
Ok(Self::new(epoch as i64, now.subsec_nanos())) 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) 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 self.secs
} }
@ -367,7 +367,7 @@ impl UnixTime {
#[cfg(feature = "timelib")] #[cfg(feature = "timelib")]
pub fn timelib_date_time(&self) -> Result<time::OffsetDateTime, time::error::ComponentRange> { pub fn timelib_date_time(&self) -> Result<time::OffsetDateTime, time::error::ComponentRange> {
Ok(time::OffsetDateTime::from_unix_timestamp(self.secs())? Ok(time::OffsetDateTime::from_unix_timestamp(self.as_secs())?
+ time::Duration::nanoseconds(self.subsec_nanos().into())) + time::Duration::nanoseconds(self.subsec_nanos().into()))
} }
@ -649,7 +649,7 @@ mod tests {
fn test_addition() { fn test_addition() {
let mut stamp0 = UnixTime::new_only_secs(1); let mut stamp0 = UnixTime::new_only_secs(1);
stamp0 += Duration::from_secs(5); stamp0 += Duration::from_secs(5);
assert_eq!(stamp0.secs(), 6); assert_eq!(stamp0.as_secs(), 6);
assert_eq!(stamp0.subsec_millis(), 0); assert_eq!(stamp0.subsec_millis(), 0);
let stamp1 = stamp0 + Duration::from_millis(500); let stamp1 = stamp0 + Duration::from_millis(500);
assert_eq!(stamp1.secs, 6); assert_eq!(stamp1.secs, 6);
@ -678,7 +678,7 @@ mod tests {
#[test] #[test]
fn test_from_now() { 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(); let dt_now = stamp_now.chrono_date_time().unwrap();
assert!(dt_now.year() >= 2020); assert!(dt_now.year() >= 2020);
} }