pub struct Instant { /* private fields */ }Expand description
Describes an instant relative to the UNIX_EPOCH - 00:00:00 Coordinated Universal Time (UTC),
Thursay, 1 January 1970 in seconds with the fractional part in nanoseconds.
If the Instant describes some moment prior to UNIX_EPOCH, both the secs and
subsec_nanos components will be negative.
The sole purpose of this type is for retrieving the “current” time using the std::time module
and for converting between the ntp timestamp formats. If you are interested in converting from
unix time to some other more human readable format, perhaps see the chrono
crate.
§Example
Here is a demonstration of displaying the Instant in local time using the chrono crate
(requires the std feature):
use chrono::TimeZone;
let unix_time = ntp_proto::unix_time::Instant::now();
let local_time = chrono::Local.timestamp(unix_time.secs(), unix_time.subsec_nanos() as _);
println!("{}", local_time);Implementations§
Source§impl Instant
impl Instant
Sourcepub fn new(secs: i64, subsec_nanos: i32) -> Instant
pub fn new(secs: i64, subsec_nanos: i32) -> Instant
Create a new Instant given its secs and subsec_nanos components.
To indicate a time following UNIX_EPOCH, both secs and subsec_nanos must be positive.
To indicate a time prior to UNIX_EPOCH, both secs and subsec_nanos must be negative.
Violating these invariants will result in a panic!.
Sourcepub fn now() -> Instant
pub fn now() -> Instant
Uses std::time::SystemTime::now and std::time::UNIX_EPOCH to determine the current
Instant.
§Example
println!("{:?}", ntp_proto::unix_time::Instant::now());Sourcepub fn subsec_nanos(&self) -> i32
pub fn subsec_nanos(&self) -> i32
The fractional component of the Instant in nanoseconds.
Trait Implementations§
Source§impl From<DateFormat> for Instant
impl From<DateFormat> for Instant
Source§fn from(d: DateFormat) -> Instant
fn from(d: DateFormat) -> Instant
Converts a 128-bit NTP date format (with explicit era) to a Unix Instant.
This conversion is unambiguous because protocol::DateFormat includes the era number.
Source§impl From<Instant> for DateFormat
impl From<Instant> for DateFormat
Source§impl From<Instant> for ShortFormat
impl From<Instant> for ShortFormat
Source§fn from(t: Instant) -> ShortFormat
fn from(t: Instant) -> ShortFormat
Source§impl From<Instant> for TimestampFormat
impl From<Instant> for TimestampFormat
Source§fn from(t: Instant) -> TimestampFormat
fn from(t: Instant) -> TimestampFormat
Converts a Unix Instant to a 32-bit NTP timestamp.
Note: This truncates to 32 bits, losing era information. The resulting
protocol::TimestampFormat is correct for NTPv4 on-wire use, but the era must
be inferred by the receiver using a pivot-based approach (see timestamp_to_instant).
Source§impl From<ShortFormat> for Instant
impl From<ShortFormat> for Instant
Source§fn from(t: ShortFormat) -> Instant
fn from(t: ShortFormat) -> Instant
Source§impl From<TimestampFormat> for Instant
Available on crate feature std only.
impl From<TimestampFormat> for Instant
std only.Source§fn from(t: TimestampFormat) -> Instant
fn from(t: TimestampFormat) -> Instant
Converts a 32-bit NTP timestamp to a Unix Instant, using the current system
time as a pivot for era disambiguation.
This is correct for live NTP usage where timestamps are close to “now”.
For offline or replay scenarios, use timestamp_to_instant with an explicit pivot.