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
impl SntpDuration
sourcepub fn abs_as_std_duration(&self) -> Result<Duration, ConversionError>
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);
sourcepub fn signum(&self) -> i32
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);
sourcepub fn as_secs_f64(&self) -> f64
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?
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());
}
sourcepub fn into_chrono_duration(self) -> Result<Duration, ConversionError>
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.
sourcepub fn into_time_duration(self) -> Result<Duration, ConversionError>
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
impl Clone for SntpDuration
source§fn clone(&self) -> SntpDuration
fn clone(&self) -> SntpDuration
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for SntpDuration
impl Debug for SntpDuration
source§impl PartialEq for SntpDuration
impl PartialEq for SntpDuration
source§fn eq(&self, other: &SntpDuration) -> bool
fn eq(&self, other: &SntpDuration) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd for SntpDuration
impl PartialOrd for SntpDuration
source§fn partial_cmp(&self, other: &SntpDuration) -> Option<Ordering>
fn partial_cmp(&self, other: &SntpDuration) -> Option<Ordering>
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 more