An RFC 4330 compliant Simple Network Time Protocol (SNTP) client library for Rust.
rsntp
provides both a synchronous (blocking) and an (optional) asynchronous API which allows
synchronization with SNTPv4 servers. Time and date handling is based on the chrono
crate.
Add this to your Cargo.toml
:
toml
[dependencies]
rsntp = "1.0.0"
Obtain the current local time with the blocking API:
```no_run use rsntp::SntpClient; use chrono::{DateTime, Local};
let client = SntpClient::new(); let result = client.synchronize("pool.ntp.org").unwrap();
let local_time: DateTime
println!("Current time is: {}", local_time); ```
And a function which uses the asynchronous API to obtain local time:
```no_run use rsntp::AsyncSntpClient; use chrono::{DateTime, Local};
async fn local_time() -> DateTime
DateTime::from(result.datetime()) } ```
The asynchronous API is compiled in by default but you can optionally disable it. This removes
dependency to tokio
which reduces crate dependencies significantly.
toml
[dependencies]
rsntp = { version = "1.0.0", default-features = false }