Compare commits
6 Commits
v0.11.0-rc
...
14b85992e8
Author | SHA1 | Date | |
---|---|---|---|
14b85992e8
|
|||
a8f1da4117
|
|||
55f99bbdfa
|
|||
3b2102b4d8
|
|||
359e5124bd
|
|||
8c23a9c924
|
12
CHANGELOG.md
12
CHANGELOG.md
@@ -8,18 +8,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
|
|||||||
|
|
||||||
# [unreleased]
|
# [unreleased]
|
||||||
|
|
||||||
# [v0.11.0-rc.0] 2024-03-04
|
|
||||||
|
|
||||||
## Added
|
|
||||||
|
|
||||||
- `From<$TY>` for the `EcssEnum$TY` ECSS enum type definitions.
|
|
||||||
- `Sub` implementation for `UnixTimestamp` to calculate the duration between two timestamps.
|
|
||||||
|
|
||||||
## Changed
|
|
||||||
|
|
||||||
- `CcsdsTimeProvider` `subsecond_millis` function now returns `u16` instead of `Option<u16>`.
|
|
||||||
- `UnixTimestamp` `subsecond_millis` function now returns `u16` instead of `Option<u16>`.
|
|
||||||
|
|
||||||
# [v0.10.0] 2024-02-17
|
# [v0.10.0] 2024-02-17
|
||||||
|
|
||||||
## Added
|
## Added
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "spacepackets"
|
name = "spacepackets"
|
||||||
version = "0.11.0-rc.0"
|
version = "0.10.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
rust-version = "1.61"
|
rust-version = "1.61"
|
||||||
authors = ["Robin Mueller <muellerr@irs.uni-stuttgart.de>"]
|
authors = ["Robin Mueller <muellerr@irs.uni-stuttgart.de>"]
|
||||||
|
@@ -360,28 +360,10 @@ impl<TYPE: Debug + Copy + Clone + PartialEq + Eq + ToBeBytes + Into<u64>> EcssEn
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! generic_ecss_enum_typedefs_and_from_impls {
|
pub type EcssEnumU8 = GenericEcssEnumWrapper<u8>;
|
||||||
($($ty:ty => $Enum:ident),*) => {
|
pub type EcssEnumU16 = GenericEcssEnumWrapper<u16>;
|
||||||
$(
|
pub type EcssEnumU32 = GenericEcssEnumWrapper<u32>;
|
||||||
pub type $Enum = GenericEcssEnumWrapper<$ty>;
|
pub type EcssEnumU64 = GenericEcssEnumWrapper<u64>;
|
||||||
|
|
||||||
impl From<$ty> for $Enum {
|
|
||||||
fn from(value: $ty) -> Self {
|
|
||||||
Self::new(value)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)*
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generates EcssEnum<$TY> type definitions as well as a From<$TY> for EcssEnum<$TY>
|
|
||||||
// implementation.
|
|
||||||
generic_ecss_enum_typedefs_and_from_impls! {
|
|
||||||
u8 => EcssEnumU8,
|
|
||||||
u16 => EcssEnumU16,
|
|
||||||
u32 => EcssEnumU32,
|
|
||||||
u64 => EcssEnumU64
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generic trait for PUS packet abstractions which can written to a raw slice as their raw
|
/// Generic trait for PUS packet abstractions which can written to a raw slice as their raw
|
||||||
/// byte representation. This is especially useful for generic abstractions which depend only
|
/// byte representation. This is especially useful for generic abstractions which depend only
|
||||||
@@ -424,8 +406,6 @@ mod tests {
|
|||||||
assert_eq!(buf[1], 1);
|
assert_eq!(buf[1], 1);
|
||||||
assert_eq!(my_enum.value(), 1);
|
assert_eq!(my_enum.value(), 1);
|
||||||
assert_eq!(my_enum.value_typed(), 1);
|
assert_eq!(my_enum.value_typed(), 1);
|
||||||
let vec = my_enum.to_vec();
|
|
||||||
assert_eq!(vec, buf[1..2]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -441,8 +421,6 @@ mod tests {
|
|||||||
assert_eq!(buf[2], 0x2f);
|
assert_eq!(buf[2], 0x2f);
|
||||||
assert_eq!(my_enum.value(), 0x1f2f);
|
assert_eq!(my_enum.value(), 0x1f2f);
|
||||||
assert_eq!(my_enum.value_typed(), 0x1f2f);
|
assert_eq!(my_enum.value_typed(), 0x1f2f);
|
||||||
let vec = my_enum.to_vec();
|
|
||||||
assert_eq!(vec, buf[1..3]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -476,8 +454,6 @@ mod tests {
|
|||||||
assert_eq!(buf[4], 0x4f);
|
assert_eq!(buf[4], 0x4f);
|
||||||
assert_eq!(my_enum.value(), 0x1f2f3f4f);
|
assert_eq!(my_enum.value(), 0x1f2f3f4f);
|
||||||
assert_eq!(my_enum.value_typed(), 0x1f2f3f4f);
|
assert_eq!(my_enum.value_typed(), 0x1f2f3f4f);
|
||||||
let vec = my_enum.to_vec();
|
|
||||||
assert_eq!(vec, buf[1..5]);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -513,8 +489,6 @@ mod tests {
|
|||||||
assert_eq!(my_enum.value(), 0x1f2f3f4f5f);
|
assert_eq!(my_enum.value(), 0x1f2f3f4f5f);
|
||||||
assert_eq!(my_enum.value_typed(), 0x1f2f3f4f5f);
|
assert_eq!(my_enum.value_typed(), 0x1f2f3f4f5f);
|
||||||
assert_eq!(u64::from_be_bytes(buf), 0x1f2f3f4f5f);
|
assert_eq!(u64::from_be_bytes(buf), 0x1f2f3f4f5f);
|
||||||
let vec = my_enum.to_vec();
|
|
||||||
assert_eq!(vec, buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@@ -672,8 +672,10 @@ impl<ProvidesDaysLen: ProvidesDaysLength> TimeProvider<ProvidesDaysLen> {
|
|||||||
unix_stamp: &UnixTimestamp,
|
unix_stamp: &UnixTimestamp,
|
||||||
days_len: LengthOfDaySegment,
|
days_len: LengthOfDaySegment,
|
||||||
) -> Result<Self, TimestampError> {
|
) -> Result<Self, TimestampError> {
|
||||||
let conv_from_dt =
|
let conv_from_dt = ConversionFromUnix::new(
|
||||||
ConversionFromUnix::new(unix_stamp.unix_seconds, unix_stamp.subsecond_millis as u32)?;
|
unix_stamp.unix_seconds,
|
||||||
|
unix_stamp.subsecond_millis.unwrap_or(0) as u32,
|
||||||
|
)?;
|
||||||
Self::generic_from_conversion(days_len, conv_from_dt)
|
Self::generic_from_conversion(days_len, conv_from_dt)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1146,7 +1148,7 @@ impl<ProvidesDaysLen: ProvidesDaysLength> CcsdsTimeProvider for TimeProvider<Pro
|
|||||||
self.unix_stamp.unix_seconds
|
self.unix_stamp.unix_seconds
|
||||||
}
|
}
|
||||||
#[inline]
|
#[inline]
|
||||||
fn subsecond_millis(&self) -> u16 {
|
fn subsecond_millis(&self) -> Option<u16> {
|
||||||
self.unix_stamp.subsecond_millis
|
self.unix_stamp.subsecond_millis
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1318,12 +1320,12 @@ mod tests {
|
|||||||
(DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32) as i64
|
(DAYS_CCSDS_TO_UNIX * SECONDS_PER_DAY as i32) as i64
|
||||||
);
|
);
|
||||||
let subsecond_millis = unix_stamp.subsecond_millis;
|
let subsecond_millis = unix_stamp.subsecond_millis;
|
||||||
assert_eq!(subsecond_millis, 0);
|
assert!(subsecond_millis.is_none());
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
time_stamper.submillis_precision(),
|
time_stamper.submillis_precision(),
|
||||||
SubmillisPrecision::Absent
|
SubmillisPrecision::Absent
|
||||||
);
|
);
|
||||||
assert_eq!(time_stamper.subsecond_millis(), 0);
|
assert!(time_stamper.subsecond_millis().is_none());
|
||||||
assert_eq!(time_stamper.ccdsd_time_code(), CcsdsTimeCodes::Cds);
|
assert_eq!(time_stamper.ccdsd_time_code(), CcsdsTimeCodes::Cds);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
time_stamper.p_field(),
|
time_stamper.p_field(),
|
||||||
@@ -1354,9 +1356,11 @@ mod tests {
|
|||||||
assert_eq!(date_time.minute(), 0);
|
assert_eq!(date_time.minute(), 0);
|
||||||
assert_eq!(date_time.second(), 0);
|
assert_eq!(date_time.second(), 0);
|
||||||
let time_stamper = TimeProvider::new_with_u16_days((-DAYS_CCSDS_TO_UNIX) as u16, 40);
|
let time_stamper = TimeProvider::new_with_u16_days((-DAYS_CCSDS_TO_UNIX) as u16, 40);
|
||||||
assert_eq!(time_stamper.subsecond_millis(), 40);
|
assert!(time_stamper.subsecond_millis().is_some());
|
||||||
|
assert_eq!(time_stamper.subsecond_millis().unwrap(), 40);
|
||||||
let time_stamper = TimeProvider::new_with_u16_days((-DAYS_CCSDS_TO_UNIX) as u16, 1040);
|
let time_stamper = TimeProvider::new_with_u16_days((-DAYS_CCSDS_TO_UNIX) as u16, 1040);
|
||||||
assert_eq!(time_stamper.subsecond_millis(), 40);
|
assert!(time_stamper.subsecond_millis().is_some());
|
||||||
|
assert_eq!(time_stamper.subsecond_millis().unwrap(), 40);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@@ -297,10 +297,10 @@ impl TimeProviderCcsdsEpoch {
|
|||||||
unix_stamp.as_date_time().unwrap(),
|
unix_stamp.as_date_time().unwrap(),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
let fractions = fractional_part_from_subsec_ns(
|
let mut fractions = None;
|
||||||
res,
|
if let Some(subsec_millis) = unix_stamp.subsecond_millis {
|
||||||
unix_stamp.subsecond_millis() as u64 * 10_u64.pow(6),
|
fractions = fractional_part_from_subsec_ns(res, subsec_millis as u64 * 10_u64.pow(6));
|
||||||
);
|
}
|
||||||
Self::new_generic(WidthCounterPair(4, ccsds_epoch as u32), fractions).map_err(|e| e.into())
|
Self::new_generic(WidthCounterPair(4, ccsds_epoch as u32), fractions).map_err(|e| e.into())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -610,15 +610,15 @@ impl CcsdsTimeProvider for TimeProviderCcsdsEpoch {
|
|||||||
self.unix_seconds()
|
self.unix_seconds()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subsecond_millis(&self) -> u16 {
|
fn subsecond_millis(&self) -> Option<u16> {
|
||||||
if let Some(fractions) = self.fractions {
|
if let Some(fractions) = self.fractions {
|
||||||
if fractions.0 == FractionalResolution::Seconds {
|
if fractions.0 == FractionalResolution::Seconds {
|
||||||
return 0;
|
return None;
|
||||||
}
|
}
|
||||||
// Rounding down here is the correct approach.
|
// Rounding down here is the correct approach.
|
||||||
return (convert_fractional_part_to_ns(fractions) / 10_u32.pow(6) as u64) as u16;
|
return Some((convert_fractional_part_to_ns(fractions) / 10_u32.pow(6) as u64) as u16);
|
||||||
}
|
}
|
||||||
0
|
None
|
||||||
}
|
}
|
||||||
|
|
||||||
fn date_time(&self) -> Option<DateTime<Utc>> {
|
fn date_time(&self) -> Option<DateTime<Utc>> {
|
||||||
@@ -763,7 +763,7 @@ mod tests {
|
|||||||
let zero_cuc = zero_cuc.unwrap();
|
let zero_cuc = zero_cuc.unwrap();
|
||||||
let res = zero_cuc.write_to_bytes(&mut buf);
|
let res = zero_cuc.write_to_bytes(&mut buf);
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
assert_eq!(zero_cuc.subsecond_millis(), 0);
|
assert!(zero_cuc.subsecond_millis().is_none());
|
||||||
assert_eq!(zero_cuc.len_as_bytes(), 5);
|
assert_eq!(zero_cuc.len_as_bytes(), 5);
|
||||||
assert_eq!(pfield_len(buf[0]), 1);
|
assert_eq!(pfield_len(buf[0]), 1);
|
||||||
let written = res.unwrap();
|
let written = res.unwrap();
|
||||||
@@ -1130,7 +1130,7 @@ mod tests {
|
|||||||
// What I would roughly expect
|
// What I would roughly expect
|
||||||
assert_eq!(cuc_stamp2.counter.1, 203);
|
assert_eq!(cuc_stamp2.counter.1, 203);
|
||||||
assert!(cuc_stamp2.fractions.unwrap().1 < 100);
|
assert!(cuc_stamp2.fractions.unwrap().1 < 100);
|
||||||
assert!(cuc_stamp2.subsecond_millis() <= 1);
|
assert!(cuc_stamp2.subsecond_millis().unwrap() <= 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
142
src/time/mod.rs
142
src/time/mod.rs
@@ -3,7 +3,7 @@ use crate::ByteConversionError;
|
|||||||
use chrono::{DateTime, LocalResult, TimeZone, Utc};
|
use chrono::{DateTime, LocalResult, TimeZone, Utc};
|
||||||
use core::cmp::Ordering;
|
use core::cmp::Ordering;
|
||||||
use core::fmt::{Display, Formatter};
|
use core::fmt::{Display, Formatter};
|
||||||
use core::ops::{Add, AddAssign, Sub};
|
use core::ops::{Add, AddAssign};
|
||||||
use core::time::Duration;
|
use core::time::Duration;
|
||||||
use core::u8;
|
use core::u8;
|
||||||
|
|
||||||
@@ -196,7 +196,6 @@ pub trait TimeWriter {
|
|||||||
fn write_to_bytes(&self, bytes: &mut [u8]) -> Result<usize, TimestampError>;
|
fn write_to_bytes(&self, bytes: &mut [u8]) -> Result<usize, TimestampError>;
|
||||||
|
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
|
|
||||||
fn to_vec(&self) -> Result<alloc::vec::Vec<u8>, TimestampError> {
|
fn to_vec(&self) -> Result<alloc::vec::Vec<u8>, TimestampError> {
|
||||||
let mut vec = alloc::vec![0; self.len_written()];
|
let mut vec = alloc::vec![0; self.len_written()];
|
||||||
self.write_to_bytes(&mut vec)?;
|
self.write_to_bytes(&mut vec)?;
|
||||||
@@ -225,9 +224,12 @@ pub trait CcsdsTimeProvider {
|
|||||||
fn ccdsd_time_code(&self) -> CcsdsTimeCodes;
|
fn ccdsd_time_code(&self) -> CcsdsTimeCodes;
|
||||||
|
|
||||||
fn unix_seconds(&self) -> i64;
|
fn unix_seconds(&self) -> i64;
|
||||||
fn subsecond_millis(&self) -> u16;
|
fn subsecond_millis(&self) -> Option<u16>;
|
||||||
fn unix_stamp(&self) -> UnixTimestamp {
|
fn unix_stamp(&self) -> UnixTimestamp {
|
||||||
UnixTimestamp::const_new(self.unix_seconds(), self.subsecond_millis())
|
if self.subsecond_millis().is_none() {
|
||||||
|
return UnixTimestamp::new_only_seconds(self.unix_seconds());
|
||||||
|
}
|
||||||
|
UnixTimestamp::const_new(self.unix_seconds(), self.subsecond_millis().unwrap())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn date_time(&self) -> Option<DateTime<Utc>>;
|
fn date_time(&self) -> Option<DateTime<Utc>>;
|
||||||
@@ -235,16 +237,18 @@ pub trait CcsdsTimeProvider {
|
|||||||
|
|
||||||
/// UNIX timestamp: Elapsed seconds since 1970-01-01T00:00:00+00:00.
|
/// UNIX timestamp: Elapsed seconds since 1970-01-01T00:00:00+00:00.
|
||||||
///
|
///
|
||||||
/// Also can optionally include subsecond millisecond for greater accuracy.
|
/// Also can optionally include subsecond millisecond for greater accuracy. Please note that a
|
||||||
|
/// subsecond millisecond value of 0 gets converted to [None].
|
||||||
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
|
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
|
||||||
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
|
||||||
pub struct UnixTimestamp {
|
pub struct UnixTimestamp {
|
||||||
pub unix_seconds: i64,
|
pub unix_seconds: i64,
|
||||||
subsecond_millis: u16,
|
subsecond_millis: Option<u16>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl UnixTimestamp {
|
impl UnixTimestamp {
|
||||||
/// Returns [None] if the subsecond millisecond value is larger than 999.
|
/// Returns none if the subsecond millisecond value is larger than 999. 0 is converted to
|
||||||
|
/// a [None] value.
|
||||||
pub fn new(unix_seconds: i64, subsec_millis: u16) -> Option<Self> {
|
pub fn new(unix_seconds: i64, subsec_millis: u16) -> Option<Self> {
|
||||||
if subsec_millis > 999 {
|
if subsec_millis > 999 {
|
||||||
return None;
|
return None;
|
||||||
@@ -253,10 +257,15 @@ impl UnixTimestamp {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Like [Self::new] but const. Panics if the subsecond value is larger than 999.
|
/// Like [Self::new] but const. Panics if the subsecond value is larger than 999.
|
||||||
pub const fn const_new(unix_seconds: i64, subsecond_millis: u16) -> Self {
|
pub const fn const_new(unix_seconds: i64, subsec_millis: u16) -> Self {
|
||||||
if subsecond_millis > 999 {
|
if subsec_millis > 999 {
|
||||||
panic!("subsec milliseconds exceeds 999");
|
panic!("subsec milliseconds exceeds 999");
|
||||||
}
|
}
|
||||||
|
let subsecond_millis = if subsec_millis == 0 {
|
||||||
|
None
|
||||||
|
} else {
|
||||||
|
Some(subsec_millis)
|
||||||
|
};
|
||||||
Self {
|
Self {
|
||||||
unix_seconds,
|
unix_seconds,
|
||||||
subsecond_millis,
|
subsecond_millis,
|
||||||
@@ -266,11 +275,11 @@ impl UnixTimestamp {
|
|||||||
pub fn new_only_seconds(unix_seconds: i64) -> Self {
|
pub fn new_only_seconds(unix_seconds: i64) -> Self {
|
||||||
Self {
|
Self {
|
||||||
unix_seconds,
|
unix_seconds,
|
||||||
subsecond_millis: 0,
|
subsecond_millis: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn subsecond_millis(&self) -> u16 {
|
pub fn subsecond_millis(&self) -> Option<u16> {
|
||||||
self.subsecond_millis
|
self.subsecond_millis
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -284,28 +293,19 @@ impl UnixTimestamp {
|
|||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn unix_seconds_f64(&self) -> f64 {
|
pub fn unix_seconds_f64(&self) -> f64 {
|
||||||
self.unix_seconds as f64 + (self.subsecond_millis as f64 / 1000.0)
|
let mut secs = self.unix_seconds as f64;
|
||||||
|
if let Some(subsec_millis) = self.subsecond_millis {
|
||||||
|
secs += subsec_millis as f64 / 1000.0;
|
||||||
|
}
|
||||||
|
secs
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_date_time(&self) -> LocalResult<DateTime<Utc>> {
|
pub fn as_date_time(&self) -> LocalResult<DateTime<Utc>> {
|
||||||
Utc.timestamp_opt(
|
Utc.timestamp_opt(
|
||||||
self.unix_seconds,
|
self.unix_seconds,
|
||||||
self.subsecond_millis as u32 * 10_u32.pow(6),
|
self.subsecond_millis.unwrap_or(0) as u32 * 10_u32.pow(6),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate the difference in milliseconds between two UnixTimestamps
|
|
||||||
pub fn difference_in_millis(&self, other: &UnixTimestamp) -> i64 {
|
|
||||||
let seconds_difference = self.unix_seconds - other.unix_seconds;
|
|
||||||
// Convert seconds difference to milliseconds
|
|
||||||
let milliseconds_difference = seconds_difference * 1000;
|
|
||||||
|
|
||||||
// Calculate the difference in subsecond milliseconds directly
|
|
||||||
let subsecond_difference = self.subsecond_millis as i64 - other.subsecond_millis as i64;
|
|
||||||
|
|
||||||
// Combine the differences
|
|
||||||
milliseconds_difference + subsecond_difference
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<DateTime<Utc>> for UnixTimestamp {
|
impl From<DateTime<Utc>> for UnixTimestamp {
|
||||||
@@ -331,7 +331,11 @@ impl Ord for UnixTimestamp {
|
|||||||
_ => (),
|
_ => (),
|
||||||
}
|
}
|
||||||
|
|
||||||
match self.subsecond_millis().cmp(&other.subsecond_millis()) {
|
match self
|
||||||
|
.subsecond_millis()
|
||||||
|
.unwrap_or(0)
|
||||||
|
.cmp(&other.subsecond_millis().unwrap_or(0))
|
||||||
|
{
|
||||||
Ordering::Less => {
|
Ordering::Less => {
|
||||||
return if self.unix_seconds < 0 {
|
return if self.unix_seconds < 0 {
|
||||||
Ordering::Greater
|
Ordering::Greater
|
||||||
@@ -352,38 +356,12 @@ impl Ord for UnixTimestamp {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Difference between two UNIX timestamps. The [Duration] type can not contain negative durations,
|
|
||||||
/// so the sign information is supplied separately.
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq)]
|
|
||||||
pub struct StampDiff {
|
|
||||||
pub positive_duration: bool,
|
|
||||||
pub duration_absolute: Duration,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Sub for UnixTimestamp {
|
|
||||||
type Output = StampDiff;
|
|
||||||
|
|
||||||
fn sub(self, rhs: Self) -> Self::Output {
|
|
||||||
let difference = self.difference_in_millis(&rhs);
|
|
||||||
if difference < 0 {
|
|
||||||
StampDiff {
|
|
||||||
positive_duration: false,
|
|
||||||
duration_absolute: Duration::from_millis(-difference as u64),
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
StampDiff {
|
|
||||||
positive_duration: true,
|
|
||||||
duration_absolute: Duration::from_millis(difference as u64),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_new_stamp_after_addition(
|
fn get_new_stamp_after_addition(
|
||||||
current_stamp: &UnixTimestamp,
|
current_stamp: &UnixTimestamp,
|
||||||
duration: Duration,
|
duration: Duration,
|
||||||
) -> UnixTimestamp {
|
) -> UnixTimestamp {
|
||||||
let mut new_subsec_millis = current_stamp.subsecond_millis() + duration.subsec_millis() as u16;
|
let mut new_subsec_millis =
|
||||||
|
current_stamp.subsecond_millis().unwrap_or(0) + duration.subsec_millis() as u16;
|
||||||
let mut new_unix_seconds = current_stamp.unix_seconds;
|
let mut new_unix_seconds = current_stamp.unix_seconds;
|
||||||
let mut increment_seconds = |value: u32| {
|
let mut increment_seconds = |value: u32| {
|
||||||
if new_unix_seconds < 0 {
|
if new_unix_seconds < 0 {
|
||||||
@@ -486,17 +464,18 @@ mod tests {
|
|||||||
fn basic_unix_stamp_test() {
|
fn basic_unix_stamp_test() {
|
||||||
let stamp = UnixTimestamp::new_only_seconds(-200);
|
let stamp = UnixTimestamp::new_only_seconds(-200);
|
||||||
assert_eq!(stamp.unix_seconds, -200);
|
assert_eq!(stamp.unix_seconds, -200);
|
||||||
assert_eq!(stamp.subsecond_millis(), 0);
|
assert!(stamp.subsecond_millis().is_none());
|
||||||
let stamp = UnixTimestamp::new_only_seconds(250);
|
let stamp = UnixTimestamp::new_only_seconds(250);
|
||||||
assert_eq!(stamp.unix_seconds, 250);
|
assert_eq!(stamp.unix_seconds, 250);
|
||||||
assert_eq!(stamp.subsecond_millis(), 0);
|
assert!(stamp.subsecond_millis().is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn basic_float_unix_stamp_test() {
|
fn basic_float_unix_stamp_test() {
|
||||||
let stamp = UnixTimestamp::new(500, 600).unwrap();
|
let stamp = UnixTimestamp::new(500, 600).unwrap();
|
||||||
|
assert!(stamp.subsecond_millis.is_some());
|
||||||
assert_eq!(stamp.unix_seconds, 500);
|
assert_eq!(stamp.unix_seconds, 500);
|
||||||
let subsec_millis = stamp.subsecond_millis();
|
let subsec_millis = stamp.subsecond_millis().unwrap();
|
||||||
assert_eq!(subsec_millis, 600);
|
assert_eq!(subsec_millis, 600);
|
||||||
assert!((500.6 - stamp.unix_seconds_f64()).abs() < 0.0001);
|
assert!((500.6 - stamp.unix_seconds_f64()).abs() < 0.0001);
|
||||||
}
|
}
|
||||||
@@ -545,7 +524,6 @@ mod tests {
|
|||||||
assert!(stamp2 <= stamp1);
|
assert!(stamp2 <= stamp1);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::nonminimal_bool)]
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_eq() {
|
fn test_eq() {
|
||||||
let stamp0 = UnixTimestamp::new(5, 0).unwrap();
|
let stamp0 = UnixTimestamp::new(5, 0).unwrap();
|
||||||
@@ -562,10 +540,11 @@ mod tests {
|
|||||||
let mut stamp0 = UnixTimestamp::new_only_seconds(1);
|
let mut stamp0 = UnixTimestamp::new_only_seconds(1);
|
||||||
stamp0 += Duration::from_secs(5);
|
stamp0 += Duration::from_secs(5);
|
||||||
assert_eq!(stamp0.unix_seconds, 6);
|
assert_eq!(stamp0.unix_seconds, 6);
|
||||||
assert_eq!(stamp0.subsecond_millis(), 0);
|
assert!(stamp0.subsecond_millis().is_none());
|
||||||
let stamp1 = stamp0 + Duration::from_millis(500);
|
let stamp1 = stamp0 + Duration::from_millis(500);
|
||||||
assert_eq!(stamp1.unix_seconds, 6);
|
assert_eq!(stamp1.unix_seconds, 6);
|
||||||
assert_eq!(stamp1.subsecond_millis(), 500);
|
assert!(stamp1.subsecond_millis().is_some());
|
||||||
|
assert_eq!(stamp1.subsecond_millis().unwrap(), 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -573,7 +552,7 @@ mod tests {
|
|||||||
let stamp0 = &UnixTimestamp::new(20, 500).unwrap();
|
let stamp0 = &UnixTimestamp::new(20, 500).unwrap();
|
||||||
let stamp1 = stamp0 + Duration::from_millis(2500);
|
let stamp1 = stamp0 + Duration::from_millis(2500);
|
||||||
assert_eq!(stamp1.unix_seconds, 23);
|
assert_eq!(stamp1.unix_seconds, 23);
|
||||||
assert_eq!(stamp1.subsecond_millis(), 0);
|
assert!(stamp1.subsecond_millis().is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -595,50 +574,15 @@ mod tests {
|
|||||||
assert!(dt_now.year() >= 2020);
|
assert!(dt_now.year() >= 2020);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_stamp_diff_positive_0() {
|
|
||||||
let stamp_later = UnixTimestamp::new(2, 0).unwrap();
|
|
||||||
let StampDiff {
|
|
||||||
positive_duration,
|
|
||||||
duration_absolute,
|
|
||||||
} = stamp_later - UnixTimestamp::new(1, 0).unwrap();
|
|
||||||
assert!(positive_duration);
|
|
||||||
assert_eq!(duration_absolute, Duration::from_secs(1));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_stamp_diff_positive_1() {
|
|
||||||
let stamp_later = UnixTimestamp::new(3, 800).unwrap();
|
|
||||||
let stamp_earlier = UnixTimestamp::new(1, 900).unwrap();
|
|
||||||
let StampDiff {
|
|
||||||
positive_duration,
|
|
||||||
duration_absolute,
|
|
||||||
} = stamp_later - stamp_earlier;
|
|
||||||
assert!(positive_duration);
|
|
||||||
assert_eq!(duration_absolute, Duration::from_millis(1900));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn test_stamp_diff_negative() {
|
|
||||||
let stamp_later = UnixTimestamp::new(3, 800).unwrap();
|
|
||||||
let stamp_earlier = UnixTimestamp::new(1, 900).unwrap();
|
|
||||||
let StampDiff {
|
|
||||||
positive_duration,
|
|
||||||
duration_absolute,
|
|
||||||
} = stamp_earlier - stamp_later;
|
|
||||||
assert!(!positive_duration);
|
|
||||||
assert_eq!(duration_absolute, Duration::from_millis(1900));
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_addition_spillover() {
|
fn test_addition_spillover() {
|
||||||
let mut stamp0 = UnixTimestamp::new(1, 900).unwrap();
|
let mut stamp0 = UnixTimestamp::new(1, 900).unwrap();
|
||||||
stamp0 += Duration::from_millis(100);
|
stamp0 += Duration::from_millis(100);
|
||||||
assert_eq!(stamp0.unix_seconds, 2);
|
assert_eq!(stamp0.unix_seconds, 2);
|
||||||
assert_eq!(stamp0.subsecond_millis(), 0);
|
assert!(stamp0.subsecond_millis().is_none());
|
||||||
stamp0 += Duration::from_millis(1100);
|
stamp0 += Duration::from_millis(1100);
|
||||||
assert_eq!(stamp0.unix_seconds, 3);
|
assert_eq!(stamp0.unix_seconds, 3);
|
||||||
assert_eq!(stamp0.subsecond_millis(), 100);
|
assert_eq!(stamp0.subsecond_millis().unwrap(), 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
@@ -77,7 +77,6 @@ pub trait UnsignedEnum {
|
|||||||
fn value(&self) -> u64;
|
fn value(&self) -> u64;
|
||||||
|
|
||||||
#[cfg(feature = "alloc")]
|
#[cfg(feature = "alloc")]
|
||||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "alloc")))]
|
|
||||||
fn to_vec(&self) -> alloc::vec::Vec<u8> {
|
fn to_vec(&self) -> alloc::vec::Vec<u8> {
|
||||||
let mut buf = alloc::vec![0; self.size()];
|
let mut buf = alloc::vec![0; self.size()];
|
||||||
self.write_to_be_bytes(&mut buf).unwrap();
|
self.write_to_be_bytes(&mut buf).unwrap();
|
||||||
|
Reference in New Issue
Block a user