#time-synchronization #sync #ntp-client #system #time #ntp-synchronization

ntp-client

The ntp-client is a Rust library designed for interacting with NTP (Network Time Protocol) servers. It enables developers to easily obtain accurate time information from NTP servers. This can be applied in scenarios such as calibrating system time and performing time synchronization. The library offers a simple and user - friendly API, and also features good performance and stability.

1 unstable release

new 0.1.0 Apr 22, 2025

#137 in Windows APIs

Download history 85/week @ 2025-04-19

85 downloads per month
Used in hw

Custom license

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.

License

中文 | 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

LICENSE COPYRIGHT

🤝 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:

  1. The code complies with the project's specifications.
  2. Necessary tests are added.
  3. Relevant documentation is updated.

📜 Open Source License

This project is dual-licensed under MIT and LICENSE.


Built with ❤️ by eternalnight996 and contributors.

Dependencies

~1.8–3MB
~55K SLoC