Skip to main content

Crate ntp_client

Crate ntp_client 

Source
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

FeatureDefaultDescription
tokionoAsync client and helpers using the tokio runtime.
smol-runtimenoAsync client and helpers using the smol runtime.
ntsnoNetwork Time Security (RFC 8915) via tokio + tokio-rustls. Implies tokio.
nts-smolnoNTS via smol + futures-rustls. Implies smol-runtime. Cannot be combined with nts (different TLS backends).
pq-ntsnoEnable post-quantum key exchange for NTS (ML-KEM via aws-lc-rs).
clocknoSystem clock step/slew functions (libc/windows-sys).
disciplinenoPLL/FLL clock discipline algorithm. Implies clock.
symmetricnoNTP symmetric active/passive mode (RFC 5905 modes 1 & 2).
broadcastnoNTP broadcast client (mode 5). Deprecated by RFC 8633.
refclocknoReference clock abstraction layer. Implies tokio.
gpsnoGPS reference clock driver. Implies refclock.
ppsnoPPS reference clock driver. Implies refclock.
hwtsnoHardware timestamping support. Implies refclock.
roughtimenoRoughtime client (draft-ietf-ntp-roughtime). Implies tokio.
socket-optsnoDSCP and IPV6_V6ONLY socket options via socket2.
ipv4noDefault to 0.0.0.0 instead of [::] for listen addresses.
ntpv5noNTPv5 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§

KissOfDeathError
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§

DisciplineState
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.