Struct rsntp::SntpDuration

source ·
pub struct SntpDuration(/* private fields */);
Expand description

Represents a signed duration value.

It’s main purpose is to store signed duration values which the std::time::Duration is not capable of, while making it possible to return a time-crate independent duration values (i.e. it works without chrono support enabled).

It can be converted to a different duration representation, depending on the enabled time crate support or it has some methods to inspect its value directly.

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

Implementations§

source§

impl SntpDuration

source

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

Returns with the absolute value of the duration

As std::time::Duration cannot store signed values, the returned duration will always be positive and will store the absolute value. This is a fallible conversion as there can be cases when duration contains a non-convertible number.

use rsntp::SntpClient;

let client = SntpClient::new();
let result = client.synchronize("pool.ntp.org").unwrap();
let clock_offset_abs = result.clock_offset().abs_as_std_duration().unwrap().as_secs_f64();
let clock_offset = clock_offset_abs * result.clock_offset().signum() as f64;

println!("Clock offset: {} seconds", clock_offset);
source

pub fn signum(&self) -> i32

Returns with the sign of the duration

Works similar way as signum methods for built-in types, returns with 1 if the duration is positive or with -1 if the duration is negative.

use rsntp::SntpClient;

let client = SntpClient::new();
let result = client.synchronize("pool.ntp.org").unwrap();
let clock_offset_abs = result.clock_offset().abs_as_std_duration().unwrap().as_secs_f64();
let clock_offset = clock_offset_abs * result.clock_offset().signum() as f64;

println!("Clock offset: {} seconds", clock_offset);
source

pub fn as_secs_f64(&self) -> f64

Returns with the number of seconds in this duration as a floating point number

The returned value will have a proper sign, i.e. it will be negative if the stored duration is negative.

use rsntp::SntpClient;

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

println!("Clock offset: {} seconds", result.clock_offset().as_secs_f64());
Examples found in repository?
examples/blocking_without_chrono.rs (line 7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
fn main() {
    let client = rsntp::SntpClient::new();
    let time_info = client.synchronize("pool.ntp.org").unwrap();

    println!(
        "Clock offset: {} ms",
        time_info.clock_offset().as_secs_f64() * 1000.0
    );
    println!(
        "Round trip delay: {} ms",
        time_info.round_trip_delay().as_secs_f64() * 1000.0
    );
    println!(
        "Server UTC UNIX timestamp: {}",
        time_info.datetime().unix_timestamp().unwrap().as_secs()
    );

    println!(
        "Reference identifier: {}",
        time_info.reference_identifier().to_string()
    );
    println!("Stratum: {}", time_info.stratum());
}
source

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

Convert instance to chrono::Duration

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

source

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

Convert instance to time::Duration

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

Trait Implementations§

source§

impl Clone for SntpDuration

source§

fn clone(&self) -> SntpDuration

Returns a copy 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 SntpDuration

source§

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

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

impl PartialEq for SntpDuration

source§

fn eq(&self, other: &SntpDuration) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for SntpDuration

source§

fn partial_cmp(&self, other: &SntpDuration) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl TryInto<Duration> for SntpDuration

§

type Error = ConversionError

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

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

Performs the conversion.
source§

impl TryInto<Duration> for SntpDuration

§

type Error = ConversionError

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

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

Performs the conversion.
source§

impl Copy for SntpDuration

source§

impl StructuralPartialEq for SntpDuration

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> 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,

§

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>,

§

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>,

§

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.