1 unstable release
new 0.1.0 | Apr 22, 2025 |
---|
#137 in Windows APIs
85 downloads per month
Used in hw
31KB
678 lines
ntp-client
ntp-client is a Rust library designed for interacting with NTP (Network Time Protocol) servers. It enables developers to conveniently obtain accurate time information from NTP servers, which can be used in scenarios such as calibrating system time and performing time synchronization. The library provides a simple and easy-to-use API, along with good performance and stability.
中文 | English
🚀 Quick Start
[dependencies]
ntp-client = { version="0.1", features=["sync-system"] }
Example: Get time from an NTP server and sync systemtime
fn main() -> e_utils::AnyResult<()> {
let target = "0.pool.ntp.org:123";
let res = ntp_client::Client::new()
.target(target)?
.format(Some("%Y/%m/%d %H:%M:%S"))
.request()?;
let res_str = res.get_datetime_str().ok_or("error")?;
println!("UTC str -> {res_str}");
let datetime = res.get_datetime_utc().ok_or("get datetime utc")?;
ntp_client::sync_systemtime(datetime)?;
Ok(())
}
Example: Get time from an NTP server and convert it to Shanghai time zone
use e_utils::chrono::FixedOffset;
fn main() -> e_utils::AnyResult<()> {
let target = "0.pool.ntp.org:123";
let res = ntp_client::Client::new()
.target(target)?
.format(Some("%Y/%m/%d %H:%M:%S"))
.offset(FixedOffset::east_opt(8 * 3600))
.request()?;
let res_str = res.get_datetime_str().ok_or("error")?;
println!("ShangHai str -> {res_str}");
Ok(())
}
License
🤝 Contributing
We welcome contributions in any form!
- Submit an Issue to report bugs or propose new features.
- Submit a Pull Request to improve the code.
- Enhance the project documentation.
- Share your usage experience.
Before submitting a PR, please ensure that:
- The code complies with the project's specifications.
- Necessary tests are added.
- Relevant documentation is updated.
📜 Open Source License
This project is dual-licensed under MIT and LICENSE.
Dependencies
~1.8–3MB
~55K SLoC