Skip to content

Commit

Permalink
Improved time jump conversion logic in statime_linux.
Browse files Browse the repository at this point in the history
  • Loading branch information
davidv1992 authored and folkertdev committed Aug 16, 2023
1 parent 94b5925 commit bc5dc49
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions statime-linux/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ edition = "2021"
arrayvec = { version = "0.7.4", default-features = false }
clap = { version = "4.3.21", features = ["derive"] }
fern = { version = "0.6.2", features = ["colored"] }
fixed = "1.23"
libc = { version = "0.2.147", features = ["extra_traits"] }
log = "0.4.20"
statime = { path = "../statime" }
Expand Down
13 changes: 9 additions & 4 deletions statime-linux/src/clock/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::path::Path;

use clock_steering::{unix::UnixClock, TimeOffset};
use fixed::traits::LossyInto;
use statime::{Clock, Duration, Time, TimePropertiesDS};

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -99,11 +100,15 @@ impl Clock for LinuxClock {
self.clock.set_leap_seconds(leap_indicator)?
}

// For nanos, we need the modulo operation, not remainder
// hence the workaround.
// Since we want nanos to be in [0,1_000_000_000), we need
// euclidean division and remainder.
let offset_nanos: i128 = time_offset.nanos().lossy_into();
let offset = TimeOffset {
seconds: (time_offset.nanos_lossy() / 1e9).floor() as _,
nanos: (((time_offset.nanos_lossy() % 1e9) + 1e9) % 1e9).floor() as _,
seconds: offset_nanos
.div_euclid(1_000_000_000)
.try_into()
.expect("Unexpected jump larger than 2^64 seconds"),
nanos: offset_nanos.rem_euclid(1_000_000_000) as _, // Result will always fit in u32
};

log::trace!(
Expand Down

0 comments on commit bc5dc49

Please sign in to comment.