pub struct NTP64(pub u64);
Expand description
A NTP 64-bits format as specified in RFC-5909
0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Seconds |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Fraction |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
The 1st 32-bits part is the number of second since the EPOCH of the physical clock,
and the 2nd 32-bits part is the fraction of second.
In case it’s part of a crate::Timestamp
generated by an crate::HLC
the last few bits
of the Fraction part are replaced by the HLC logical counter.
The size of this counter is currently hard-coded as crate::CSIZE
.
§Conversion to/from String
2 different String representations are supported:
- as an unsigned integer in decimal format
- Such conversion is lossless and thus bijective.
- NTP64 to String: use
std::fmt::Display::fmt()
orstd::string::ToString::to_string()
. - String to NTP64: use
std::str::FromStr::from_str()
- as a RFC3339 (human readable) format:
- Such conversion loses some precision because of rounding when conferting the fraction part to nanoseconds
- As a consequence it’s not bijective: a NTP64 converted to RFC3339 String and then converted back to NTP64 might result to a different time.
- NTP64 to String: use
std::fmt::Display::fmt()
with the alternate flag ({:#}
) orNTP64::to_string_rfc3339_lossy()
. - String to NTP64: use
NTP64::parse_rfc3339()
§On EPOCH
This timestamp in actually similar to a std::time::Duration
, as it doesn’t define an EPOCH.
Only NTP64::to_system_time()
, NTP64::to_string_rfc3339_lossy()
and std::fmt::Display::fmt()
(when using {:#}
alternate flag)
operations assume that it’s relative to UNIX_EPOCH (1st Jan 1970) to display the timestamp in RFC-3339 format.
Tuple Fields§
§0: u64
Implementations§
Source§impl NTP64
impl NTP64
Sourcepub fn as_secs_f64(&self) -> f64
pub fn as_secs_f64(&self) -> f64
Returns this NTP64 as a f64 in seconds.
The integer part of the f64 is the NTP64’s Seconds part.
The decimal part of the f64 is the result of a division of NTP64’s Fraction part divided by 2^32.
Considering the probable large number of Seconds (for a time relative to UNIX_EPOCH), the precision of the resulting f64 might be in the order of microseconds.
Therefore, it should not be used for comparison. Directly comparing NTP64 objects is preferable.
Sourcepub fn subsec_nanos(&self) -> u32
pub fn subsec_nanos(&self) -> u32
Returns the 32-bits fraction of second part converted to nanoseconds.
Sourcepub fn to_duration(self) -> Duration
pub fn to_duration(self) -> Duration
Convert to a Duration
.
Sourcepub fn to_system_time(self) -> SystemTime
pub fn to_system_time(self) -> SystemTime
Convert to a SystemTime
(making the assumption that this NTP64 is relative to UNIX_EPOCH
).
Sourcepub fn to_string_rfc3339_lossy(&self) -> String
pub fn to_string_rfc3339_lossy(&self) -> String
Convert to a RFC3339 time representation with nanoseconds precision. e.g.: `“2024-07-01T13:51:12.129693000Z”``
Sourcepub fn parse_rfc3339(s: &str) -> Result<NTP64, ParseNTP64Error>
pub fn parse_rfc3339(s: &str) -> Result<NTP64, ParseNTP64Error>
Parse a RFC3339 time representation into a NTP64.
Trait Implementations§
Source§impl AddAssign<u64> for NTP64
impl AddAssign<u64> for NTP64
Source§fn add_assign(&mut self, other: u64)
fn add_assign(&mut self, other: u64)
+=
operation. Read moreSource§impl<'de> Deserialize<'de> for NTP64
impl<'de> Deserialize<'de> for NTP64
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<NTP64, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<NTP64, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Display for NTP64
impl Display for NTP64
Source§fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>
By default formats the value as an unsigned integer in decimal format.
If the alternate flag {:#}
is used, formats the value with RFC3339 representation with nanoseconds precision.
§Examples
use uhlc::NTP64;
let t = NTP64(7386690599959157260);
println!("{t}"); // displays: 7386690599959157260
println!("{t:#}"); // displays: 2024-07-01T15:32:06.860479000Z
Source§impl Ord for NTP64
impl Ord for NTP64
Source§impl PartialOrd for NTP64
impl PartialOrd for NTP64
Source§impl Serialize for NTP64
impl Serialize for NTP64
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Source§impl SubAssign<u64> for NTP64
impl SubAssign<u64> for NTP64
Source§fn sub_assign(&mut self, other: u64)
fn sub_assign(&mut self, other: u64)
-=
operation. Read moreimpl Copy for NTP64
impl Eq for NTP64
impl StructuralPartialEq for NTP64
Auto Trait Implementations§
impl Freeze for NTP64
impl RefUnwindSafe for NTP64
impl Send for NTP64
impl Sync for NTP64
impl Unpin for NTP64
impl UnwindSafe for NTP64
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more