Expand description
NTP client library with synchronous, async (tokio/smol), and NTS support.
§Example
Shows how to use the ntp_client library to fetch the current time according to the requested ntp server.
extern crate chrono;
extern crate ntp_client;
use chrono::TimeZone;
fn main() {
let address = "time.nist.gov:123";
let result = ntp_client::request(address).unwrap();
let unix_time = ntp_client::unix_time::Instant::from(result.transmit_timestamp);
let local_time = chrono::Local.timestamp_opt(unix_time.secs(), unix_time.subsec_nanos() as _).unwrap();
println!("{}", local_time);
println!("Offset: {:.6} seconds", result.offset_seconds);
}§Feature Flags
| Feature | Default | Description |
|---|---|---|
tokio | no | Async client and helpers using the tokio runtime. |
smol-runtime | no | Async client and helpers using the smol runtime. |
nts | no | Network Time Security (RFC 8915) via tokio + tokio-rustls. Implies tokio. |
nts-smol | no | NTS via smol + futures-rustls. Implies smol-runtime. Cannot be combined with nts (different TLS backends). |
pq-nts | no | Enable post-quantum key exchange for NTS (ML-KEM via aws-lc-rs). |
clock | no | System clock step/slew functions (libc/windows-sys). |
discipline | no | PLL/FLL clock discipline algorithm. Implies clock. |
symmetric | no | NTP symmetric active/passive mode (RFC 5905 modes 1 & 2). |
broadcast | no | NTP broadcast client (mode 5). Deprecated by RFC 8633. |
refclock | no | Reference clock abstraction layer. Implies tokio. |
gps | no | GPS reference clock driver. Implies refclock. |
pps | no | PPS reference clock driver. Implies refclock. |
hwts | no | Hardware timestamping support. Implies refclock. |
roughtime | no | Roughtime client (draft-ietf-ntp-roughtime). Implies tokio. |
socket-opts | no | DSCP and IPV6_V6ONLY socket options via socket2. |
ipv4 | no | Default to 0.0.0.0 instead of [::] for listen addresses. |
ntpv5 | no | NTPv5 draft support (draft-ietf-ntp-ntpv5). |
Modules§
- error
- Custom error types for the NTP client.
- extension
- NTP extension field parsing and NTS extension types. NTP extension field parsing and NTS (Network Time Security) extension types.
- protocol
- NTP protocol types and constants (RFC 5905). Types and constants that precisely match the specification.
- sntp
- Simple Network Time Protocol (SNTP) client per RFC 4330.
- unix_
time - Unix time conversion utilities for NTP timestamps.
Structs§
- Kiss
OfDeath Error - Error returned when the server responds with a Kiss-o’-Death (KoD) packet.
- NtpResult
- The result of an NTP request, containing the server’s response packet along with computed timing information.
Enums§
- Discipline
State - Clock discipline states per RFC 5905 Figure 24.
Functions§
- request
- Send a blocking request to an NTP server with a hardcoded 5 second timeout.
- request_
with_ timeout - Send a blocking request to an NTP server with a configurable timeout.