#sntp #ntp #timestamp #chrono #protocols #request #sntp-client

sntp_request

Tiny Rust library to request timestamp from NTP servers trough SNTP protocol

8 stable releases

2.0.1 Dec 21, 2022
2.0.0 Apr 13, 2020
1.1.1 Jan 7, 2020

#282 in Date and time

Download history 201/week @ 2024-07-21 406/week @ 2024-07-28 298/week @ 2024-08-04 142/week @ 2024-08-11 101/week @ 2024-08-18 164/week @ 2024-08-25 227/week @ 2024-09-01 53/week @ 2024-09-08 248/week @ 2024-09-15 162/week @ 2024-09-22 244/week @ 2024-09-29 601/week @ 2024-10-06 163/week @ 2024-10-13 417/week @ 2024-10-20 325/week @ 2024-10-27 326/week @ 2024-11-03

1,250 downloads per month

MIT/Apache

11KB
104 lines

sntp_request

CI/CD Crates.io Documentation License

sntp_request Tiny Rust library to request timestamps from NTP servers through SNTP protocol.

Example

The example below shows how to obtain precise timestamp from main NTP server:

use chrono::{Local, TimeZone};
use sntp_request::SntpRequest;

fn main() {
    let sntp = SntpRequest::new();
    let timestamp = Local.timestamp_opt(sntp.get_unix_time().unwrap(), 0);
    println!("{}", timestamp.unwrap());
}

Also, it is possible to get the raw timestamp, for example:

use sntp_request::SntpRequest;

fn main() {
    let sntp = SntpRequest::new();
    let timestamp = sntp.get_raw_time().unwrap();
    let nsec = (timestamp.frac as f64 / u32::max_value() as f64) * 1000.0;
    println!("seconds: {} frac: {}", timestamp.secs, timestamp.frac);
    println!("milliseconds: {}", nsec);
}

Contributions

Pull Requests are welcome! =)

License

sntp_request is licensed under the Apache License 2.0 or MIT License.

No runtime deps