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
impl SntpDateTime
Sourcepub fn unix_timestamp(&self) -> Result<Duration, ConversionError>
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?
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}
Sourcepub fn into_system_time(self) -> Result<SystemTime, ConversionError>
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.
Sourcepub fn into_chrono_datetime(self) -> Result<DateTime<Utc>, ConversionError>
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.
Sourcepub fn into_offset_date_time(self) -> Result<OffsetDateTime, ConversionError>
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
impl Clone for SntpDateTime
Source§fn clone(&self) -> SntpDateTime
fn clone(&self) -> SntpDateTime
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more