Crates.io | sntpc |
lib.rs | sntpc |
version | 0.4.0 |
source | src |
created_at | 2019-02-15 22:14:53.524274 |
updated_at | 2024-10-26 03:32:45.686349 |
description | Library for making SNTP requests |
homepage | https://github.com/vpetrigo/sntpc |
repository | https://github.com/vpetrigo/sntpc |
max_upload_size | |
id | 115042 |
size | 65,775 |
This crate provides a method for sending requests to NTP servers and process responses, extracting received timestamp.
Supported SNTP protocol versions:
This crate works with Cargo and is on
crates.io. Add it to your Cargo.toml
like so:
[dependencies]
sntpc = "0.4.0"
By calling the get_time()
method and providing a proper NTP pool or server you
should get a valid synchronization timestamp:
use std::net::UdpSocket;
use std::time::Duration;
fn main() {
let socket =
UdpSocket::bind("0.0.0.0:0").expect("Unable to crate UDP socket");
socket
.set_read_timeout(Some(Duration::from_secs(2)))
.expect("Unable to set UDP socket read timeout");
let result = sntpc::simple_get_time("time.google.com:123", &socket);
match result {
Ok(time) => {
println!("Got time: {}.{}", time.sec(), sntpc::fraction_to_milliseconds(time.sec_fraction()));
}
Err(err) => println!("Err: {:?}", err),
}
}
no_std
supportCurrently, there are basic no_std
support available, thanks to no-std-net
crate. There is an example available on how to use smoltcp
stack and that should provide
general idea on how to bootstrap no_std
networking and timestamping tools for sntpc
library usage
async
supportFeature async_tokio
allows to use crate together with tokio.
Example available in the repository.
There is also no_std
support with feature async
, but it requires Rust >= 1.75-nightly
version.
The example can be found in separate repository.
You can find several examples that shows how to use the library in details under [examples/] folder. Currently, there are examples that show:
std
environmentsmoltcp
TCP/IP stack. Some std
dependencies
required only due to smoltcp available interfacesContributions are always welcome! If you have an idea, it's best to float it by me before working on it to ensure no effort is wasted. If there's already an open issue for it, knock yourself out. See the contributing section for additional details
NtpResult
structureasync
supportREADME.md
calculate_offset
Really appreciate all your efforts! Please let me know if I forgot someone.