Expand description
Rust SNTP client implementation
§Overview
This crate provides an async-first SNTP client for sending requests to NTP servers and processing responses to extract accurate timestamps.
Supported protocol version: SNTPv4 (RFC 4330)
§Quick Start
Add to your Cargo.toml:
[dependencies]
sntpc = "0.8"For common usage patterns, choose a network adapter:
sntpc-net-std- Standard library UDP socketssntpc-net-tokio- Tokio async runtimesntpc-net-embassy- Embassy embedded runtime
§Features
std- Standard library support (includesStdTimestampGen)sync- Synchronous API insyncmodule (default is async)utils- OS-specific utilities for system time sync ⚠️ Unstable APIlog- Debug logging vialogcratedefmt- Debug logging viadefmt(mutually exclusive withlog)
Warning: log and defmt are mutually exclusive features. If both are enabled,
defmt takes priority.
§Architecture
The library is designed to work in both std and no_std environments through two key traits:
NtpUdpSocket- Implement for your UDP socket typeNtpTimestampGenerator- Implement for your timestamp source
For std environments, StdTimestampGen is provided.
§API Approaches
get_time- Complete request/response in a single call (suitable for most cases)sntp_send_requestandsntp_process_response- Split send/receive workflow (useful when the TCP/IP stack requires polling or has custom timing requirements)
§Examples
See the examples directory for complete examples:
simple-request- Basic synchronous usagetokio- Async with tokio runtimeembassy-net- Embedded async with embassysmoltcp-request- Customno_stdnetworking- And more…
Refer to individual function documentation for minimal code examples
Modules§
- sync
- Synchronous interface for the SNTP client
Structs§
- NtpContext
- SNTP client context that contains of objects that may be required for client’s operation
- NtpResult
- SNTP request result representation
- Send
Request Result - Preserve SNTP request sending operation result required during receiving and processing state
- StdTimestamp
Gen - Standard library timestamp generator wrapper type
that relies on
std::timeto provide timestamps during SNTP client operations
Enums§
- Error
- The error type for SNTP client Errors originate on network layer or during processing response from a NTP server
Traits§
- NtpTimestamp
Generator - A trait encapsulating timestamp generator’s operations
- NtpUdp
Socket - A trait encapsulating UDP socket interface required for SNTP client operations
Functions§
- fraction_
to_ microseconds - Convert second fraction value to microseconds value
- fraction_
to_ milliseconds - Convert second fraction value to milliseconds value
- fraction_
to_ nanoseconds - Convert second fraction value to nanoseconds value
- fraction_
to_ picoseconds - Convert second fraction value to picoseconds value
- get_
time - Retrieves the current time from an NTP server.
- sntp_
process_ response - Processes the response from an NTP server.
- sntp_
send_ request - Sends an SNTP request to an NTP server.
Type Aliases§
- Result
- SNTP library result type