Skip to main content

Instant

Struct Instant 

Source
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

Source

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!.

Source

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());
Source

pub fn secs(&self) -> i64

The “seconds” component of the Instant.

Source

pub fn subsec_nanos(&self) -> i32

The fractional component of the Instant in nanoseconds.

Trait Implementations§

Source§

impl Clone for Instant

Source§

fn clone(&self) -> Instant

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Instant

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl From<DateFormat> for Instant

Source§

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

Source§

fn from(t: Instant) -> DateFormat

Converts a Unix Instant to a 128-bit NTP date format with explicit era.

This conversion preserves era information and is unambiguous.

Source§

impl From<Instant> for ShortFormat

Source§

fn from(t: Instant) -> ShortFormat

Converts to this type from the input type.
Source§

impl From<Instant> for TimestampFormat

Source§

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

Source§

fn from(t: ShortFormat) -> Instant

Converts to this type from the input type.
Source§

impl From<TimestampFormat> for Instant

Available on crate feature std only.
Source§

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.

Source§

impl Copy for Instant

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.