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

source

pub fn new() -> SntpClient

Creates a new instance with default configuration

§Example
use rsntp::SntpClient;

let client = SntpClient::new();
Examples found in repository?
examples/blocking_without_chrono.rs (line 2)
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
Hide additional examples
examples/blocking_with_chrono.rs (line 6)
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());
}
source

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());
source

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?
examples/blocking_without_chrono.rs (line 3)
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
Hide additional examples
examples/blocking_with_chrono.rs (line 7)
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());
}
source

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));
source

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());
source

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

source§

fn clone(&self) -> SntpClient

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 SntpClient

source§

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

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

impl Default for SntpClient

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl Hash for SntpClient

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more

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.