15 releases

0.4.0 Oct 26, 2024
0.3.7 Jan 7, 2024
0.3.6 Jun 11, 2023
0.3.2 Nov 9, 2022
0.1.1 Feb 15, 2019

#26 in Date and time

Download history 2098/week @ 2024-08-17 2202/week @ 2024-08-24 2118/week @ 2024-08-31 2106/week @ 2024-09-07 2333/week @ 2024-09-14 2414/week @ 2024-09-21 2497/week @ 2024-09-28 2210/week @ 2024-10-05 2693/week @ 2024-10-12 3061/week @ 2024-10-19 3942/week @ 2024-10-26 3291/week @ 2024-11-02 2991/week @ 2024-11-09 4041/week @ 2024-11-16 4316/week @ 2024-11-23 3796/week @ 2024-11-30

15,708 downloads per month
Used in 4 crates

BSD-3-Clause

65KB
1K SLoC

sntpc test

Simple Rust SNTP client


This crate provides a method for sending requests to NTP servers and process responses, extracting received timestamp.

Supported SNTP protocol versions:

Documentation


https://docs.rs/sntpc

Installation


This crate works with Cargo and is on crates.io. Add it to your Cargo.toml like so:

[dependencies]
sntpc = "0.4.0"

By calling the get_time() method and providing a proper NTP pool or server you should get a valid synchronization timestamp:

use std::net::UdpSocket;
use std::time::Duration;

fn main() {
    let socket =
        UdpSocket::bind("0.0.0.0:0").expect("Unable to crate UDP socket");
    socket
        .set_read_timeout(Some(Duration::from_secs(2)))
        .expect("Unable to set UDP socket read timeout");
    let result = sntpc::simple_get_time("time.google.com:123", &socket);
    match result {
        Ok(time) => {
            println!("Got time: {}.{}", time.sec(), sntpc::fraction_to_milliseconds(time.sec_fraction()));
        }
        Err(err) => println!("Err: {:?}", err),
    }
}

no_std support


Currently, there are basic no_std support available, thanks to no-std-net crate. There is an example available on how to use smoltcp stack and that should provide general idea on how to bootstrap no_std networking and timestamping tools for sntpc library usage

async support


Feature async_tokio allows to use crate together with tokio. Example available in the repository.

There is also no_std support with feature async, but it requires Rust >= 1.75-nightly version. The example can be found in separate repository.

Examples


You can find several examples that shows how to use the library in details under examples/ folder. Currently, there are examples that show:

  • usage of SNTP library in std environment
  • usage of SNTP library with smoltcp TCP/IP stack. Some std dependencies required only due to smoltcp available interfaces

Contribution


Contributions are always welcome! If you have an idea, it's best to float it by me before working on it to ensure no effort is wasted. If there's already an open issue for it, knock yourself out. See the contributing section for additional details

Thanks

  1. Frank A. Stevenson: for implementing stricter adherence to RFC4330 verification scheme
  2. Timothy Mertz: for fixing possible overflow in offset calculation
  3. HannesH: for fixing a typo in the README.md
  4. Richard Penney: for adding two indicators of the NTP server's accuracy into the NtpResult structure
  5. Vitali Pikulik: for adding async support
  6. tsingwong: for fixing invalid link in the README.md
  7. Robert Bastian: for fixing the overflow issue in the calculate_offset

Really appreciate all your efforts! Please let me know if I forgot someone.

License


This project is licensed under The 3-Clause BSD License
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in time by you, as defined in the 3-Clause BSD license, shall be licensed as above, without any additional terms or conditions.

Dependencies

~0.2–6.5MB
~35K SLoC