Struct SntpDateTime

Source
pub struct SntpDateTime { /* private fields */ }
Expand description

Represents a date and time

It’s main purpose is to have a wrapper for different date and time representations, which is usable regadless of the enabled time crate support.

It can be inspected directly, but there is no built-in timezone conversion, it will always return with UTC timestamps. If you need timezone support then you have to use chrono or time crate for conversion.

If chrono crate support is enabled then it will have TryInto<chrono::DateTime<Utc>> implemented. If time crate support is enabled then it will have TryInto<time::OffsetDateTime> implemented.

Implementations§

Source§

impl SntpDateTime

Source

pub fn unix_timestamp(&self) -> Result<Duration, ConversionError>

Returns with the duration since Unix epoch i.e. Unix timestamp

Then conversion can fail in cases like internal overflow or when the date is not representable with a Unix timestamp (like it is before Unix epoch).

Note that the function uses the actual system time during execution so assumes that it is monotonic. If the time has been changed between the actual synchronization and the call of this function, then it may return with undefined results.

use rsntp::SntpClient;

let client = SntpClient::new();
let result = client.synchronize("pool.ntp.org").unwrap();

let unix_timetamp_utc = result.datetime().unix_timestamp().unwrap();
Examples found in repository?
examples/blocking_without_chrono.rs (line 15)
1fn main() {
2    let client = rsntp::SntpClient::new();
3    let time_info = client.synchronize("pool.ntp.org").unwrap();
4
5    println!(
6        "Clock offset: {} ms",
7        time_info.clock_offset().as_secs_f64() * 1000.0
8    );
9    println!(
10        "Round trip delay: {} ms",
11        time_info.round_trip_delay().as_secs_f64() * 1000.0
12    );
13    println!(
14        "Server UTC UNIX timestamp: {}",
15        time_info.datetime().unix_timestamp().unwrap().as_secs()
16    );
17
18    println!(
19        "Reference identifier: {}",
20        time_info.reference_identifier().to_string()
21    );
22    println!("Stratum: {}", time_info.stratum());
23}
Source

pub fn into_system_time(self) -> Result<SystemTime, ConversionError>

Convert instance to std::time::SystemTime.

Convenience wrapper for TryInto<std::time::SystemTime>::try_into to avoid type annotations.

Source

pub fn into_chrono_datetime(self) -> Result<DateTime<Utc>, ConversionError>

Convert instance to chrono::DateTime<chrono::Utc>.

Convenience wrapper for TryInto<chrono::DateTime<chrono::Utc>>::try_into to avoid type annotations.

Source

pub fn into_offset_date_time(self) -> Result<OffsetDateTime, ConversionError>

Convert instance to time::OffsetDateTime.

Convenience wrapper for TryInto<time::OffsetDateTime>::try_into to avoid type annotations.

Trait Implementations§

Source§

impl Clone for SntpDateTime

Source§

fn clone(&self) -> SntpDateTime

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 SntpDateTime

Source§

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

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

impl TryInto<DateTime<Utc>> for SntpDateTime

Source§

type Error = ConversionError

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

fn try_into(self) -> Result<DateTime<Utc>, ConversionError>

Performs the conversion.
Source§

impl TryInto<OffsetDateTime> for SntpDateTime

Source§

type Error = ConversionError

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

fn try_into(self) -> Result<OffsetDateTime, ConversionError>

Performs the conversion.
Source§

impl TryInto<SystemTime> for SntpDateTime

Source§

type Error = ConversionError

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

fn try_into(self) -> Result<SystemTime, ConversionError>

Performs the conversion.
Source§

impl Copy for SntpDateTime

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.