Struct rsntp::SntpClient
source · pub struct SntpClient { /* private fields */ }
Expand description
Blocking client instance
This is the main entry point of the blocking API.
Implementations§
source§impl SntpClient
impl SntpClient
sourcepub fn new() -> SntpClient
pub fn new() -> SntpClient
Creates a new instance with default configuration
§Example
use rsntp::SntpClient;
let client = SntpClient::new();
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 with_config(config: Config) -> SntpClient
pub fn with_config(config: Config) -> SntpClient
Creates a new instance with the specified configuration
§Example
use rsntp::{Config, SntpClient};
let client = SntpClient::with_config(Config::default());
sourcepub fn synchronize<A: ToServerAddrs>(
&self,
server_address: A
) -> Result<SynchronizationResult, SynchronizationError>
pub fn synchronize<A: ToServerAddrs>( &self, server_address: A ) -> Result<SynchronizationResult, SynchronizationError>
Synchronize with the server
Sends a request to the server, waits for the reply, and processes it. This is a blocking call and can block for a long time. After sending the request, it waits for a timeout; if no reply is received, an error is returned.
If the supplied server address resolves to multiple addresses, only the first one is used.
§Example
use rsntp::SntpClient;
let client = SntpClient::new();
let result = client.synchronize("pool.ntp.org");
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 set_timeout(&mut self, timeout: Duration)
pub fn set_timeout(&mut self, timeout: Duration)
Sets synchronization timeout
Sets the time the client waits for a reply after the request has been sent. Default is 3 seconds.
§Example
use rsntp::SntpClient;
use std::time::Duration;
let mut client = SntpClient::new();
client.set_timeout(Duration::from_secs(10));
sourcepub fn set_bind_address(&mut self, address: SocketAddr)
pub fn set_bind_address(&mut self, address: SocketAddr)
Set UDP bind address
Sets the local address which is used to send/receive UDP packets. By default, it is “0.0.0.0:0” which means that IPv4 address and port are chosen automatically.
To synchronize with IPv6 servers, you might need to set it to an IPv6 address.
§Example
use rsntp::SntpClient;
let mut client = SntpClient::new();
client.set_bind_address("192.168.0.1:0".parse().unwrap());
sourcepub fn set_config(&mut self, config: Config)
pub fn set_config(&mut self, config: Config)
Set the configuration
§Example
use rsntp::{Config, SntpClient};
let client = SntpClient::new();
let config = Config::default().bind_address("192.168.0.1:0".parse().unwrap());
Trait Implementations§
source§impl Clone for SntpClient
impl Clone for SntpClient
source§fn clone(&self) -> SntpClient
fn clone(&self) -> SntpClient
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more