use std::sync::OnceLock;
use std::sync::atomic::{AtomicU64, Ordering};
use time::{Duration, Instant, SystemTime, UNIX_EPOCH};
#[cfg(feature = "local")]
pub mod local {
use jiff::civil::{Date, DateTime};
use jiff::{Timestamp, Zoned};
#[derive(Clone, Debug)]
pub struct Zone(platform::Zone);
impl Zone {
#[must_use]
pub fn here() -> Self {
if let Some(zone) = told::installed() {
return zone;
}
Self(platform::Zone::here())
}
#[must_use]
pub fn named(name: &str) -> Option<Self> {
if !crate::is_zone_name(name) {
return None;
}
#[cfg(all(target_family = "wasm", not(tilo_no_js)))]
{
platform::Zone::named(name).map(Self)
}
#[cfg(any(not(target_family = "wasm"), tilo_no_js))]
{
jiff::tz::TimeZone::get(name)
.ok()
.map(|zone| Self(platform::Zone::of(zone)))
}
}
#[cfg(not(target_family = "wasm"))]
#[must_use]
pub fn from_tzif(name: &str, data: &[u8]) -> Option<Self> {
jiff::tz::TimeZone::tzif(name, data).ok().map(Self::of)
}
#[cfg(not(target_family = "wasm"))]
#[must_use]
pub fn of(zone: jiff::tz::TimeZone) -> Self {
Self(platform::Zone::of(zone))
}
#[must_use]
pub fn at(&self, at: Timestamp) -> Zoned {
self.0.at(at)
}
#[must_use]
pub fn name(&self) -> Option<String> {
self.0.name()
}
#[must_use]
pub fn instant_of(&self, local: DateTime) -> Option<Timestamp> {
self.0.instant_of(local)
}
}
pub use told::install;
#[cfg(not(target_family = "wasm"))]
pub use zone_data::tzif;
#[must_use]
pub fn zone_names() -> Vec<String> {
#[cfg(all(target_family = "wasm", not(tilo_no_js)))]
let mut names = crate::page::zone_names();
#[cfg(any(not(target_family = "wasm"), tilo_no_js))]
let mut names: Vec<String> = jiff::tz::db()
.available()
.map(|name| name.as_str().to_owned())
.filter(|name| crate::is_zone_name(name))
.collect();
names.sort();
names.dedup();
names
}
mod told {
use std::cell::RefCell;
use super::Zone;
thread_local! {
static INSTALLED: RefCell<Option<Zone>> = const { RefCell::new(None) };
}
pub(super) fn installed() -> Option<Zone> {
INSTALLED.with(|installed| installed.borrow().clone())
}
pub fn install(zone: Option<Zone>) {
INSTALLED.with(|installed| *installed.borrow_mut() = zone);
}
}
#[cfg(not(target_family = "wasm"))]
mod zone_data {
use std::collections::HashMap;
use std::path::PathBuf;
use std::sync::{Arc, Mutex, PoisonError};
use super::Zone;
type Read = HashMap<String, Option<Arc<[u8]>>>;
static READ: Mutex<Option<Read>> = Mutex::new(None);
const MAX_REMEMBERED: usize = 1_024;
#[must_use]
pub fn tzif(name: &str) -> Option<Arc<[u8]>> {
if !crate::is_zone_name(name) {
return None;
}
let known = READ
.lock()
.unwrap_or_else(PoisonError::into_inner)
.as_ref()
.and_then(|read| read.get(name).cloned());
if let Some(known) = known {
return known;
}
let found = databases()
.iter()
.find_map(|database| std::fs::read(database.join(name)).ok())
.filter(|data| Zone::from_tzif(name, data).is_some())
.map(Arc::<[u8]>::from);
let mut remembered = READ.lock().unwrap_or_else(PoisonError::into_inner);
let read = remembered.get_or_insert_with(HashMap::new);
if read.len() >= MAX_REMEMBERED {
read.clear();
}
read.insert(name.to_owned(), found.clone());
drop(remembered);
found
}
fn databases() -> Vec<PathBuf> {
let mut databases: Vec<PathBuf> = std::env::var_os("TZDIR")
.map(PathBuf::from)
.filter(|dir| dir.is_absolute())
.into_iter()
.collect();
databases.extend(
[
"/usr/share/zoneinfo",
"/usr/share/lib/zoneinfo",
"/etc/zoneinfo",
]
.into_iter()
.map(PathBuf::from),
);
databases
}
#[cfg(test)]
mod tests {
use super::tzif;
use crate::is_zone_name;
#[test]
fn only_a_zones_name_is_read() {
assert!(is_zone_name("Pacific/Auckland"));
assert!(is_zone_name("America/Argentina/Buenos_Aires"));
assert!(is_zone_name("Etc/GMT+12"));
assert!(is_zone_name("UTC"));
assert!(!is_zone_name(""));
assert!(!is_zone_name("/etc/passwd"));
assert!(!is_zone_name("../../etc/passwd"));
assert!(!is_zone_name("Europe/../../x"));
assert!(!is_zone_name("Europe//Vienna"));
assert!(!is_zone_name("Europe/Vi enna"));
assert!(tzif("../../etc/passwd").is_none());
}
#[test]
fn a_zone_read_from_its_data_is_the_databases() {
let Some(data) = tzif("Pacific/Auckland") else {
return;
};
let zone = super::Zone::from_tzif("Pacific/Auckland", &data).unwrap();
let at = jiff::Timestamp::from_second(1_773_583_500).unwrap();
assert_eq!(zone.name().as_deref(), Some("Pacific/Auckland"));
assert_eq!(
zone.at(at).offset().seconds(),
13 * 3600,
"summer in Auckland"
);
}
}
}
#[must_use]
pub fn now_instant() -> Timestamp {
Timestamp::try_from(crate::to_std(crate::wall())).unwrap_or(Timestamp::UNIX_EPOCH)
}
#[must_use]
pub fn now() -> Zoned {
at(now_instant())
}
#[must_use]
pub fn at(at: Timestamp) -> Zoned {
Zone::here().at(at)
}
#[must_use]
pub fn today() -> Date {
now().date()
}
#[must_use]
pub fn instant_of(local: DateTime) -> Option<Timestamp> {
Zone::here().instant_of(local)
}
#[cfg(any(test, all(target_family = "wasm", not(tilo_no_js))))]
fn resolve(
local: DateTime,
offset_at: impl Fn(Timestamp) -> jiff::tz::Offset,
) -> Option<Timestamp> {
const A_DAY: jiff::SignedDuration = jiff::SignedDuration::from_hours(26);
let bare = local.to_zoned(jiff::tz::TimeZone::UTC).ok()?.timestamp();
let around = [
bare.checked_sub(A_DAY).ok()?,
bare,
bare.checked_add(A_DAY).ok()?,
];
let instants = around.map(|instant| {
let offset = offset_at(instant);
bare.checked_sub(jiff::SignedDuration::from_secs(i64::from(offset.seconds())))
.ok()
});
instants
.into_iter()
.flatten()
.filter(|at| read(*at, &offset_at).datetime() == local)
.min()
}
#[cfg(any(test, all(target_family = "wasm", not(tilo_no_js))))]
fn read(at: Timestamp, offset_at: impl Fn(Timestamp) -> jiff::tz::Offset) -> Zoned {
at.to_zoned(jiff::tz::TimeZone::fixed(offset_at(at)))
}
#[cfg(any(not(target_family = "wasm"), tilo_no_js))]
mod platform {
use jiff::civil::DateTime;
use jiff::tz::{AmbiguousOffset, TimeZone};
use jiff::{Timestamp, Zoned};
#[derive(Clone, Debug)]
pub(super) struct Zone(TimeZone);
impl Zone {
pub(super) fn here() -> Self {
Self(TimeZone::system())
}
pub(super) fn of(zone: TimeZone) -> Self {
Self(zone)
}
pub(super) fn at(&self, at: Timestamp) -> Zoned {
at.to_zoned(self.0.clone())
}
pub(super) fn name(&self) -> Option<String> {
self.0.iana_name().map(str::to_owned)
}
pub(super) fn instant_of(&self, local: DateTime) -> Option<Timestamp> {
let named = self.0.to_ambiguous_timestamp(local);
if matches!(named.offset(), AmbiguousOffset::Gap { .. }) {
return None;
}
named.earlier().ok()
}
}
}
#[cfg(all(target_family = "wasm", not(tilo_no_js)))]
mod platform {
use std::rc::Rc;
use jiff::civil::DateTime;
use jiff::tz::Offset;
use jiff::{Timestamp, Zoned};
type Offsets = Box<dyn Fn(i64) -> Option<i32>>;
#[derive(Clone)]
pub(super) enum Zone {
Browser,
Named(Rc<(String, Offsets)>),
}
impl std::fmt::Debug for Zone {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Browser => f.write_str("Browser"),
Self::Named(named) => f.debug_tuple("Named").field(&named.0).finish(),
}
}
}
impl Zone {
pub(super) const fn here() -> Self {
Self::Browser
}
pub(super) fn named(name: &str) -> Option<Self> {
let offsets = crate::page::zone_offsets(name)?;
Some(Self::Named(Rc::new((name.to_owned(), Box::new(offsets)))))
}
pub(super) fn at(&self, at: Timestamp) -> Zoned {
super::read(at, |at| self.offset(at))
}
pub(super) fn name(&self) -> Option<String> {
match self {
Self::Browser => crate::page::zone_name(),
Self::Named(named) => Some(named.0.clone()),
}
}
pub(super) fn instant_of(&self, local: DateTime) -> Option<Timestamp> {
super::resolve(local, |at| self.offset(at))
}
fn offset(&self, at: Timestamp) -> Offset {
let seconds = match self {
Self::Browser => Some(crate::page::offset_seconds(at.as_millisecond())),
Self::Named(named) => (named.1)(at.as_millisecond()),
};
seconds
.and_then(|seconds| Offset::from_seconds(seconds).ok())
.unwrap_or(Offset::UTC)
}
}
}
#[cfg(test)]
mod tests {
use jiff::Timestamp;
use jiff::civil::{Date, DateTime, Time};
use jiff::tz::{Offset, TimeZone};
use super::{read, resolve};
fn instant(local: DateTime) -> Timestamp {
local
.to_zoned(TimeZone::UTC)
.expect("the test's wall times are inside the calendar")
.timestamp()
}
fn spring() -> Timestamp {
instant(Date::new(2026, 3, 29).unwrap().at(1, 0, 0, 0))
}
fn autumn() -> Timestamp {
instant(Date::new(2026, 10, 25).unwrap().at(1, 0, 0, 0))
}
fn offsets(at: Timestamp) -> Offset {
if (spring()..autumn()).contains(&at) {
jiff::tz::offset(2)
} else {
jiff::tz::offset(1)
}
}
fn wall(at: Timestamp) -> DateTime {
read(at, offsets).datetime()
}
fn seconds(at: Timestamp) -> i64 {
at.as_second()
}
#[test]
fn a_clock_reads_local_on_each_side_of_a_change_of_clocks() {
let before = spring()
.checked_sub(jiff::SignedDuration::from_secs(1))
.unwrap();
assert_eq!((wall(before).hour(), wall(before).minute()), (1, 59));
assert_eq!(read(before, offsets).offset().seconds(), 3600);
assert_eq!(
(wall(spring()).hour(), wall(spring()).minute()),
(3, 0),
"02:00 is skipped"
);
assert_eq!(read(spring(), offsets).offset().seconds(), 7200);
let before = autumn()
.checked_sub(jiff::SignedDuration::from_secs(1))
.unwrap();
assert_eq!(read(before, offsets).offset().seconds(), 7200);
assert_eq!((wall(before).hour(), wall(before).minute()), (2, 59));
assert_eq!(wall(autumn()).hour(), 2, "02:00 comes twice");
assert_eq!(read(autumn(), offsets).offset().seconds(), 3600);
}
#[test]
fn a_zone_is_read_by_its_name_and_a_thread_told_it_reads_there() {
let Some(vienna) = super::Zone::named("Europe/Vienna") else {
return;
};
assert!(
super::zone_names()
.iter()
.any(|name| name == "Europe/Vienna")
);
assert!(super::Zone::named("../../etc/passwd").is_none());
assert!(super::Zone::named("Nowhere/Atlantis").is_none());
let winter = instant(Date::new(2026, 1, 15).unwrap().at(12, 0, 0, 0));
assert_eq!(vienna.at(winter).offset().seconds(), 3600);
std::thread::spawn(move || {
super::install(Some(vienna));
assert_eq!(super::Zone::here().name().as_deref(), Some("Europe/Vienna"));
assert_eq!(super::at(winter).datetime().hour(), 13);
super::install(None);
assert!(super::told::installed().is_none());
})
.join()
.unwrap();
}
#[test]
fn a_wall_time_the_clocks_skipped_names_no_instant() {
let skipped = Date::new(2026, 3, 29).unwrap().at(2, 30, 0, 0);
assert_eq!(resolve(skipped, offsets), None);
}
#[test]
fn a_wall_time_the_clocks_passed_twice_names_the_earlier_instant() {
let twice = Date::new(2026, 10, 25).unwrap().at(2, 30, 0, 0);
let at = resolve(twice, offsets).expect("02:30 stands twice");
assert_eq!(wall(at), twice, "the earlier one still reads 02:30");
assert_eq!(
seconds(at),
seconds(autumn()) - 1800,
"at +2, before the change"
);
}
#[test]
fn a_wall_time_away_from_a_change_names_its_one_instant() {
let noon = Date::new(2026, 7, 1).unwrap().at(12, 0, 0, 0);
let at = resolve(noon, offsets).expect("noon of a July day stands once");
assert_eq!(wall(at), noon);
assert_eq!(at.as_second() % 60, 0);
let midnight = Date::new(2026, 1, 1).unwrap().to_datetime(Time::midnight());
assert_eq!(
read(resolve(midnight, offsets).unwrap(), offsets).datetime(),
midnight
);
}
#[test]
fn the_wall_clock_is_one_reading_as_an_instant_and_in_the_persons_zone() {
tilo_test_env::in_own_process(
module_path!(),
"the_wall_clock_is_one_reading_as_an_instant_and_in_the_persons_zone",
|| {
let held = crate::UNIX_EPOCH + crate::time::Duration::from_secs(1_791_086_040);
let _held = crate::testing::hold_wall_everywhere(
crate::testing::OwnProcess::of_a_test_in_its_own_process(),
held,
);
let instant = super::now_instant();
assert_eq!(instant.as_second(), 1_791_086_040);
assert_eq!(
super::now().timestamp(),
instant,
"the local reading is that instant"
);
assert_eq!(
super::at(instant),
super::now(),
"and the instant reads back as the same local time"
);
},
);
}
}
}
pub mod time {
pub use crate::imp::{Duration, Instant, SystemTime, SystemTimeError, UNIX_EPOCH};
}
#[cfg(any(not(target_family = "wasm"), tilo_no_js))]
mod imp {
pub use std::time::{Duration, Instant, SystemTime, SystemTimeError, UNIX_EPOCH};
#[inline]
pub const fn to_std(time: SystemTime) -> std::time::SystemTime {
time
}
#[inline]
pub const fn from_std(time: std::time::SystemTime) -> SystemTime {
time
}
}
#[cfg(all(target_family = "wasm", not(tilo_no_js)))]
mod imp {
pub use web_time::{Duration, Instant, SystemTime, SystemTimeError, UNIX_EPOCH};
pub fn to_std(time: SystemTime) -> std::time::SystemTime {
let converted = match time.duration_since(UNIX_EPOCH) {
Ok(after) => std::time::UNIX_EPOCH.checked_add(after),
Err(before) => std::time::UNIX_EPOCH.checked_sub(before.duration()),
};
converted.unwrap_or(std::time::UNIX_EPOCH)
}
pub fn from_std(time: std::time::SystemTime) -> SystemTime {
let converted = match time.duration_since(std::time::UNIX_EPOCH) {
Ok(after) => UNIX_EPOCH.checked_add(after),
Err(before) => UNIX_EPOCH.checked_sub(before.duration()),
};
converted.unwrap_or(UNIX_EPOCH)
}
}
#[allow(
clippy::disallowed_methods,
reason = "off the page this crate is where std's monotonic clock is read"
)]
#[must_use]
#[inline]
pub fn now() -> Instant {
#[cfg(any(test, feature = "test-support"))]
if let Some(at) = testing::monotonic_override() {
return at;
}
Instant::now()
}
#[cfg(any(test, feature = "test-support"))]
pub mod testing {
use super::time::{Instant, SystemTime};
use std::cell::Cell;
thread_local! {
static MONOTONIC: Cell<Option<Instant>> = const { Cell::new(None) };
}
pub(super) fn monotonic_override() -> Option<Instant> {
MONOTONIC.with(Cell::get)
}
struct Restore(Option<Instant>);
impl Drop for Restore {
fn drop(&mut self) {
MONOTONIC.with(|clock| clock.set(self.0));
}
}
pub fn with_monotonic_time<T>(at: Instant, operation: impl FnOnce() -> T) -> T {
let _restore = Restore(MONOTONIC.with(|clock| clock.replace(Some(at))));
operation()
}
struct RestoreWall(Option<u64>);
impl Drop for RestoreWall {
fn drop(&mut self) {
super::FROZEN_HERE_MS.with(|here| here.set(self.0));
}
}
#[derive(Clone, Copy)]
pub struct OwnProcess(());
impl OwnProcess {
#[doc(hidden)]
#[must_use]
pub const fn of_a_test_in_its_own_process() -> Self {
Self(())
}
}
pub struct WallHold {
_process: OwnProcess,
pinned: std::sync::Arc<super::AtomicU64>,
replaced: Option<std::sync::Arc<super::AtomicU64>>,
}
impl Drop for WallHold {
fn drop(&mut self) {
let mut process = super::PROCESS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
if process
.as_ref()
.is_some_and(|pin| std::sync::Arc::ptr_eq(pin, &self.pinned))
{
*process = self.replaced.take();
}
}
}
pub fn hold_wall_everywhere(process: OwnProcess, at: SystemTime) -> WallHold {
let pinned = std::sync::Arc::new(super::AtomicU64::new(super::wall_ms(at)));
let replaced = super::PROCESS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.replace(pinned.clone());
WallHold {
_process: process,
pinned,
replaced,
}
}
thread_local! {
static FOLLOWED: std::cell::RefCell<Option<std::sync::Arc<std::sync::atomic::AtomicU64>>> =
const { std::cell::RefCell::new(None) };
}
pub(super) fn followed_ms() -> u64 {
FOLLOWED.with(|followed| {
followed
.borrow()
.as_ref()
.map_or(0, |wall| wall.load(std::sync::atomic::Ordering::Relaxed))
})
}
#[derive(Clone, Debug, Default)]
pub struct SharedWall(std::sync::Arc<std::sync::atomic::AtomicU64>);
impl SharedWall {
pub fn follow(&self) {
FOLLOWED.with(|followed| *followed.borrow_mut() = Some(self.0.clone()));
}
pub fn pin(&self, at: SystemTime) {
self.0
.store(super::wall_ms(at), std::sync::atomic::Ordering::Relaxed);
}
pub fn release(&self) {
self.0.store(0, std::sync::atomic::Ordering::Relaxed);
}
}
pub fn with_wall<T>(at: SystemTime, operation: impl FnOnce() -> T) -> T {
let ms = super::wall_ms(at);
let _restore = RestoreWall(super::FROZEN_HERE_MS.with(|here| here.replace(Some(ms))));
operation()
}
}
#[cfg(test)]
mod scoped_monotonic_tests {
use super::*;
tilo_test_env::scenario! {
fn a_held_clock_is_the_holding_threads_alone_and_ends_with_its_scope;
phase a_shared_wall_pins_the_threads_following_it_until_released;
{
const PINNED: u64 = 123_456;
let wall = testing::SharedWall::default();
let follower = {
let wall = wall.clone();
move || {
wall.follow();
unix_now()
}
};
assert_ne!(std::thread::spawn(follower.clone()).join().unwrap(), PINNED);
wall.pin(UNIX_EPOCH + Duration::from_secs(PINNED));
assert_eq!(std::thread::spawn(follower.clone()).join().unwrap(), PINNED);
assert_ne!(
unix_now(),
PINNED,
"a thread following none keeps its clock"
);
wall.release();
assert_ne!(std::thread::spawn(follower).join().unwrap(), PINNED);
}
phase scoped_time_is_thread_local_nested_and_restored_after_unwind;
{
let at = now().checked_add(Duration::from_secs(60)).unwrap();
testing::with_monotonic_time(at, || {
assert_eq!(now(), at);
let later = at.checked_add(Duration::from_secs(1)).unwrap();
testing::with_monotonic_time(later, || assert_eq!(now(), later));
assert_eq!(now(), at);
assert!(std::thread::spawn(now).join().unwrap() < at);
let panicked = std::panic::catch_unwind(|| {
testing::with_monotonic_time(later, || panic!("unwind the clock scope"));
});
assert!(panicked.is_err());
assert_eq!(now(), at);
});
assert!(now() < at);
}
phase scoped_wall_time_is_thread_local_nested_and_restored_after_unwind;
{
let at = UNIX_EPOCH + Duration::from_secs(4_000_000_000);
testing::with_wall(at, || {
assert_eq!(wall(), at);
let later = at + Duration::from_secs(3);
testing::with_wall(later, || assert_eq!(wall(), later));
assert_eq!(wall(), at);
assert!(std::thread::spawn(wall).join().unwrap() < at);
let panicked = std::panic::catch_unwind(|| {
testing::with_wall(later, || panic!("unwind the clock scope"));
});
assert!(panicked.is_err());
assert_eq!(wall(), at);
});
assert!(wall() < at);
}
phase a_clock_lent_at_the_epoch_is_a_clock_the_code_reads;
{
let (ms, held, frozen) = with_wall_ms(0, || (unix_now_ms(), wall(), wall_frozen()));
assert_eq!(ms, 0, "the lent time is the time the code reads");
assert_eq!(held, UNIX_EPOCH);
assert!(frozen, "a hold at the epoch is a hold");
assert!(
unix_now_ms() > 0,
"the thread's own clock runs again after the hold"
);
}
phase two_threads_holding_different_wall_times_at_once_each_read_their_own;
{
let first = UNIX_EPOCH + Duration::from_secs(4_000_000_000);
let second = UNIX_EPOCH + Duration::from_secs(4_100_000_000);
let together = std::sync::Arc::new(std::sync::Barrier::new(2));
let hold = |at: SystemTime, other: SystemTime| {
let together = std::sync::Arc::clone(&together);
std::thread::spawn(move || {
testing::with_wall(at, || {
together.wait();
assert_eq!(wall(), at);
assert_ne!(wall(), other);
together.wait();
});
together.wait();
assert!(wall() < first, "the hold ended with its scope");
})
};
let threads = [hold(first, second), hold(second, first)];
for thread in threads {
thread.join().unwrap();
}
}
}
}
thread_local! {
static FROZEN_HERE_MS: std::cell::Cell<Option<u64>> = const { std::cell::Cell::new(None) };
}
fn frozen_ms() -> Option<u64> {
match FROZEN_HERE_MS.with(std::cell::Cell::get) {
Some(here) => Some(here),
None => match followed_ms() {
0 => match pinned_ms() {
0 => None,
pinned => Some(pinned),
},
followed => Some(followed),
},
}
}
pub fn with_wall_ms<T>(now_ms: u64, f: impl FnOnce() -> T) -> T {
struct Restore(Option<u64>);
impl Drop for Restore {
fn drop(&mut self) {
FROZEN_HERE_MS.with(|here| here.set(self.0));
}
}
let _restore = Restore(FROZEN_HERE_MS.with(|here| here.replace(Some(now_ms))));
f()
}
static PROCESS: std::sync::Mutex<Option<std::sync::Arc<AtomicU64>>> = std::sync::Mutex::new(None);
fn pinned_ms() -> u64 {
PROCESS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner)
.as_ref()
.map_or(0, |ms| ms.load(Ordering::Relaxed))
}
#[cfg(any(test, feature = "test-support"))]
fn followed_ms() -> u64 {
testing::followed_ms()
}
#[cfg(not(any(test, feature = "test-support")))]
const fn followed_ms() -> u64 {
0
}
#[allow(
clippy::disallowed_methods,
reason = "off the page this crate is where std's wall clock is read"
)]
#[must_use]
#[inline]
pub fn wall() -> SystemTime {
match frozen_ms() {
Some(ms) => UNIX_EPOCH
.checked_add(Duration::from_millis(ms))
.unwrap_or(UNIX_EPOCH),
None => SystemTime::now(),
}
}
#[cfg(not(test))]
pub fn pin_wall(at: SystemTime) {
let pinned = std::sync::Arc::new(AtomicU64::new(wall_ms(at)));
*PROCESS
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner) = Some(pinned);
}
fn wall_ms(at: SystemTime) -> u64 {
at.duration_since(UNIX_EPOCH)
.map_or(1, |since| saturating_u64(since.as_millis()).max(1))
}
#[must_use]
pub fn wall_frozen() -> bool {
frozen_ms().is_some()
}
#[must_use]
pub fn is_synchronised() -> Option<bool> {
#[cfg(all(target_os = "linux", not(target_family = "wasm")))]
{
let output = std::process::Command::new("timedatectl")
.args(["show", "-p", "NTPSynchronized", "--value"])
.output()
.ok()?;
if !output.status.success() {
return None;
}
match String::from_utf8_lossy(&output.stdout).trim() {
"yes" => Some(true),
"no" => Some(false),
_ => None,
}
}
#[cfg(not(all(target_os = "linux", not(target_family = "wasm"))))]
{
None
}
}
#[must_use]
pub fn is_zone_name(name: &str) -> bool {
const MAX_NAME: usize = 64;
!name.is_empty()
&& name.len() <= MAX_NAME
&& name.split('/').all(|part| {
!part.is_empty()
&& part != "."
&& part != ".."
&& part.chars().all(|character| {
character.is_ascii_alphanumeric() || "_+-.".contains(character)
})
})
}
#[must_use]
pub fn local_zone_name() -> Option<String> {
#[cfg(all(unix, not(target_family = "wasm")))]
{
if let Ok(tz) = std::env::var("TZ") {
let tz = tz.trim_start_matches(':');
if !tz.is_empty() && !tz.starts_with('/') {
return Some(tz.to_owned());
}
}
let link = std::fs::read_link("/etc/localtime").ok()?;
let (_, zone) = link.to_str()?.split_once("zoneinfo/")?;
(!zone.is_empty()).then(|| zone.to_owned())
}
#[cfg(all(target_family = "wasm", not(tilo_no_js)))]
{
page::zone_name()
}
#[cfg(not(any(
all(unix, not(target_family = "wasm")),
all(target_family = "wasm", not(tilo_no_js))
)))]
{
None
}
}
#[cfg(all(target_family = "wasm", not(tilo_no_js)))]
mod page {
use wasm_bindgen::JsValue;
thread_local! {
static ZONE_NAME: std::cell::OnceCell<Option<String>> = const { std::cell::OnceCell::new() };
}
pub(crate) fn zone_name() -> Option<String> {
ZONE_NAME.with(|name| name.get_or_init(read_zone_name).clone())
}
fn read_zone_name() -> Option<String> {
let format = js_sys::Intl::DateTimeFormat::default();
let resolved = js_sys::Intl::DateTimeFormat::resolved_options(&format);
js_sys::Reflect::get(&resolved, &JsValue::from_str("timeZone"))
.ok()?
.as_string()
}
#[cfg(feature = "local")]
pub(crate) fn zone_offsets(name: &str) -> Option<impl Fn(i64) -> Option<i32> + use<>> {
use wasm_bindgen::JsCast as _;
let intl = js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str("Intl")).ok()?;
let make = js_sys::Reflect::get(&intl, &JsValue::from_str("DateTimeFormat"))
.ok()?
.dyn_into::<js_sys::Function>()
.ok()?;
let options = js_sys::Object::new();
js_sys::Reflect::set(
&options,
&JsValue::from_str("timeZone"),
&JsValue::from_str(name),
)
.ok()?;
js_sys::Reflect::set(
&options,
&JsValue::from_str("timeZoneName"),
&JsValue::from_str("longOffset"),
)
.ok()?;
let arguments = js_sys::Array::of2(&js_sys::Array::of1(&"en-US".into()), &options);
let format: js_sys::Intl::DateTimeFormat = js_sys::Reflect::construct(&make, &arguments)
.ok()?
.unchecked_into();
Some(move |unix_ms: i64| {
let date = js_sys::Date::new(&millis(unix_ms));
let parts = format.format_to_parts(&date);
parts.iter().find_map(|part| {
let kind = js_sys::Reflect::get(&part, &JsValue::from_str("type")).ok()?;
if kind.as_string().as_deref() != Some("timeZoneName") {
return None;
}
let value = js_sys::Reflect::get(&part, &JsValue::from_str("value")).ok()?;
offset_named(&value.as_string()?)
})
})
}
#[cfg(feature = "local")]
pub(crate) fn zone_names() -> Vec<String> {
let names = (|| {
let intl = js_sys::Reflect::get(&js_sys::global(), &JsValue::from_str("Intl")).ok()?;
let list: js_sys::Function = wasm_bindgen::JsCast::dyn_into(
js_sys::Reflect::get(&intl, &JsValue::from_str("supportedValuesOf")).ok()?,
)
.ok()?;
let names = list.call1(&intl, &JsValue::from_str("timeZone")).ok()?;
Some(
js_sys::Array::from(&names)
.iter()
.filter_map(|name| name.as_string())
.collect::<Vec<_>>(),
)
})();
names.unwrap_or_default()
}
#[cfg(feature = "local")]
fn offset_named(written: &str) -> Option<i32> {
let offset = written.strip_prefix("GMT")?;
if offset.is_empty() {
return Some(0);
}
let mut chars = offset.chars();
let sign = match chars.next()? {
'+' => 1,
'-' | '\u{2212}' => -1,
_ => return None,
};
let offset = chars.as_str();
let mut parts = offset.split(':');
let hours: i32 = parts.next()?.parse().ok()?;
let minutes: i32 = parts
.next()
.map_or(Some(0), |minutes| minutes.parse().ok())?;
let seconds: i32 = parts
.next()
.map_or(Some(0), |seconds| seconds.parse().ok())?;
hours
.checked_mul(3600)?
.checked_add(minutes.checked_mul(60)?)?
.checked_add(seconds)?
.checked_mul(sign)
}
#[cfg(feature = "local")]
pub(crate) fn offset_seconds(unix_ms: i64) -> i32 {
let date = js_sys::Date::new(&millis(unix_ms));
let west = minutes(js_sys::Date::get_timezone_offset(&date));
west.saturating_mul(60).saturating_neg()
}
#[cfg(feature = "local")]
#[allow(
clippy::cast_precision_loss,
reason = "the browser's clock takes milliseconds as a double, which holds them exactly"
)]
fn millis(unix_ms: i64) -> JsValue {
JsValue::from_f64(unix_ms as f64)
}
#[cfg(feature = "local")]
#[allow(
clippy::cast_possible_truncation,
reason = "the browser's offset is a whole number of minutes, within an hour's twelve"
)]
fn minutes(west_of_utc: f64) -> i32 {
i32::try_from(west_of_utc as i64).unwrap_or(0)
}
}
#[must_use]
#[inline]
pub fn to_std(time: SystemTime) -> std::time::SystemTime {
imp::to_std(time)
}
#[must_use]
#[inline]
pub fn from_std(time: std::time::SystemTime) -> SystemTime {
imp::from_std(time)
}
fn since_epoch() -> Option<Duration> {
wall().duration_since(UNIX_EPOCH).ok()
}
fn saturating_u64(value: u128) -> u64 {
u64::try_from(value).unwrap_or(u64::MAX)
}
pub fn try_unix_now() -> Option<u64> {
since_epoch().map(|duration| duration.as_secs())
}
pub fn try_unix_now_ms() -> Option<u64> {
since_epoch().map(|duration| saturating_u64(duration.as_millis()))
}
pub fn try_unix_now_ns() -> Option<u64> {
since_epoch().map(|duration| saturating_u64(duration.as_nanos()))
}
pub fn unix_now() -> u64 {
try_unix_now().unwrap_or(0)
}
pub fn unix_now_ms() -> u64 {
try_unix_now_ms().unwrap_or(0)
}
pub fn unix_now_ns() -> u64 {
try_unix_now_ns().unwrap_or(0)
}
pub fn unix_now_i64() -> i64 {
i64::try_from(unix_now()).unwrap_or(i64::MAX)
}
pub fn unix_now_ms_i64() -> i64 {
i64::try_from(unix_now_ms()).unwrap_or(i64::MAX)
}
pub trait Clock: Send + Sync {
fn now_ms(&self) -> u64;
}
#[derive(Clone, Copy, Debug, Default)]
pub struct SystemClock;
impl Clock for SystemClock {
fn now_ms(&self) -> u64 {
unix_now_ms()
}
}
#[derive(Clone, Copy, Debug, Default)]
pub struct MonotonicClock;
impl Clock for MonotonicClock {
#[allow(
clippy::disallowed_methods,
reason = "off the page this crate is where std's monotonic clock is read"
)]
fn now_ms(&self) -> u64 {
static START: OnceLock<Instant> = OnceLock::new();
saturating_u64(START.get_or_init(now).elapsed().as_millis())
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct FixedClock(pub u64);
impl Clock for FixedClock {
fn now_ms(&self) -> u64 {
self.0
}
}
#[derive(Debug, Default)]
pub struct ManualClock(AtomicU64);
impl ManualClock {
pub const fn new(now_ms: u64) -> Self {
Self(AtomicU64::new(now_ms))
}
pub fn set(&self, now_ms: u64) {
self.0.store(now_ms, Ordering::SeqCst);
}
pub fn advance(&self, delta_ms: u64) {
let _ = self
.0
.fetch_update(Ordering::SeqCst, Ordering::SeqCst, |now| {
Some(now.saturating_add(delta_ms))
});
}
}
impl Clock for ManualClock {
fn now_ms(&self) -> u64 {
self.0.load(Ordering::SeqCst)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_threads_own_wall_clock_shadows_the_process_pin_only_on_that_thread() {
const OTHER: u64 = 4_000_000_000;
let at = UNIX_EPOCH + Duration::from_secs(OTHER);
tilo_test_env::in_own_process(
module_path!(),
"a_threads_own_wall_clock_shadows_the_process_pin_only_on_that_thread",
|| {
let process = testing::OwnProcess::of_a_test_in_its_own_process();
let _held = testing::hold_wall_everywhere(process, at);
assert_eq!(unix_now(), OTHER);
assert!(wall_frozen());
testing::with_wall(at + Duration::from_secs(1), || {
assert_eq!(wall(), at + Duration::from_secs(1), "the thread's own");
assert_eq!(
std::thread::spawn(wall).join().unwrap(),
at,
"another thread keeps the process's"
);
});
assert_eq!(wall(), at, "the process's, after the thread's scope");
},
);
}
#[test]
#[cfg(feature = "local")]
fn a_named_zone_is_read_as_itself_whatever_the_machines_own() {
tilo_test_env::in_own_process(
module_path!(),
"a_named_zone_is_read_as_itself_whatever_the_machines_own",
|| {
let mut zone = tilo_test_env::ScopedEnv::new();
zone.set("TZ", "America/Los_Angeles");
let _held = testing::hold_wall_everywhere(
testing::OwnProcess::of_a_test_in_its_own_process(),
UNIX_EPOCH + Duration::from_secs(1_794_459_600),
);
let now = local::now();
assert_eq!(
now.time_zone().iana_name(),
Some("America/Los_Angeles"),
"the named zone is the one the clock stands in"
);
assert_eq!(
local::today().to_string(),
"2026-11-11",
"their day, not UTC's"
);
assert_eq!(
local::instant_of(jiff::civil::date(2026, 11, 11).at(21, 0, 0, 0)),
jiff::Timestamp::from_second(1_794_459_600).ok(),
"and the evening they read is the instant it names"
);
},
);
}
tilo_test_env::scenario! {
fn clocks_read_units_that_agree_and_a_hold_ends_with_its_scope;
phase a_frozen_wall_clock_holds_while_the_monotonic_clock_runs;
{
let at = UNIX_EPOCH + Duration::from_secs(1_782_816_120);
let before = now();
testing::with_wall(at, || {
assert!(wall_frozen());
assert_eq!(wall(), at);
assert_eq!(unix_now(), 1_782_816_120);
assert_eq!(wall(), at, "it stays put");
assert!(now() >= before, "the monotonic clock is not frozen");
});
assert!(!wall_frozen(), "the scope ended");
}
phase clocks_agree_on_units_move_only_as_told_and_never_backwards;
{
assert!(!wall_frozen(), "no other test's pinned time is read here");
{
let secs = unix_now();
let ms = unix_now_ms();
let ns = unix_now_ns();
assert!(secs > 1_600_000_000);
assert!(ms / 1_000 >= secs);
assert!(ns / 1_000_000 >= ms);
assert_eq!(unix_now_i64(), i64::try_from(unix_now()).unwrap());
}
{
assert_eq!(FixedClock(7).now_ms(), 7);
let clock = ManualClock::new(10);
assert_eq!(clock.now_ms(), 10);
clock.advance(5);
assert_eq!(clock.now_ms(), 15);
clock.set(3);
assert_eq!(clock.now_ms(), 3);
clock.set(u64::MAX - 1);
clock.advance(10);
assert_eq!(clock.now_ms(), u64::MAX);
}
{
let first = MonotonicClock.now_ms();
assert!(MonotonicClock.now_ms() >= first);
}
}
phase now_is_monotonic;
{
let earlier = now();
assert!(now() >= earlier);
}
phase wall_is_after_the_epoch;
{
assert!(wall().duration_since(UNIX_EPOCH).is_ok());
}
phase std_conversions_round_trip_on_both_sides_of_the_epoch;
{
let offset = Duration::from_millis(1_700_000_000_123);
for time in [UNIX_EPOCH + offset, UNIX_EPOCH - offset, UNIX_EPOCH] {
assert_eq!(from_std(to_std(time)), time);
}
assert_eq!(to_std(UNIX_EPOCH + offset), std::time::UNIX_EPOCH + offset);
}
}
}