now to test it..
Some checks failed
Rust/spacepackets/pipeline/head There was a failure building this commit
Some checks failed
Rust/spacepackets/pipeline/head There was a failure building this commit
This commit is contained in:
parent
3ba68b4e64
commit
8da93443b9
371
src/time/cds.rs
371
src/time/cds.rs
@ -8,6 +8,7 @@ use chrono::Datelike;
|
|||||||
use core::fmt::Debug;
|
use core::fmt::Debug;
|
||||||
use core::ops::Add;
|
use core::ops::Add;
|
||||||
use core::time::Duration;
|
use core::time::Duration;
|
||||||
|
use delegate::delegate;
|
||||||
|
|
||||||
/// Base value for the preamble field for a time field parser to determine the time field type.
|
/// Base value for the preamble field for a time field parser to determine the time field type.
|
||||||
pub const P_FIELD_BASE: u8 = (CcsdsTimeCodes::Cds as u8) << 4;
|
pub const P_FIELD_BASE: u8 = (CcsdsTimeCodes::Cds as u8) << 4;
|
||||||
@ -17,7 +18,7 @@ pub const MAX_DAYS_24_BITS: u32 = 2_u32.pow(24) - 1;
|
|||||||
/// Generic trait implemented by token structs to specify the length of day field at type
|
/// Generic trait implemented by token structs to specify the length of day field at type
|
||||||
/// level. This trait is only meant to be implemented in this crate and therefore sealed.
|
/// level. This trait is only meant to be implemented in this crate and therefore sealed.
|
||||||
pub trait ProvidesDaysLength: Sealed {
|
pub trait ProvidesDaysLength: Sealed {
|
||||||
type FieldType: Copy + Clone + TryFrom<i32> + Into<u32> + Into<i64>;
|
type FieldType: Copy + Clone + TryFrom<i32> + TryFrom<u32> + From<u16> + Into<u32> + Into<i64>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Type level token to be used as a generic parameter to [TimeProvider].
|
/// Type level token to be used as a generic parameter to [TimeProvider].
|
||||||
@ -88,6 +89,7 @@ pub fn length_of_day_segment_from_pfield(pfield: u8) -> LengthOfDaySegment {
|
|||||||
}
|
}
|
||||||
LengthOfDaySegment::Short16Bits
|
LengthOfDaySegment::Short16Bits
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn precision_from_pfield(pfield: u8) -> SubmillisPrecision {
|
pub fn precision_from_pfield(pfield: u8) -> SubmillisPrecision {
|
||||||
match pfield & 0b11 {
|
match pfield & 0b11 {
|
||||||
0b01 => SubmillisPrecision::Microseconds(0),
|
0b01 => SubmillisPrecision::Microseconds(0),
|
||||||
@ -141,51 +143,134 @@ pub struct TimeProvider<DaysLen: ProvidesDaysLength = DaysLen16Bits> {
|
|||||||
unix_seconds: i64,
|
unix_seconds: i64,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper struct which generates fields for the CDS time provider from a datetime.
|
/// Private trait which serves as an abstraction for different converters.
|
||||||
struct ConversionFromDatetime {
|
trait CdsConverter {
|
||||||
|
fn submillis_precision(&self) -> Option<SubmillisPrecision>;
|
||||||
|
fn ms_of_day(&self) -> u32;
|
||||||
|
fn ccsds_days(&self) -> u32;
|
||||||
|
fn unix_days_seconds(&self) -> i64;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct ConversionFromUnix {
|
||||||
ccsds_days: u32,
|
ccsds_days: u32,
|
||||||
ms_of_day: u32,
|
ms_of_day: u32,
|
||||||
/// This is a side-product of the calculation of the CCSDS days. It is useful for
|
/// This is a side-product of the calculation of the CCSDS days. It is useful for
|
||||||
/// re-calculating the datetime at a later point and therefore supplied as well.
|
/// re-calculating the datetime at a later point and therefore supplied as well.
|
||||||
unix_days_seconds: i64,
|
unix_days_seconds: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ConversionFromUnix {
|
||||||
|
fn new(unix_seconds: i64, subsec_millis: u32) -> Self {
|
||||||
|
let (unix_days, secs_of_day) = calc_unix_days_and_secs_of_day(unix_seconds);
|
||||||
|
Self {
|
||||||
|
ccsds_days: unix_to_ccsds_days(unix_days) as u32,
|
||||||
|
ms_of_day: secs_of_day * 1000 + subsec_millis,
|
||||||
|
unix_days_seconds: unix_days * SECONDS_PER_DAY as i64,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl CdsConverter for ConversionFromUnix {
|
||||||
|
fn submillis_precision(&self) -> Option<SubmillisPrecision> {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ms_of_day(&self) -> u32 {
|
||||||
|
self.ms_of_day
|
||||||
|
}
|
||||||
|
|
||||||
|
fn ccsds_days(&self) -> u32 {
|
||||||
|
self.ccsds_days
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unix_days_seconds(&self) -> i64 {
|
||||||
|
self.unix_days_seconds
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Helper struct which generates fields for the CDS time provider from a datetime.
|
||||||
|
struct ConversionFromDatetime {
|
||||||
|
unix_conversion: ConversionFromUnix,
|
||||||
submillis_prec: Option<SubmillisPrecision>,
|
submillis_prec: Option<SubmillisPrecision>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn calc_unix_days_and_secs_of_day(unix_seconds: i64) -> (i64, i64) {
|
impl CdsConverter for ConversionFromDatetime {
|
||||||
let secs_of_day = unix_seconds % SECONDS_PER_DAY as i64;
|
fn submillis_precision(&self) -> Option<SubmillisPrecision> {
|
||||||
|
self.submillis_prec
|
||||||
|
}
|
||||||
|
|
||||||
|
delegate! {
|
||||||
|
to self.unix_conversion {
|
||||||
|
fn ms_of_day(&self) -> u32;
|
||||||
|
fn ccsds_days(&self) -> u32;
|
||||||
|
fn unix_days_seconds(&self) -> i64;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
fn calc_unix_days_and_secs_of_day(unix_seconds: i64) -> (i64, u32) {
|
||||||
|
let mut secs_of_day = unix_seconds % SECONDS_PER_DAY as i64;
|
||||||
let unix_days = if secs_of_day > 0 {
|
let unix_days = if secs_of_day > 0 {
|
||||||
(unix_seconds - secs_of_day) / SECONDS_PER_DAY as i64
|
(unix_seconds - secs_of_day) / SECONDS_PER_DAY as i64
|
||||||
} else {
|
} else {
|
||||||
(unix_seconds + secs_of_day) / SECONDS_PER_DAY as i64
|
(unix_seconds + secs_of_day) / SECONDS_PER_DAY as i64
|
||||||
};
|
};
|
||||||
(unix_days, secs_of_day)
|
if secs_of_day < 0 {
|
||||||
|
secs_of_day = -secs_of_day
|
||||||
|
}
|
||||||
|
(unix_days, secs_of_day as u32)
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConversionFromDatetime {
|
impl ConversionFromDatetime {
|
||||||
fn new(dt: DateTime<Utc>) -> Self {
|
fn new(dt: DateTime<Utc>) -> Result<Self, DateTime<Utc>> {
|
||||||
|
Self::new_generic(dt, None)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_with_submillis_us_prec(dt: DateTime<Utc>) -> Result<Self, DateTime<Utc>> {
|
||||||
|
Self::new_generic(dt, Some(SubmillisPrecision::Microseconds(0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_with_submillis_ps_prec(dt: DateTime<Utc>) -> Result<Self, DateTime<Utc>> {
|
||||||
|
Self::new_generic(dt, Some(SubmillisPrecision::Picoseconds(0)))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn new_generic(
|
||||||
|
dt: DateTime<Utc>,
|
||||||
|
mut prec: Option<SubmillisPrecision>,
|
||||||
|
) -> Result<Self, DateTime<Utc>> {
|
||||||
// The CDS timestamp does not support timestamps before the CCSDS epoch.
|
// The CDS timestamp does not support timestamps before the CCSDS epoch.
|
||||||
assert!(dt.year() >= 1958);
|
if dt.year() < 1958 {
|
||||||
let unix_seconds = dt.timestamp();
|
return Err(dt);
|
||||||
let (unix_days, _) = calc_unix_days_and_secs_of_day(unix_seconds);
|
|
||||||
// This should always be positive now.
|
|
||||||
let ccsds_days = unix_to_ccsds_days(unix_days) as u32;
|
|
||||||
let unix_ms = dt.timestamp_millis();
|
|
||||||
let mut ms_of_day = unix_ms % MS_PER_DAY as i64;
|
|
||||||
if ms_of_day < 0 {
|
|
||||||
ms_of_day = -ms_of_day;
|
|
||||||
}
|
}
|
||||||
Self {
|
// The contained values in the conversion should be all positive now
|
||||||
ccsds_days,
|
let unix_conversion = ConversionFromUnix::new(dt.timestamp(), dt.timestamp_subsec_millis());
|
||||||
ms_of_day: ms_of_day as u32,
|
if let Some(submilli_prec) = prec {
|
||||||
unix_days_seconds: unix_days * SECONDS_PER_DAY as i64,
|
match submilli_prec {
|
||||||
submillis_prec: None,
|
SubmillisPrecision::Microseconds(_) => {
|
||||||
|
prec = Some(SubmillisPrecision::Microseconds(
|
||||||
|
(dt.timestamp_subsec_micros() % 1000) as u16,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
SubmillisPrecision::Picoseconds(_) => {
|
||||||
|
prec = Some(SubmillisPrecision::Microseconds(
|
||||||
|
(dt.timestamp_subsec_nanos() * 1000) as u16,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Ok(Self {
|
||||||
|
unix_conversion,
|
||||||
|
submillis_prec: prec,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
struct ConversionFromNow {
|
struct ConversionFromNow {
|
||||||
base: ConversionFromDatetime,
|
unix_conversion: ConversionFromUnix,
|
||||||
|
submillis_prec: Option<SubmillisPrecision>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
@ -205,8 +290,8 @@ impl ConversionFromNow {
|
|||||||
fn new_generic(mut prec: Option<SubmillisPrecision>) -> Result<Self, SystemTimeError> {
|
fn new_generic(mut prec: Option<SubmillisPrecision>) -> 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();
|
||||||
|
let unix_conversion = ConversionFromUnix::new(epoch as i64, now.subsec_millis());
|
||||||
// Both values should now be positive
|
// Both values should now be positive
|
||||||
let (unix_days, secs_of_day) = calc_unix_days_and_secs_of_day(epoch as i64);
|
|
||||||
if let Some(submilli_prec) = prec {
|
if let Some(submilli_prec) = prec {
|
||||||
match submilli_prec {
|
match submilli_prec {
|
||||||
SubmillisPrecision::Microseconds(_) => {
|
SubmillisPrecision::Microseconds(_) => {
|
||||||
@ -223,16 +308,26 @@ impl ConversionFromNow {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
base: ConversionFromDatetime {
|
unix_conversion,
|
||||||
ms_of_day: secs_of_day as u32 * 1000 + now.subsec_millis(),
|
submillis_prec: prec,
|
||||||
ccsds_days: unix_to_ccsds_days(unix_days) as u32,
|
|
||||||
unix_days_seconds: unix_days * SECONDS_PER_DAY as i64,
|
|
||||||
submillis_prec: prec,
|
|
||||||
},
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl CdsConverter for ConversionFromNow {
|
||||||
|
fn submillis_precision(&self) -> Option<SubmillisPrecision> {
|
||||||
|
self.submillis_prec
|
||||||
|
}
|
||||||
|
|
||||||
|
delegate! {
|
||||||
|
to self.unix_conversion {
|
||||||
|
fn ms_of_day(&self) -> u32;
|
||||||
|
fn ccsds_days(&self) -> u32;
|
||||||
|
fn unix_days_seconds(&self) -> i64;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<ProvidesDaysLen: ProvidesDaysLength> TimeProvider<ProvidesDaysLen> {
|
impl<ProvidesDaysLen: ProvidesDaysLength> TimeProvider<ProvidesDaysLen> {
|
||||||
pub fn set_submillis_precision(&mut self, prec: SubmillisPrecision) {
|
pub fn set_submillis_precision(&mut self, prec: SubmillisPrecision) {
|
||||||
self.pfield &= !(0b11);
|
self.pfield &= !(0b11);
|
||||||
@ -343,6 +438,7 @@ impl<ProvidesDaysLen: ProvidesDaysLength> TimeProvider<ProvidesDaysLen> {
|
|||||||
self.calc_unix_seconds(unix_days_seconds, ms_of_day);
|
self.calc_unix_seconds(unix_days_seconds, ms_of_day);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn calc_unix_seconds(&mut self, unix_days_seconds: i64, ms_of_day: u32) {
|
fn calc_unix_seconds(&mut self, unix_days_seconds: i64, ms_of_day: u32) {
|
||||||
self.unix_seconds = unix_days_seconds;
|
self.unix_seconds = unix_days_seconds;
|
||||||
let seconds_of_day = (ms_of_day / 1000) as i64;
|
let seconds_of_day = (ms_of_day / 1000) as i64;
|
||||||
@ -393,38 +489,87 @@ impl<ProvidesDaysLen: ProvidesDaysLength> TimeProvider<ProvidesDaysLen> {
|
|||||||
submillis_precision: None,
|
submillis_precision: None,
|
||||||
};
|
};
|
||||||
let unix_days_seconds = ccsds_to_unix_days(i64::from(ccsds_days)) * SECONDS_PER_DAY as i64;
|
let unix_days_seconds = ccsds_to_unix_days(i64::from(ccsds_days)) * SECONDS_PER_DAY as i64;
|
||||||
provider.setup(unix_days_seconds, ms_of_day.into());
|
provider.setup(unix_days_seconds, ms_of_day);
|
||||||
Ok(provider)
|
Ok(provider)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(feature = "std")]
|
pub fn from_dt_generic(
|
||||||
fn generic_from_now(
|
dt: DateTime<Utc>,
|
||||||
days_len: LengthOfDaySegment,
|
days_len: LengthOfDaySegment,
|
||||||
conversion_from_now: ConversionFromNow,
|
) -> Result<Self, TimestampError> {
|
||||||
) -> Result<Self, StdTimestampError>
|
let conv_from_dt =
|
||||||
where
|
ConversionFromDatetime::new(dt).map_err(TimestampError::DateBeforeCcsdsEpoch)?;
|
||||||
<ProvidesDaysLen as ProvidesDaysLength>::FieldType: TryFrom<u32>,
|
Self::generic_from_conversion(days_len, conv_from_dt)
|
||||||
{
|
}
|
||||||
let ccsds_days: ProvidesDaysLen::FieldType = conversion_from_now
|
|
||||||
.base
|
pub fn from_dt_generic_us_prec(
|
||||||
.ccsds_days
|
dt: DateTime<Utc>,
|
||||||
.try_into()
|
days_len: LengthOfDaySegment,
|
||||||
.map_err(|_| {
|
) -> Result<Self, TimestampError> {
|
||||||
StdTimestampError::TimestampError(
|
let conv_from_dt = ConversionFromDatetime::new_with_submillis_us_prec(dt)
|
||||||
CdsError::InvalidCcsdsDays(conversion_from_now.base.ccsds_days.into()).into(),
|
.map_err(TimestampError::DateBeforeCcsdsEpoch)?;
|
||||||
)
|
Self::generic_from_conversion(days_len, conv_from_dt)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_dt_generic_ps_prec(
|
||||||
|
dt: DateTime<Utc>,
|
||||||
|
days_len: LengthOfDaySegment,
|
||||||
|
) -> Result<Self, TimestampError> {
|
||||||
|
let conv_from_dt = ConversionFromDatetime::new_with_submillis_ps_prec(dt)
|
||||||
|
.map_err(TimestampError::DateBeforeCcsdsEpoch)?;
|
||||||
|
Self::generic_from_conversion(days_len, conv_from_dt)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_unix_generic(
|
||||||
|
unix_seconds: i64,
|
||||||
|
subsec_millis: u32,
|
||||||
|
days_len: LengthOfDaySegment,
|
||||||
|
) -> Result<Self, TimestampError> {
|
||||||
|
let conv_from_dt = ConversionFromUnix::new(unix_seconds, subsec_millis);
|
||||||
|
Self::generic_from_conversion(days_len, conv_from_dt)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub fn from_now_generic(days_len: LengthOfDaySegment) -> Result<Self, StdTimestampError> {
|
||||||
|
let conversion_from_now = ConversionFromNow::new()?;
|
||||||
|
Self::generic_from_conversion(days_len, conversion_from_now)
|
||||||
|
.map_err(StdTimestampError::TimestampError)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub fn from_now_generic_us_prec(
|
||||||
|
days_len: LengthOfDaySegment,
|
||||||
|
) -> Result<Self, StdTimestampError> {
|
||||||
|
let conversion_from_now = ConversionFromNow::new_with_submillis_us_prec()?;
|
||||||
|
Self::generic_from_conversion(days_len, conversion_from_now)
|
||||||
|
.map_err(StdTimestampError::TimestampError)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(feature = "std")]
|
||||||
|
pub fn from_now_generic_ps_prec(
|
||||||
|
days_len: LengthOfDaySegment,
|
||||||
|
) -> Result<Self, StdTimestampError> {
|
||||||
|
let conversion_from_now = ConversionFromNow::new_with_submillis_ps_prec()?;
|
||||||
|
Self::generic_from_conversion(days_len, conversion_from_now)
|
||||||
|
.map_err(StdTimestampError::TimestampError)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn generic_from_conversion<C: CdsConverter>(
|
||||||
|
days_len: LengthOfDaySegment,
|
||||||
|
converter: C,
|
||||||
|
) -> Result<Self, TimestampError> {
|
||||||
|
let ccsds_days: ProvidesDaysLen::FieldType =
|
||||||
|
converter.ccsds_days().try_into().map_err(|_| {
|
||||||
|
TimestampError::CdsError(CdsError::InvalidCcsdsDays(converter.ccsds_days().into()))
|
||||||
})?;
|
})?;
|
||||||
let mut provider = Self {
|
let mut provider = Self {
|
||||||
pfield: Self::generate_p_field(days_len, conversion_from_now.base.submillis_prec),
|
pfield: Self::generate_p_field(days_len, converter.submillis_precision()),
|
||||||
ccsds_days,
|
ccsds_days,
|
||||||
ms_of_day: conversion_from_now.base.ms_of_day as u32,
|
ms_of_day: converter.ms_of_day(),
|
||||||
unix_seconds: 0,
|
unix_seconds: 0,
|
||||||
submillis_precision: conversion_from_now.base.submillis_prec,
|
submillis_precision: converter.submillis_precision(),
|
||||||
};
|
};
|
||||||
provider.setup(
|
provider.setup(converter.unix_days_seconds(), converter.ms_of_day());
|
||||||
conversion_from_now.base.unix_days_seconds as i64,
|
|
||||||
conversion_from_now.base.ms_of_day,
|
|
||||||
);
|
|
||||||
Ok(provider)
|
Ok(provider)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -464,64 +609,82 @@ impl<ProvidesDaysLen: ProvidesDaysLength> TimeProvider<ProvidesDaysLen> {
|
|||||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||||
pub fn update_from_now(&mut self) -> Result<(), StdTimestampError>
|
pub fn update_from_now(&mut self) -> Result<(), StdTimestampError>
|
||||||
where
|
where
|
||||||
<ProvidesDaysLen as ProvidesDaysLength>::FieldType: From<u32>,
|
<ProvidesDaysLen as ProvidesDaysLength>::FieldType: TryFrom<u32>,
|
||||||
{
|
{
|
||||||
let conversion_from_now = self.generic_conversion_from_now()?;
|
let conversion_from_now = self.generic_conversion_from_now()?;
|
||||||
let ccsds_days: ProvidesDaysLen::FieldType = conversion_from_now
|
let ccsds_days: ProvidesDaysLen::FieldType = conversion_from_now
|
||||||
.base
|
.unix_conversion
|
||||||
.ccsds_days
|
.ccsds_days
|
||||||
.try_into()
|
.try_into()
|
||||||
.map_err(|_| {
|
.map_err(|_| {
|
||||||
StdTimestampError::TimestampError(
|
StdTimestampError::TimestampError(
|
||||||
CdsError::InvalidCcsdsDays(conversion_from_now.base.ccsds_days as i64).into(),
|
CdsError::InvalidCcsdsDays(
|
||||||
|
conversion_from_now.unix_conversion.ccsds_days as i64,
|
||||||
|
)
|
||||||
|
.into(),
|
||||||
)
|
)
|
||||||
})?;
|
})?;
|
||||||
self.ccsds_days = ccsds_days;
|
self.ccsds_days = ccsds_days;
|
||||||
self.ms_of_day = conversion_from_now.base.ms_of_day as u32;
|
self.ms_of_day = conversion_from_now.unix_conversion.ms_of_day;
|
||||||
self.setup(
|
self.setup(
|
||||||
conversion_from_now.base.unix_days_seconds as i64,
|
conversion_from_now.unix_conversion.unix_days_seconds,
|
||||||
conversion_from_now.base.ms_of_day,
|
conversion_from_now.unix_conversion.ms_of_day,
|
||||||
);
|
);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TimeProvider<DaysLen24Bits> {
|
impl TimeProvider<DaysLen24Bits> {
|
||||||
pub fn new_with_u24_days_from_unix_stamp(unix_seconds: i64) {
|
|
||||||
//let seconds_of_day = unix_seconds % SECONDS_PER_DAY;
|
|
||||||
//let unix_days = (unix_seconds - seconds_of_day) / SECONDS_PER_DAY;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Generate a new timestamp provider with the days field width set to 24 bits
|
/// Generate a new timestamp provider with the days field width set to 24 bits
|
||||||
pub fn new_with_u24_days(ccsds_days: u32, ms_of_day: u32) -> Result<Self, CdsError> {
|
pub fn new(ccsds_days: u32, ms_of_day: u32) -> Result<Self, CdsError> {
|
||||||
if ccsds_days > 2_u32.pow(24) {
|
if ccsds_days > MAX_DAYS_24_BITS {
|
||||||
return Err(CdsError::InvalidCcsdsDays(ccsds_days.into()));
|
return Err(CdsError::InvalidCcsdsDays(ccsds_days.into()));
|
||||||
}
|
}
|
||||||
Self::generic_new(LengthOfDaySegment::Long24Bits, ccsds_days, ms_of_day)
|
Self::generic_new(LengthOfDaySegment::Long24Bits, ccsds_days, ms_of_day)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_with_u24_days(ccsds_days: u32, ms_of_day: u32) -> Result<Self, CdsError> {
|
||||||
|
Self::new(ccsds_days, ms_of_day)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_unix_seconds_u24_days(
|
||||||
|
unix_seconds: i64,
|
||||||
|
subsec_millis: u32,
|
||||||
|
) -> Result<Self, TimestampError> {
|
||||||
|
Self::from_unix_generic(unix_seconds, subsec_millis, LengthOfDaySegment::Long24Bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_dt_u24_days(dt: DateTime<Utc>) -> Result<Self, TimestampError> {
|
||||||
|
Self::from_dt_generic(dt, LengthOfDaySegment::Long24Bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_dt_with_us_precision_u24_days(dt: DateTime<Utc>) -> Result<Self, TimestampError> {
|
||||||
|
Self::from_dt_generic_us_prec(dt, LengthOfDaySegment::Long24Bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_dt_with_ps_precision_u24_days(dt: DateTime<Utc>) -> Result<Self, TimestampError> {
|
||||||
|
Self::from_dt_generic_ps_prec(dt, LengthOfDaySegment::Long24Bits)
|
||||||
|
}
|
||||||
|
|
||||||
/// 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")]
|
||||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||||
pub fn from_now_with_u24_days() -> Result<Self, StdTimestampError> {
|
pub fn from_now_with_u24_days() -> Result<Self, StdTimestampError> {
|
||||||
let conversion_from_now = ConversionFromNow::new()?;
|
Self::from_now_generic(LengthOfDaySegment::Long24Bits)
|
||||||
Self::generic_from_now(LengthOfDaySegment::Long24Bits, conversion_from_now)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Like [Self::from_now_with_u24_days] but with microsecond sub-millisecond precision.
|
/// Like [Self::from_now] but with microsecond sub-millisecond precision.
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||||
pub fn from_now_with_u24_days_and_us_prec() -> Result<Self, StdTimestampError> {
|
pub fn from_now_with_us_prec() -> Result<Self, StdTimestampError> {
|
||||||
let conversion_from_now = ConversionFromNow::new_with_submillis_us_prec()?;
|
Self::from_now_generic_us_prec(LengthOfDaySegment::Long24Bits)
|
||||||
Self::generic_from_now(LengthOfDaySegment::Long24Bits, conversion_from_now)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Like [Self::from_now_with_u24_days] but with picoseconds sub-millisecond precision.
|
/// Like [Self::from_now] but with picoseconds sub-millisecond precision.
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||||
pub fn from_now_with_u24_days_ps_submillis_prec() -> Result<Self, StdTimestampError> {
|
pub fn from_now_with_ps_prec() -> Result<Self, StdTimestampError> {
|
||||||
let conversion_from_now = ConversionFromNow::new_with_submillis_ps_prec()?;
|
Self::from_now_generic_us_prec(LengthOfDaySegment::Long24Bits)
|
||||||
Self::generic_from_now(LengthOfDaySegment::Long24Bits, conversion_from_now)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes_24_bit_days(buf: &[u8]) -> Result<Self, TimestampError> {
|
fn from_bytes_24_bit_days(buf: &[u8]) -> Result<Self, TimestampError> {
|
||||||
@ -531,7 +694,7 @@ impl TimeProvider<DaysLen24Bits> {
|
|||||||
temp_buf[1..4].copy_from_slice(&buf[1..4]);
|
temp_buf[1..4].copy_from_slice(&buf[1..4]);
|
||||||
let cccsds_days: u32 = u32::from_be_bytes(temp_buf);
|
let cccsds_days: u32 = u32::from_be_bytes(temp_buf);
|
||||||
let ms_of_day: u32 = u32::from_be_bytes(buf[4..8].try_into().unwrap());
|
let ms_of_day: u32 = u32::from_be_bytes(buf[4..8].try_into().unwrap());
|
||||||
let mut provider = Self::new_with_u24_days(cccsds_days, ms_of_day)?;
|
let mut provider = Self::new(cccsds_days, ms_of_day)?;
|
||||||
match submillis_precision {
|
match submillis_precision {
|
||||||
SubmillisPrecision::Microseconds(_) => {
|
SubmillisPrecision::Microseconds(_) => {
|
||||||
provider.set_submillis_precision(SubmillisPrecision::Microseconds(
|
provider.set_submillis_precision(SubmillisPrecision::Microseconds(
|
||||||
@ -549,33 +712,53 @@ impl TimeProvider<DaysLen24Bits> {
|
|||||||
|
|
||||||
impl TimeProvider<DaysLen16Bits> {
|
impl TimeProvider<DaysLen16Bits> {
|
||||||
/// Generate a new timestamp provider with the days field width set to 16 bits
|
/// Generate a new timestamp provider with the days field width set to 16 bits
|
||||||
pub fn new_with_u16_days(ccsds_days: u16, ms_of_day: u32) -> Self {
|
pub fn new(ccsds_days: u16, ms_of_day: u32) -> Self {
|
||||||
// This should never fail, type system ensures CCSDS can not be negative or too large
|
// This should never fail, type system ensures CCSDS can not be negative or too large
|
||||||
Self::generic_new(LengthOfDaySegment::Short16Bits, ccsds_days, ms_of_day).unwrap()
|
Self::generic_new(LengthOfDaySegment::Short16Bits, ccsds_days, ms_of_day).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_with_u16_days(ccsds_days: u16, ms_of_day: u32) -> Self {
|
||||||
|
Self::new(ccsds_days, ms_of_day)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_unix_seconds_with_u16_days(
|
||||||
|
unix_seconds: i64,
|
||||||
|
subsec_millis: u32,
|
||||||
|
) -> Result<Self, TimestampError> {
|
||||||
|
Self::from_unix_generic(unix_seconds, subsec_millis, LengthOfDaySegment::Short16Bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_dt_with_u16_days(dt: DateTime<Utc>) -> Result<Self, TimestampError> {
|
||||||
|
Self::from_dt_generic(dt, LengthOfDaySegment::Short16Bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_dt_with_u16_days_us_precision(dt: DateTime<Utc>) -> Result<Self, TimestampError> {
|
||||||
|
Self::from_dt_generic_us_prec(dt, LengthOfDaySegment::Short16Bits)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_dt_with_u16_days_ps_precision(dt: DateTime<Utc>) -> Result<Self, TimestampError> {
|
||||||
|
Self::from_dt_generic_ps_prec(dt, LengthOfDaySegment::Short16Bits)
|
||||||
|
}
|
||||||
|
|
||||||
/// 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")]
|
||||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||||
pub fn from_now_with_u16_days() -> Result<Self, StdTimestampError> {
|
pub fn from_now_with_u16_days() -> Result<Self, StdTimestampError> {
|
||||||
let conversion_from_now = ConversionFromNow::new()?;
|
Self::from_now_generic(LengthOfDaySegment::Short16Bits)
|
||||||
Self::generic_from_now(LengthOfDaySegment::Short16Bits, conversion_from_now)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Like [Self::from_now_with_u16_days] but with microsecond sub-millisecond precision.
|
/// Like [Self::from_now_with_u16_days] but with microsecond sub-millisecond precision.
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||||
pub fn from_now_with_u16_days_and_us_prec() -> Result<Self, StdTimestampError> {
|
pub fn from_now_with_u16_days_us_prec() -> Result<Self, StdTimestampError> {
|
||||||
let conversion_from_now = ConversionFromNow::new_with_submillis_us_prec()?;
|
Self::from_now_generic_us_prec(LengthOfDaySegment::Short16Bits)
|
||||||
Self::generic_from_now(LengthOfDaySegment::Short16Bits, conversion_from_now)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Like [Self::from_now_with_u16_days] but with picosecond sub-millisecond precision.
|
/// Like [Self::from_now_with_u16_days] but with picosecond sub-millisecond precision.
|
||||||
#[cfg(feature = "std")]
|
#[cfg(feature = "std")]
|
||||||
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
|
||||||
pub fn from_now_with_u16_days_and_ps_prec() -> Result<Self, StdTimestampError> {
|
pub fn from_now_with_u16_days_ps_prec() -> Result<Self, StdTimestampError> {
|
||||||
let conversion_from_now = ConversionFromNow::new_with_submillis_ps_prec()?;
|
Self::from_now_generic_ps_prec(LengthOfDaySegment::Short16Bits)
|
||||||
Self::generic_from_now(LengthOfDaySegment::Short16Bits, conversion_from_now)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes_16_bit_days(buf: &[u8]) -> Result<Self, TimestampError> {
|
fn from_bytes_16_bit_days(buf: &[u8]) -> Result<Self, TimestampError> {
|
||||||
@ -583,7 +766,7 @@ impl TimeProvider<DaysLen16Bits> {
|
|||||||
Self::generic_raw_read_checks(buf, LengthOfDaySegment::Short16Bits)?;
|
Self::generic_raw_read_checks(buf, LengthOfDaySegment::Short16Bits)?;
|
||||||
let ccsds_days: u16 = u16::from_be_bytes(buf[1..3].try_into().unwrap());
|
let ccsds_days: u16 = u16::from_be_bytes(buf[1..3].try_into().unwrap());
|
||||||
let ms_of_day: u32 = u32::from_be_bytes(buf[3..7].try_into().unwrap());
|
let ms_of_day: u32 = u32::from_be_bytes(buf[3..7].try_into().unwrap());
|
||||||
let mut provider = Self::new_with_u16_days(ccsds_days, ms_of_day);
|
let mut provider = Self::new(ccsds_days, ms_of_day);
|
||||||
provider.pfield = buf[0];
|
provider.pfield = buf[0];
|
||||||
match submillis_precision {
|
match submillis_precision {
|
||||||
SubmillisPrecision::Microseconds(_) => provider.set_submillis_precision(
|
SubmillisPrecision::Microseconds(_) => provider.set_submillis_precision(
|
||||||
@ -661,7 +844,7 @@ impl Add<Duration> for TimeProvider<DaysLen16Bits> {
|
|||||||
fn add(self, duration: Duration) -> Self::Output {
|
fn add(self, duration: Duration) -> Self::Output {
|
||||||
let (next_ccsds_days, next_ms_of_day, precision) =
|
let (next_ccsds_days, next_ms_of_day, precision) =
|
||||||
add_for_max_ccsds_days_val(self, u16::MAX as u32, duration);
|
add_for_max_ccsds_days_val(self, u16::MAX as u32, duration);
|
||||||
let mut provider = Self::new_with_u16_days(next_ccsds_days as u16, next_ms_of_day);
|
let mut provider = Self::new(next_ccsds_days as u16, next_ms_of_day);
|
||||||
if let Some(prec) = precision {
|
if let Some(prec) = precision {
|
||||||
provider.set_submillis_precision(prec);
|
provider.set_submillis_precision(prec);
|
||||||
}
|
}
|
||||||
@ -675,7 +858,7 @@ impl Add<Duration> for TimeProvider<DaysLen24Bits> {
|
|||||||
fn add(self, duration: Duration) -> Self::Output {
|
fn add(self, duration: Duration) -> Self::Output {
|
||||||
let (next_ccsds_days, next_ms_of_day, precision) =
|
let (next_ccsds_days, next_ms_of_day, precision) =
|
||||||
add_for_max_ccsds_days_val(self, MAX_DAYS_24_BITS, duration);
|
add_for_max_ccsds_days_val(self, MAX_DAYS_24_BITS, duration);
|
||||||
let mut provider = Self::new_with_u24_days(next_ccsds_days, next_ms_of_day).unwrap();
|
let mut provider = Self::new(next_ccsds_days, next_ms_of_day).unwrap();
|
||||||
if let Some(prec) = precision {
|
if let Some(prec) = precision {
|
||||||
provider.set_submillis_precision(prec);
|
provider.set_submillis_precision(prec);
|
||||||
}
|
}
|
||||||
@ -684,12 +867,12 @@ impl Add<Duration> for TimeProvider<DaysLen24Bits> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<DateTime<Utc>> for TimeProvider<DaysLen16Bits> {
|
impl TryFrom<DateTime<Utc>> for TimeProvider<DaysLen16Bits> {
|
||||||
type Error = ();
|
type Error = TimestampError;
|
||||||
|
|
||||||
fn try_from(dt: DateTime<Utc>) -> Result<Self, Self::Error> {
|
fn try_from(dt: DateTime<Utc>) -> Result<Self, Self::Error> {
|
||||||
//todo!()
|
let conversion =
|
||||||
//let unix_ms = dt.timestamp_millis();
|
ConversionFromDatetime::new(dt).map_err(TimestampError::DateBeforeCcsdsEpoch)?;
|
||||||
Ok(Self::new_with_u16_days(0, 0))
|
Self::generic_from_conversion(LengthOfDaySegment::Short16Bits, conversion)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<ProvidesDaysLen: ProvidesDaysLength> CcsdsTimeProvider for TimeProvider<ProvidesDaysLen> {
|
impl<ProvidesDaysLen: ProvidesDaysLength> CcsdsTimeProvider for TimeProvider<ProvidesDaysLen> {
|
||||||
@ -829,7 +1012,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_large_days_field_write() {
|
fn test_large_days_field_write() {
|
||||||
let time_stamper = TimeProvider::new_with_u24_days(0x108020, 0);
|
let time_stamper = TimeProvider::new_with_u24_days(0x108020_u32, 0);
|
||||||
assert!(time_stamper.is_ok());
|
assert!(time_stamper.is_ok());
|
||||||
let time_stamper = time_stamper.unwrap();
|
let time_stamper = time_stamper.unwrap();
|
||||||
assert_eq!(time_stamper.len_as_bytes(), 8);
|
assert_eq!(time_stamper.len_as_bytes(), 8);
|
||||||
@ -848,7 +1031,7 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_large_days_field_read() {
|
fn test_large_days_field_read() {
|
||||||
let time_stamper = TimeProvider::new_with_u24_days(0x108020, 0);
|
let time_stamper = TimeProvider::new_with_u24_days(0x108020_u32, 0);
|
||||||
assert!(time_stamper.is_ok());
|
assert!(time_stamper.is_ok());
|
||||||
let time_stamper = time_stamper.unwrap();
|
let time_stamper = time_stamper.unwrap();
|
||||||
let mut buf = [0; 16];
|
let mut buf = [0; 16];
|
||||||
|
@ -63,6 +63,7 @@ pub enum TimestampError {
|
|||||||
ByteConversionError(ByteConversionError),
|
ByteConversionError(ByteConversionError),
|
||||||
CdsError(cds::CdsError),
|
CdsError(cds::CdsError),
|
||||||
CucError(cuc::CucError),
|
CucError(cuc::CucError),
|
||||||
|
DateBeforeCcsdsEpoch(DateTime<Utc>),
|
||||||
CustomEpochNotSupported,
|
CustomEpochNotSupported,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,6 +120,9 @@ impl Display for TimestampError {
|
|||||||
TimestampError::ByteConversionError(e) => {
|
TimestampError::ByteConversionError(e) => {
|
||||||
write!(f, "byte conversion error {}", e)
|
write!(f, "byte conversion error {}", e)
|
||||||
}
|
}
|
||||||
|
TimestampError::DateBeforeCcsdsEpoch(e) => {
|
||||||
|
write!(f, "datetime with date before ccsds epoch: {}", e)
|
||||||
|
}
|
||||||
TimestampError::CustomEpochNotSupported => {
|
TimestampError::CustomEpochNotSupported => {
|
||||||
write!(f, "custom epochs are not supported")
|
write!(f, "custom epochs are not supported")
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user