[−][src]Crate rsntp
rsntp
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.
Usage
Add this to your Cargo.toml
:
[dependencies]
rsntp = "0.3.2"
Obtain the current local time with the blocking API:
use rsntp::SntpClient; use chrono::{DateTime, Local}; let client = SntpClient::new("pool.ntp.org").unwrap(); let result = client.synchronize().unwrap(); let local_time: DateTime<Local> = DateTime::from(result.datetime()); println!("Current time is: {}", local_time);
And a function which uses the asynchronous API to obtain local time:
use rsntp::AsyncSntpClient; use chrono::{DateTime, Local}; async fn local_time() -> DateTime<Local> { let client = AsyncSntpClient::new("pool.ntp.org"); let result = client.synchronize().await.unwrap(); DateTime::from(result.datetime()) }
Disabling asynchronous API
The asynchronous API is compiled in by default but you can optionally disable it. This removes
dependency to tokio
which reduces crate dependencies significantly.
[dependencies]
rsntp = { version = "0.3.2", default-features = false }
Structs
AsyncSntpClient | Asynchronous API client instance |
SntpClient | Blocking SNTP client instance |
SynchronizationResult | Results of a synchronization. |
Enums
KissCode | Kiss code, reason of a Kiss-o'-Death reply. |
LeapIndicator | Leap indicator |
ProtocolError | A detailed information about SNTP protocol related errors. |
ReferenceIdentifier | Identifies the particular reference source. |
SynchroniztationError | Synchronization error |