Skip to content

Commit

Permalink
sntpc: Add helper methods for converting second fractional field
Browse files Browse the repository at this point in the history
  • Loading branch information
vpetrigo committed Jun 11, 2023
1 parent ccd336c commit 327ac30
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ like so:

```toml
[dependencies]
sntpc = "0.3.5"
sntpc = "0.3.6"
```

By calling the `get_time()` method and providing a proper NTP pool or server you
Expand All @@ -50,7 +50,7 @@ fn main() {

match result {
Ok(time) => {
let microseconds = time.sec_fraction() as u64 * 1_000_000u64 / u32::MAX as u64;
let microseconds = sntpc::fraction_to_microseconds(time.sec_fraction());
println!("Got time: {}.{}", time.sec(), microseconds);
}
Err(err) => println!("Err: {:?}", err),
Expand Down
90 changes: 82 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
//! Put this in your `Cargo.toml`:
//! ```cargo
//! [dependencies]
//! sntpc = "0.3.5"
//! sntpc = "0.3.6"
//! ```
//!
//! ## Features
Expand Down Expand Up @@ -54,7 +54,6 @@
//! ```rust
//! # #[cfg(not(feature = "std"))]
//! # use no_std_net::{SocketAddr, ToSocketAddrs, IpAddr, Ipv4Addr};
//! # use sntpc::Result;
//! # #[cfg(feature = "std")]
//! use std::net::UdpSocket;
//! use std::time::Duration;
Expand All @@ -64,16 +63,16 @@
//! # struct UdpSocket;
//! # #[cfg(not(feature = "std"))]
//! # impl UdpSocket {
//! # fn bind(addr: &str) -> Result<Self> {
//! # fn bind(addr: &str) -> sntpc::Result<Self> {
//! # Ok(UdpSocket)
//! # }
//! # fn send_to<T: ToSocketAddrs>(&self, buf: &[u8], dest: T) -> Result<usize> {
//! # fn send_to<T: ToSocketAddrs>(&self, buf: &[u8], dest: T) -> sntpc::Result<usize> {
//! # Ok(0usize)
//! # }
//! # fn recv_from(&self, buf: &mut [u8]) -> Result<(usize, SocketAddr)> {
//! # fn recv_from(&self, buf: &mut [u8]) -> sntpc::Result<(usize, SocketAddr)> {
//! # Ok((0usize, SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 0)))
//! # }
//! # fn set_read_timeout<T>(&self, _arg: T) -> Result<()> {
//! # fn set_read_timeout<T>(&self, _arg: T) -> sntpc::Result<()> {
//! # Ok(())
//! # }
//! # }
Expand All @@ -89,7 +88,7 @@
//! # #[cfg(all(feature = "std", feature = "sup"))]
//! match result {
//! Ok(time) => {
//! println!("Got time: {}.{}", time.sec(), time.sec_fraction());
//! println!("Got time: {}.{}", time.sec(), sntpc::fraction_to_milliseconds(time.sec_fraction()));
//! }
//! Err(err) => println!("Err: {:?}", err),
//! }
Expand Down Expand Up @@ -685,9 +684,36 @@ fn get_ntp_timestamp<T: NtpTimestampGenerator>(timestamp_gen: T) -> u64 {
/ USEC_IN_SEC as u64
}

/// Convert second fraction value to milliseconds value
pub fn fraction_to_milliseconds(sec_fraction: u32) -> u32 {
(u64::from(sec_fraction) * u64::from(MSEC_IN_SEC) / u64::from(u32::MAX))
as u32
}

/// Convert second fraction value to microseconds value
pub fn fraction_to_microseconds(sec_fraction: u32) -> u32 {
(u64::from(sec_fraction) * u64::from(USEC_IN_SEC) / u64::from(u32::MAX))
as u32
}

/// Convert second fraction value to nanoseconds value
pub fn fraction_to_nanoseconds(sec_fraction: u32) -> u32 {
(u64::from(sec_fraction) * u64::from(NSEC_IN_SEC) / u64::from(u32::MAX))
as u32
}

/// Convert second fraction value to picoseconds value
pub fn fraction_to_picoseconds(sec_fraction: u32) -> u64 {
(u128::from(sec_fraction) * u128::from(PSEC_IN_SEC) / u128::from(u32::MAX))
as u64
}

#[cfg(test)]
mod sntpc_ntp_result_tests {
use crate::NtpResult;
use crate::{
fraction_to_microseconds, fraction_to_milliseconds,
fraction_to_nanoseconds, fraction_to_picoseconds, NtpResult,
};

#[test]
fn test_ntp_result() {
Expand Down Expand Up @@ -733,6 +759,54 @@ mod sntpc_ntp_result_tests {
assert_eq!(0, result.roundtrip());
assert_eq!(0, result.offset());
}

#[test]
fn test_conversion_to_ms() {
let result = NtpResult::new(0, u32::MAX - 1, 0, 0, 1, 0);
let milliseconds = fraction_to_milliseconds(result.seconds_fraction);
assert_eq!(999u32, milliseconds);

let result = NtpResult::new(0, 0, 0, 0, 1, 0);
let milliseconds = fraction_to_milliseconds(result.seconds_fraction);
assert_eq!(0u32, milliseconds);
}

#[test]
fn test_conversion_to_us() {
let result = NtpResult::new(0, u32::MAX - 1, 0, 0, 1, 0);
let microseconds = fraction_to_microseconds(result.seconds_fraction);
assert_eq!(999999u32, microseconds);

let result = NtpResult::new(0, 0, 0, 0, 1, 0);
let microseconds = fraction_to_microseconds(result.seconds_fraction);
assert_eq!(0u32, microseconds);
}

#[test]
fn test_conversion_to_ns() {
let result = NtpResult::new(0, u32::MAX - 1, 0, 0, 1, 0);
let nanoseconds = fraction_to_nanoseconds(result.seconds_fraction);
assert_eq!(999999999u32, nanoseconds);

let result = NtpResult::new(0, 0, 0, 0, 1, 0);
let nanoseconds = fraction_to_nanoseconds(result.seconds_fraction);
assert_eq!(0u32, nanoseconds);
}

#[test]
fn test_conversion_to_ps() {
let result = NtpResult::new(0, u32::MAX - 1, 0, 0, 1, 0);
let picoseconds = fraction_to_picoseconds(result.seconds_fraction);
assert_eq!(999999999767u64, picoseconds);

let result = NtpResult::new(0, 1, 0, 0, 1, 0);
let picoseconds = fraction_to_picoseconds(result.seconds_fraction);
assert_eq!(232u64, picoseconds);

let result = NtpResult::new(0, 0, 0, 0, 1, 0);
let picoseconds = fraction_to_picoseconds(result.seconds_fraction);
assert_eq!(0u64, picoseconds);
}
}

#[cfg(all(test, feature = "std"))]
Expand Down
3 changes: 2 additions & 1 deletion src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ pub(crate) const VERSION_SHIFT: u8 = 3;
pub(crate) const LI_MASK: u8 = 0b1100_0000;
/// SNTP LI bit mask shift value
pub(crate) const LI_SHIFT: u8 = 6;
/// SNTP picoseconds in second constant
pub(crate) const PSEC_IN_SEC: u64 = 1_000_000_000_000;
/// SNTP nanoseconds in second constant
#[allow(dead_code)]
pub(crate) const NSEC_IN_SEC: u32 = 1_000_000_000;
/// SNTP microseconds in second constant
pub(crate) const USEC_IN_SEC: u32 = 1_000_000;
Expand Down

0 comments on commit 327ac30

Please sign in to comment.