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<Self, ParseNTP64Error>
pub fn parse_rfc3339(s: &str) -> Result<Self, 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<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl Display for NTP64
impl Display for NTP64
source§fn fmt(&self, f: &mut Formatter<'_>) -> Result
fn fmt(&self, f: &mut Formatter<'_>) -> Result
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 PartialEq for NTP64
impl PartialEq for NTP64
source§impl PartialOrd for NTP64
impl PartialOrd for NTP64
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§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 more