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

sntp_request

一个用于通过SNTP协议从NTP服务器请求时间戳的轻量级Rust库

8个稳定版本

2.0.1 2022年12月21日
2.0.0 2020年4月13日
1.1.1 2020年1月7日

日期和时间类别中排名第545

Download history 52/week @ 2024-03-13 4/week @ 2024-03-20 5/week @ 2024-03-27 8/week @ 2024-04-03 2/week @ 2024-04-17 185/week @ 2024-04-24 459/week @ 2024-05-01 378/week @ 2024-05-08 282/week @ 2024-05-15 287/week @ 2024-05-22 271/week @ 2024-05-29 78/week @ 2024-06-05 90/week @ 2024-06-12 20/week @ 2024-06-19 54/week @ 2024-06-26

每月下载256

MIT/Apache

11KB
104 代码行

sntp_request

CI/CD Crates.io Documentation License

sntp_request是一个轻量级的Rust库,用于通过NTP服务器SNTP协议请求时间戳。

示例

以下示例展示了如何从主NTP服务器获取精确的时间戳

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());
}

此外,还可以获取原始时间戳,例如

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);
}

贡献

欢迎提交拉取请求! =)

许可证

sntp_request根据Apache License 2.0MIT许可证授权。

无运行时依赖