ntp-client

Crates.iontp-client
lib.rsntp-client
version0.1.0
created_at2025-04-22 08:22:55.128572+00
updated_at2025-04-22 08:22:55.128572+00
descriptionThe 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.
homepagehttps://gitee.com/eternalnight996
repository
max_upload_size
id1643714
size54,438
Eternal (EternalNight996)

documentation

https://docs.rs/ntp-client

README

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.
Commit count: 0

cargo fmt