Struct rsntp::SynchronizationResult
source · pub struct SynchronizationResult { /* private fields */ }
Expand description
Results of a synchronization.
If you just simply need a fairly accurate SNTP time then check the datetime()
method. Other methods
provide more detailed information about the outcome of the synchronization and might need deeper
knwoledge about SNTP protocol internals.
Implementations§
source§impl SynchronizationResult
impl SynchronizationResult
sourcepub fn clock_offset(&self) -> SntpDuration
pub fn clock_offset(&self) -> SntpDuration
Returns with the offset between server and local clock.
It is a signed duration, negative value means the local clock is ahead.
§Example
Print the synchronized local time using clock offset:
use rsntp::SntpClient;
let client = SntpClient::new();
let result = client.synchronize("pool.ntp.org").unwrap();
println!("Clock offset: {}", 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());
}
More examples
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
fn chrono_example() {
let client = rsntp::SntpClient::new();
let time_info = client.synchronize("pool.ntp.org").unwrap();
let clock_offset: Duration = time_info.clock_offset().try_into().unwrap();
println!("Clock offset: {} ms", clock_offset);
let round_trip_delay: Duration = time_info.clock_offset().try_into().unwrap();
println!("Round trip delay: {} ms", round_trip_delay);
let datetime_utc: DateTime<Utc> = time_info.datetime().try_into().unwrap();
let local_time: DateTime<Local> = DateTime::from(datetime_utc);
println!("Local time: {}", local_time);
println!(
"Reference identifier: {}",
time_info.reference_identifier().to_string()
);
println!("Stratum: {}", time_info.stratum());
}
sourcepub fn round_trip_delay(&self) -> SntpDuration
pub fn round_trip_delay(&self) -> SntpDuration
Returns with the round trip delay
The time is needed for SNTP packets to travel back and forth between the host and the server. It is a signed value but negative values should not be possible in client mode (which is currently always used by the library).
§Example
use rsntp::SntpClient;
let client = SntpClient::new();
let result = client.synchronize("pool.ntp.org").unwrap();
println!("RTT: {} ms", result.round_trip_delay().as_secs_f64() * 1000.0);
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 reference_identifier(&self) -> &ReferenceIdentifier
pub fn reference_identifier(&self) -> &ReferenceIdentifier
Returns with the server reference identifier.
This identifies the synchronizaion source of the server. For primary servers (startum = 1) this is a four byte ASCII string, for secondary IPv4 servers (startum >= 2) this is an IP address, for secondary IPv6 servers this contains first 32 bits of an MD5 hash of an IPv6 address.
§Example
use rsntp::SntpClient;
let client = SntpClient::new();
let result = client.synchronize("pool.ntp.org").unwrap();
println!("Server reference identifier: {}", result.reference_identifier());
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());
}
More examples
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
fn chrono_example() {
let client = rsntp::SntpClient::new();
let time_info = client.synchronize("pool.ntp.org").unwrap();
let clock_offset: Duration = time_info.clock_offset().try_into().unwrap();
println!("Clock offset: {} ms", clock_offset);
let round_trip_delay: Duration = time_info.clock_offset().try_into().unwrap();
println!("Round trip delay: {} ms", round_trip_delay);
let datetime_utc: DateTime<Utc> = time_info.datetime().try_into().unwrap();
let local_time: DateTime<Local> = DateTime::from(datetime_utc);
println!("Local time: {}", local_time);
println!(
"Reference identifier: {}",
time_info.reference_identifier().to_string()
);
println!("Stratum: {}", time_info.stratum());
}
sourcepub fn datetime(&self) -> SntpDateTime
pub fn datetime(&self) -> SntpDateTime
Returns with the current UTC date and time, based on the synchronized SNTP timestamp.
This is the current UTC date and time, calculated by adding clock offset the UTC time. To be accurate, use the returned value immediately.
§Example
Calcuating synchronized local time:
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?
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());
}
More examples
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
fn chrono_example() {
let client = rsntp::SntpClient::new();
let time_info = client.synchronize("pool.ntp.org").unwrap();
let clock_offset: Duration = time_info.clock_offset().try_into().unwrap();
println!("Clock offset: {} ms", clock_offset);
let round_trip_delay: Duration = time_info.clock_offset().try_into().unwrap();
println!("Round trip delay: {} ms", round_trip_delay);
let datetime_utc: DateTime<Utc> = time_info.datetime().try_into().unwrap();
let local_time: DateTime<Local> = DateTime::from(datetime_utc);
println!("Local time: {}", local_time);
println!(
"Reference identifier: {}",
time_info.reference_identifier().to_string()
);
println!("Stratum: {}", time_info.stratum());
}
sourcepub fn leap_indicator(&self) -> LeapIndicator
pub fn leap_indicator(&self) -> LeapIndicator
Returns with the leap indicator
This is the leap indicator returned by the server. It is a warning of an impending leap second to be inserted/deleted in the last minute of the current day.
It is set before 23:59 on the day of insertion and reset after 00:00 on the following day. This causes the number of seconds (rollover interval) in the day of insertion to be increased or decreased by one.
§Example
Printing leap indicator:
use rsntp::SntpClient;
let client = SntpClient::new();
let result = client.synchronize("pool.ntp.org").unwrap();
println!("Leap indicator: {:?}", result.leap_indicator());
sourcepub fn stratum(&self) -> u8
pub fn stratum(&self) -> u8
Returns with the server stratum
NTP uses a hierarchical, semi-layered system of time sources. Each level of this hierarchy is termed a stratum and is assigned a number starting with zero for the reference clock at the top. A server synchronized to a stratum n server runs at stratum n + 1
Values defined as:
- 1 - Primary reference (e.g., calibrated atomic clock, radio clock, etc…)
- 2..15 - Secondary reference (via NTP, calculated as the stratum of system peer plus one)
- 16 - Unsynchronized
- 16..255 - Reserved
§Example
use rsntp::SntpClient;
let client = SntpClient::new();
let result = client.synchronize("pool.ntp.org").unwrap();
assert!(result.stratum() >= 1);
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());
}
More examples
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
fn chrono_example() {
let client = rsntp::SntpClient::new();
let time_info = client.synchronize("pool.ntp.org").unwrap();
let clock_offset: Duration = time_info.clock_offset().try_into().unwrap();
println!("Clock offset: {} ms", clock_offset);
let round_trip_delay: Duration = time_info.clock_offset().try_into().unwrap();
println!("Round trip delay: {} ms", round_trip_delay);
let datetime_utc: DateTime<Utc> = time_info.datetime().try_into().unwrap();
let local_time: DateTime<Local> = DateTime::from(datetime_utc);
println!("Local time: {}", local_time);
println!(
"Reference identifier: {}",
time_info.reference_identifier().to_string()
);
println!("Stratum: {}", time_info.stratum());
}
Trait Implementations§
source§impl Clone for SynchronizationResult
impl Clone for SynchronizationResult
source§fn clone(&self) -> SynchronizationResult
fn clone(&self) -> SynchronizationResult
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more