Might handle leap seconds better

This commit is contained in:
Artemis Tosini 2023-12-12 20:40:27 +00:00
parent d0ce857365
commit ecf78a4264
Signed by: artemist
GPG key ID: EE5227935FE3FF18
4 changed files with 16 additions and 9 deletions

View file

@ -51,6 +51,7 @@
packages = [ packages = [
pkg-config pkg-config
dbus dbus
libfaketime
toolchain.cargo toolchain.cargo
toolchain.rustc toolchain.rustc
toolchain.rustfmt toolchain.rustfmt

View file

@ -106,9 +106,11 @@ pub async fn local_time_stream(config: LocalTimeConfig) {
..Default::default() ..Default::default()
}; };
} }
let utc_next = utc_now.trunc_subsecs(0) + chrono::Duration::seconds(1); // It would take more resources to get time again, and we want to err on the side of
let difference = utc_next - utc_now; // "wait a few µs too long" rather than too short.
// Use a mod here in case someone handles leap seconds this way
let difference = 1_000_000_000 - (utc_now.timestamp_subsec_nanos() % 1_000_000_000);
sleep(difference.to_std().unwrap()).await; sleep(std::time::Duration::from_nanos(difference.into())).await;
} }
} }

View file

@ -16,9 +16,11 @@ pub async fn system_time_stream(config: SystemTimeConfig) {
..Default::default() ..Default::default()
}); });
let next = now.trunc_subsecs(0) + chrono::Duration::seconds(1); // It would take more resources to get time again, and we want to err on the side of
let difference = next - now; // "wait a few µs too long" rather than too short.
// Use a mod here in case someone handles leap seconds this way
let difference = 1_000_000_000 - (now.timestamp_subsec_nanos() % 1_000_000_000);
sleep(difference.to_std().unwrap()).await; sleep(std::time::Duration::from_nanos(difference.into())).await;
} }
} }

View file

@ -16,9 +16,11 @@ pub async fn utc_time_stream(config: UtcTimeConfig) {
..Default::default() ..Default::default()
}); });
let next = now.trunc_subsecs(0) + chrono::Duration::seconds(1); // It would take more resources to get time again, and we want to err on the side of
let difference = next - now; // "wait a few µs too long" rather than too short.
// Use a mod here in case someone handles leap seconds this way
let difference = 1_000_000_000 - (now.timestamp_subsec_nanos() % 1_000_000_000);
sleep(difference.to_std().unwrap()).await; sleep(std::time::Duration::from_nanos(difference.into())).await;
} }
} }