From ecf78a4264b7c46a1624f0a0e69e1864a7e0c0bb Mon Sep 17 00:00:00 2001 From: Artemis Tosini Date: Tue, 12 Dec 2023 20:40:27 +0000 Subject: [PATCH] Might handle leap seconds better --- flake.nix | 1 + src/tiles/local_time.rs | 8 +++++--- src/tiles/system_time.rs | 8 +++++--- src/tiles/utc_time.rs | 8 +++++--- 4 files changed, 16 insertions(+), 9 deletions(-) diff --git a/flake.nix b/flake.nix index e3e8fb6..2f32738 100644 --- a/flake.nix +++ b/flake.nix @@ -51,6 +51,7 @@ packages = [ pkg-config dbus + libfaketime toolchain.cargo toolchain.rustc toolchain.rustfmt diff --git a/src/tiles/local_time.rs b/src/tiles/local_time.rs index 64b8e3f..45d8333 100644 --- a/src/tiles/local_time.rs +++ b/src/tiles/local_time.rs @@ -106,9 +106,11 @@ pub async fn local_time_stream(config: LocalTimeConfig) { ..Default::default() }; } - let utc_next = utc_now.trunc_subsecs(0) + chrono::Duration::seconds(1); - let difference = utc_next - utc_now; + // It would take more resources to get time again, and we want to err on the side of + // "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; } } diff --git a/src/tiles/system_time.rs b/src/tiles/system_time.rs index d4de7fc..5ec89f5 100644 --- a/src/tiles/system_time.rs +++ b/src/tiles/system_time.rs @@ -16,9 +16,11 @@ pub async fn system_time_stream(config: SystemTimeConfig) { ..Default::default() }); - let next = now.trunc_subsecs(0) + chrono::Duration::seconds(1); - let difference = next - now; + // It would take more resources to get time again, and we want to err on the side of + // "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; } } diff --git a/src/tiles/utc_time.rs b/src/tiles/utc_time.rs index 5e1e619..70a1456 100644 --- a/src/tiles/utc_time.rs +++ b/src/tiles/utc_time.rs @@ -16,9 +16,11 @@ pub async fn utc_time_stream(config: UtcTimeConfig) { ..Default::default() }); - let next = now.trunc_subsecs(0) + chrono::Duration::seconds(1); - let difference = next - now; + // It would take more resources to get time again, and we want to err on the side of + // "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; } }