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 = [
pkg-config
dbus
libfaketime
toolchain.cargo
toolchain.rustc
toolchain.rustfmt

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}