From c5262db54a1ef16b6f33e361bacd7ac169e6f7ec Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Tue, 3 Nov 2026 23:41:26 +0100 Subject: [PATCH] fix: resolve schedule time zones from current tzdata, drop chrono-tz (#12196) * fix: resolve schedule time zones from current tzdata instead of chrono-tz Co-Authored-By: Claude Opus 5.5 * chore: point ee-repo-ref at the partition tz companion change Co-Authored-By: Claude Opus 5.5 * fix: resolve legacy zone names from the bundled tzdb when the system lacks them Co-Authored-By: Claude Opus 5.5 * fix: resolve time zones from whichever tzdb copy is newer, system or bundled Co-Authored-By: Claude Opus 5.5 * chore: update ee-repo-ref to e44abce4b4c37f7cafc2840184274b14574103f1 This commit updates the EE repository reference after PR #948 was merged in windmill-ee-private. Previous ee-repo-ref: 07168bf424ddcd5ef438fc918bf5dace9d825e01 New ee-repo-ref: e44abce4b4c37f7cafc2840184274b14574103f1 Automated by sync-ee-ref workflow. --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: windmill-internal-app[bot] --- backend/Cargo.lock | 7 +- backend/Cargo.toml | 3 +- backend/ee-repo-ref.txt | 2 +- backend/windmill-api-schedule/Cargo.toml | 1 - backend/windmill-api-schedule/src/lib.rs | 3 +- backend/windmill-api/Cargo.toml | 1 - backend/windmill-api/src/jobs.rs | 3 +- backend/windmill-common/Cargo.toml | 3 +- backend/windmill-common/src/lib.rs | 1 + backend/windmill-common/src/tz.rs | 309 +++++++++++++++++++++++ backend/windmill-common/src/utils.rs | 12 +- backend/windmill-queue/Cargo.toml | 1 - backend/windmill-queue/src/schedule.rs | 8 +- 13 files changed, 329 insertions(+), 25 deletions(-) create mode 100644 backend/windmill-common/src/tz.rs diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 367a254b60..c2e28a2fc3 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -7293,6 +7293,7 @@ dependencies = [ "defmt", "jiff-core", "jiff-static", + "jiff-tzdb", "jiff-tzdb-platform", "log", "portable-atomic", @@ -15789,7 +15790,6 @@ dependencies = [ "base64 0.22.1", "bytes", "chrono", - "chrono-tz", "const_format", "cookie", "cron", @@ -16245,7 +16245,6 @@ version = "1.859.0" dependencies = [ "axum", "chrono", - "chrono-tz", "serde", "serde_json", "sql-builder", @@ -16477,7 +16476,6 @@ dependencies = [ "bitflags 2.13.2", "bytes", "chrono", - "chrono-tz", "const-str", "const_format", "crc", @@ -16500,6 +16498,8 @@ dependencies = [ "indexmap 2.14.2", "ipnet", "itertools 0.14.0", + "jiff", + "jiff-tzdb", "jsonwebtoken 10.4.0", "lazy_static", "magic-crypt", @@ -17082,7 +17082,6 @@ dependencies = [ "axum", "backon", "chrono", - "chrono-tz", "cron", "dashmap", "futures", diff --git a/backend/Cargo.toml b/backend/Cargo.toml index abf39a6cad..25040fbd94 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -471,7 +471,8 @@ uuid = { version = "^1", features = ["serde", "v4", "js"] } thiserror = "^2" anyhow = "^1" chrono = { version = "^0.4", features = ["serde"] } -chrono-tz = "^0.10.1" +jiff = { version = "0.2.39", features = ["tzdb-bundle-always"] } +jiff-tzdb = "0.1.9" derive_more = { version = "1", features = ["deref", "deref_mut"], default-features = false } tracing = "^0" tracing-subscriber = { version = "^0", features = ["env-filter", "json"] } diff --git a/backend/ee-repo-ref.txt b/backend/ee-repo-ref.txt index 7aa161da8a..826508bf7d 100644 --- a/backend/ee-repo-ref.txt +++ b/backend/ee-repo-ref.txt @@ -1 +1 @@ -c4e610999ca0d1abab06b1660cc48e486ab870b3 +e44abce4b4c37f7cafc2840184274b14574103f1 diff --git a/backend/windmill-api-schedule/Cargo.toml b/backend/windmill-api-schedule/Cargo.toml index f7a612c628..ea79b374f9 100644 --- a/backend/windmill-api-schedule/Cargo.toml +++ b/backend/windmill-api-schedule/Cargo.toml @@ -24,7 +24,6 @@ windmill-git-sync.workspace = true windmill-queue.workspace = true axum.workspace = true chrono.workspace = true -chrono-tz.workspace = true serde.workspace = true serde_json.workspace = true sql-builder.workspace = true diff --git a/backend/windmill-api-schedule/src/lib.rs b/backend/windmill-api-schedule/src/lib.rs index 70b0984605..eb952f157b 100644 --- a/backend/windmill-api-schedule/src/lib.rs +++ b/backend/windmill-api-schedule/src/lib.rs @@ -1133,8 +1133,7 @@ pub async fn preview_schedule( let schedule = ScheduleType::from_str(&payload.schedule, payload.cron_version.as_deref(), true)?; - let tz = - chrono_tz::Tz::from_str(&payload.timezone).map_err(|e| Error::BadRequest(e.to_string()))?; + let tz = windmill_common::tz::Tz::from_str(&payload.timezone).map_err(Error::BadRequest)?; let upcoming: Vec> = schedule.upcoming(tz, 5)?; diff --git a/backend/windmill-api/Cargo.toml b/backend/windmill-api/Cargo.toml index 583d139cf5..925fad6549 100644 --- a/backend/windmill-api/Cargo.toml +++ b/backend/windmill-api/Cargo.toml @@ -104,7 +104,6 @@ tracing.workspace = true sql-builder.workspace = true serde_json.workspace = true chrono.workspace = true -chrono-tz.workspace = true hex.workspace = true base64.workspace = true base32.workspace = true diff --git a/backend/windmill-api/src/jobs.rs b/backend/windmill-api/src/jobs.rs index c1934accb8..76f5440cdd 100644 --- a/backend/windmill-api/src/jobs.rs +++ b/backend/windmill-api/src/jobs.rs @@ -1028,8 +1028,7 @@ async fn refuse_upcoming_schedule_tick(db: &DB, w_id: &str, id: Uuid) -> error:: return Ok(()); }; let sched = ScheduleType::from_str(&tick.schedule, tick.cron_version.as_deref(), false)?; - let tz = - chrono_tz::Tz::from_str(&tick.timezone).map_err(|e| Error::BadRequest(e.to_string()))?; + let tz = windmill_common::tz::Tz::from_str(&tick.timezone).map_err(Error::BadRequest)?; let just_before = (tick.scheduled_for - chrono::Duration::milliseconds(1)).with_timezone(&tz); if sched.find_next(&just_before)? == tick.scheduled_for { return Err(Error::BadRequest(format!( diff --git a/backend/windmill-common/Cargo.toml b/backend/windmill-common/Cargo.toml index 52c38908b5..c44679b611 100644 --- a/backend/windmill-common/Cargo.toml +++ b/backend/windmill-common/Cargo.toml @@ -43,7 +43,8 @@ serde_yml.workspace = true memchr.workspace = true erased-serde = "0.4" chrono.workspace = true -chrono-tz.workspace = true +jiff.workspace = true +jiff-tzdb.workspace = true hex.workspace = true async-trait.workspace = true http.workspace = true diff --git a/backend/windmill-common/src/lib.rs b/backend/windmill-common/src/lib.rs index 8f1e1da8ec..9e98d5be6c 100644 --- a/backend/windmill-common/src/lib.rs +++ b/backend/windmill-common/src/lib.rs @@ -152,6 +152,7 @@ pub mod tracing_init; pub mod trashbin; pub mod trigger_history; pub mod triggers; +pub mod tz; pub mod user_drafts; pub mod usernames; pub mod users; diff --git a/backend/windmill-common/src/tz.rs b/backend/windmill-common/src/tz.rs new file mode 100644 index 0000000000..847b1d907e --- /dev/null +++ b/backend/windmill-common/src/tz.rs @@ -0,0 +1,309 @@ +//! An IANA time zone as a `chrono::TimeZone`, resolved from the newer of two copies of +//! the time zone database: the system's (`$TZDIR`, else `/usr/share/zoneinfo`) and the +//! one bundled into the binary; a name missing from that copy is looked up in the other. +//! chrono-tz compiles its rules in and is no longer updated, so zones that change their +//! rules (British Columbia, Manitoba, ...) kept their old offsets. + +use std::{ + fmt, + io::{BufRead, BufReader}, + path::{Path, PathBuf}, + str::FromStr, + sync::LazyLock, +}; + +use chrono::{FixedOffset, LocalResult, NaiveDate, NaiveDateTime, NaiveTime}; +use jiff::tz::{AmbiguousOffset, TimeZone, TimeZoneDatabase}; + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Tz(TimeZone); + +impl Tz { + pub const UTC: Tz = Tz(TimeZone::UTC); + + fn offset(&self, offset: jiff::tz::Offset, at: jiff::Timestamp) -> TzOffset { + TzOffset { + tz: self.clone(), + // The database holds no offset beyond chrono's ±24h. + fixed: FixedOffset::east_opt(offset.seconds()) + .unwrap_or_else(|| FixedOffset::east_opt(0).unwrap()), + at, + } + } +} + +impl FromStr for Tz { + type Err = String; + + fn from_str(name: &str) -> Result { + static DATABASES: LazyLock = LazyLock::new(|| { + Databases::choose( + system_database(), + TimeZoneDatabase::bundled(), + jiff_tzdb::VERSION, + ) + }); + DATABASES.lookup(name) + } +} + +struct Databases { + primary: TimeZoneDatabase, + /// Debian (trixie onward) ships the backward-compatible names (`US/Eastern`, + /// `Asia/Calcutta`, which browsers still report) in `tzdata-legacy`, which images + /// rarely install, so the system copy can lack a name the bundled one has. + fallback: TimeZoneDatabase, +} + +impl Databases { + /// A system copy of unknown version wins: it is what the host, or `$TZDIR`, asked for. + fn choose( + system: Option<(TimeZoneDatabase, Option)>, + bundled: TimeZoneDatabase, + bundled_version: Option<&str>, + ) -> Databases { + let Some((system, system_version)) = system else { + return Databases { primary: bundled, fallback: TimeZoneDatabase::none() }; + }; + // IANA versions are a year then a letter (`2026f`), so they order as strings. + let bundled_is_newer = matches!( + (bundled_version, system_version.as_deref()), + (Some(b), Some(s)) if b > s + ); + tracing::info!( + "time zones resolve from the {} tzdb (system {}, bundled {})", + if bundled_is_newer { + "bundled" + } else { + "system" + }, + system_version.as_deref().unwrap_or("of unknown version"), + bundled_version.unwrap_or("of unknown version"), + ); + if bundled_is_newer { + Databases { primary: bundled, fallback: system } + } else { + Databases { primary: system, fallback: bundled } + } + } + + fn lookup(&self, name: &str) -> Result { + self.primary + .get(name) + .or_else(|_| self.fallback.get(name)) + .map(Tz) + .map_err(|_| format!("'{name}' is not a valid timezone")) + } +} + +/// The directories jiff searches, in its order, so the version read here is that of the +/// copy whose zones are read. +fn system_database() -> Option<(TimeZoneDatabase, Option)> { + let dirs: Vec = match std::env::var_os("TZDIR") { + Some(dir) => vec![dir.into()], + None => [ + "/usr/share/zoneinfo", + "/usr/share/lib/zoneinfo", + "/etc/zoneinfo", + ] + .into_iter() + .map(PathBuf::from) + .collect(), + }; + dirs.into_iter().find_map(|dir| { + let db = TimeZoneDatabase::from_dir(&dir).ok()?; + (!db.is_definitively_empty()).then(|| (db, system_version(&dir))) + }) +} + +/// The `# version 2026f` line heading `tzdata.zi`, or macOS's `+VERSION`. +fn system_version(dir: &Path) -> Option { + let from_zi = std::fs::File::open(dir.join("tzdata.zi")) + .ok() + .and_then(|f| { + let mut line = String::new(); + BufReader::new(f).read_line(&mut line).ok()?; + Some(line.strip_prefix("# version ")?.trim().to_string()) + }); + from_zi.or_else(|| { + let v = std::fs::read_to_string(dir.join("+VERSION")).ok()?; + Some(v.trim().to_string()).filter(|v| !v.is_empty()) + }) +} + +/// The offset of a `chrono::DateTime`. chrono rebuilds the zone from it +/// (`TimeZone::from_offset`), so it carries the zone, and the instant it applies at so +/// that `%Z` prints the zone's abbreviation as chrono-tz did. +#[derive(Clone, Debug)] +pub struct TzOffset { + tz: Tz, + fixed: FixedOffset, + at: jiff::Timestamp, +} + +impl chrono::Offset for TzOffset { + fn fix(&self) -> FixedOffset { + self.fixed + } +} + +impl fmt::Display for TzOffset { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(self.tz.0.to_offset_info(self.at).abbreviation()) + } +} + +/// Local times resolve as in chrono-tz: a time in a gap has no offset, and one in a fold +/// yields the earlier instant first. +impl chrono::TimeZone for Tz { + type Offset = TzOffset; + + fn from_offset(offset: &Self::Offset) -> Self { + offset.tz.clone() + } + + fn offset_from_local_date(&self, local: &NaiveDate) -> LocalResult { + // As chrono-tz: a date takes any offset in effect on that day, preferring one + // that is unambiguous at its first or last second. + let earliest = self.offset_from_local_datetime(&local.and_time(NaiveTime::MIN)); + let latest = self.offset_from_local_datetime(&local.and_hms_opt(23, 59, 59).unwrap()); + use LocalResult::*; + match (earliest, latest) { + (result @ Single(_), _) => result, + (_, result @ Single(_)) => result, + (Ambiguous(offset, _), _) => Single(offset), + (_, Ambiguous(offset, _)) => Single(offset), + (None, None) => None, + } + } + + fn offset_from_local_datetime(&self, local: &NaiveDateTime) -> LocalResult { + let Some(civil) = to_civil(local) else { + return LocalResult::None; + }; + let at = |offset: jiff::tz::Offset| { + self.offset( + offset, + offset.to_timestamp(civil).unwrap_or(jiff::Timestamp::MAX), + ) + }; + match self.0.to_ambiguous_timestamp(civil).offset() { + AmbiguousOffset::Unambiguous { offset } => LocalResult::Single(at(offset)), + AmbiguousOffset::Fold { before, after } => { + LocalResult::Ambiguous(at(before), at(after)) + } + AmbiguousOffset::Gap { .. } => LocalResult::None, + } + } + + fn offset_from_utc_date(&self, utc: &NaiveDate) -> Self::Offset { + self.offset_from_utc_datetime(&utc.and_time(NaiveTime::MIN)) + } + + fn offset_from_utc_datetime(&self, utc: &NaiveDateTime) -> Self::Offset { + let utc = utc.and_utc(); + let at = jiff::Timestamp::new(utc.timestamp(), utc.timestamp_subsec_nanos() as i32) + .unwrap_or(if utc.timestamp() < 0 { + jiff::Timestamp::MIN + } else { + jiff::Timestamp::MAX + }); + self.offset(self.0.to_offset(at), at) + } +} + +fn to_civil(local: &NaiveDateTime) -> Option { + use chrono::{Datelike, Timelike}; + jiff::civil::DateTime::new( + i16::try_from(local.year()).ok()?, + local.month() as i8, + local.day() as i8, + local.hour() as i8, + local.minute() as i8, + local.second() as i8, + // chrono carries a leap second as nanoseconds past 1e9, which jiff has no room for. + (local.nanosecond() % 1_000_000_000) as i32, + ) + .ok() +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::utils::ScheduleType; + use chrono::{TimeZone as _, Utc}; + + /// The bundled copy, so the expectations do not depend on the host's tzdata. + fn bundled(name: &str) -> Tz { + Tz(jiff::tz::TimeZoneDatabase::bundled().get(name).unwrap()) + } + + fn utc(s: &str) -> chrono::DateTime { + s.parse().unwrap() + } + + #[test] + fn schedules_follow_current_tzdata() { + // British Columbia stopped changing its clocks in 2026: 08:00 stays at UTC-7. + let vancouver = bundled("America/Vancouver"); + let from = utc("2026-11-03T16:00:00Z").with_timezone(&vancouver); + for version in ["v1", "v2"] { + let daily = ScheduleType::from_str("0 0 8 * * *", Some(version), true).unwrap(); + let next = daily.find_next(&from).unwrap(); + assert_eq!( + next.with_timezone(&Utc), + utc("2026-11-04T15:00:00Z"), + "{version}" + ); + } + } + + #[test] + fn the_newer_copy_wins_and_the_other_fills_in_missing_names() { + // An empty database stands in for a system copy lacking the legacy names. + let with_system = |version: Option<&str>| { + Databases::choose( + Some((TimeZoneDatabase::none(), version.map(str::to_string))), + TimeZoneDatabase::bundled(), + Some("2026e"), + ) + }; + let system_wins = |dbs: &Databases| dbs.primary.is_definitively_empty(); + assert!(!system_wins(&with_system(Some("2024a")))); + assert!(system_wins(&with_system(Some("2026f")))); + assert!(system_wins(&with_system(None))); + + let dbs = with_system(Some("2026f")); + for name in ["Asia/Calcutta", "US/Eastern", "Europe/Kiev", "EST5EDT"] { + assert!(dbs.lookup(name).is_ok(), "{name}"); + } + assert!(dbs.lookup("Not/AZone").is_err()); + } + + #[test] + fn local_times_resolve_as_chrono_tz_did() { + let ny = bundled("America/New_York"); + let local = |s: &str| NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M").unwrap(); + + let LocalResult::Ambiguous(earlier, later) = + ny.from_local_datetime(&local("2025-11-02 01:30")) + else { + panic!("a time in the fall-back hour is ambiguous"); + }; + assert_eq!(earlier.with_timezone(&Utc), utc("2025-11-02T05:30:00Z")); + assert_eq!(later.with_timezone(&Utc), utc("2025-11-02T06:30:00Z")); + assert_eq!(earlier.format("%Z %z").to_string(), "EDT -0400"); + assert_eq!(later.format("%Z %z").to_string(), "EST -0500"); + + assert!(ny + .from_local_datetime(&local("2025-03-09 02:30")) + .single() + .is_none()); + assert_eq!( + ny.with_ymd_and_hms(2025, 3, 9, 3, 0, 0) + .unwrap() + .with_timezone(&Utc), + utc("2025-03-09T07:00:00Z") + ); + } +} diff --git a/backend/windmill-common/src/utils.rs b/backend/windmill-common/src/utils.rs index 6ae44ccf7f..8b02a2179f 100644 --- a/backend/windmill-common/src/utils.rs +++ b/backend/windmill-common/src/utils.rs @@ -12,6 +12,7 @@ use crate::ee_oss::LICENSE_KEY_ID; use crate::ee_oss::{send_critical_alert, CriticalAlertKind}; use crate::error::{to_anyhow, Error, Result}; use crate::global_settings::UNIQUE_ID_SETTING; +use crate::tz::Tz; use crate::worker::{EXIT_AFTER_N_JOBS, WORKER_SUFFIX}; use crate::DB; use anyhow::Context; @@ -1032,10 +1033,7 @@ impl ScheduleType { /// date), and schedule pushes disable the schedule on it. Every other error must stay /// transient: croner fails across a DST jump longer than an hour (Antarctica/Troll) /// and succeeds again once the jump has passed. - pub fn find_next( - &self, - starting_from: &chrono::DateTime, - ) -> Result> { + pub fn find_next(&self, starting_from: &chrono::DateTime) -> Result> { let no_run_left = || { Error::NotFound(format!( "cron: the schedule has no run left after {}", @@ -1116,7 +1114,7 @@ impl ScheduleType { // Additional check to make sure the provided schedule can generate a next event if let Ok(ScheduleType::Croner(croner_schedule)) = &schedule_type_result { - let test_time = chrono::Utc::now().with_timezone(&chrono_tz::UTC); + let test_time = chrono::Utc::now().with_timezone(&Tz::UTC); let result = panic::catch_unwind(AssertUnwindSafe(|| { croner_schedule .find_next_occurrence(&test_time, false) @@ -1148,7 +1146,7 @@ impl ScheduleType { pub fn upcoming( &self, - tz: chrono_tz::Tz, + tz: Tz, count: usize, // Number of upcoming events to take ) -> Result>> { let start_time = Utc::now().with_timezone(&tz); @@ -1804,7 +1802,7 @@ mod tests { #[test] fn find_next_reports_only_a_cron_with_no_run_left_as_not_found() { use chrono::TimeZone; - let troll: chrono_tz::Tz = "Antarctica/Troll".parse().unwrap(); + let troll: Tz = "Antarctica/Troll".parse().unwrap(); // Troll's clocks jump from 01:00 to 03:00 on the last Sunday of March. let before_jump = troll.with_ymd_and_hms(2027, 3, 28, 0, 30, 0).unwrap(); diff --git a/backend/windmill-queue/Cargo.toml b/backend/windmill-queue/Cargo.toml index de788f9f25..dcaf1a1d34 100644 --- a/backend/windmill-queue/Cargo.toml +++ b/backend/windmill-queue/Cargo.toml @@ -37,7 +37,6 @@ serde_json.workspace = true ulid.workspace = true uuid.workspace = true chrono.workspace = true -chrono-tz.workspace = true hex.workspace = true rand.workspace = true reqwest.workspace = true diff --git a/backend/windmill-queue/src/schedule.rs b/backend/windmill-queue/src/schedule.rs index 246a05bb0c..b2f4e14085 100644 --- a/backend/windmill-queue/src/schedule.rs +++ b/backend/windmill-queue/src/schedule.rs @@ -37,6 +37,7 @@ use windmill_common::DB; use windmill_common::{ error::{self, Result}, schedule::Schedule, + tz::Tz, utils::{now_from_db, report_critical_error, ScheduleType, StripPath}, }; @@ -144,8 +145,7 @@ pub async fn push_scheduled_job<'c>( let sched = ScheduleType::from_str(&schedule.schedule, schedule.cron_version.as_deref(), false)?; - let tz = chrono_tz::Tz::from_str(&schedule.timezone) - .map_err(|e| error::Error::BadRequest(e.to_string()))?; + let tz = Tz::from_str(&schedule.timezone).map_err(error::Error::BadRequest)?; let now = now_from_db(&mut *tx).await?; @@ -702,7 +702,7 @@ const MAX_COUNTED_MISSES: u32 = 1000; /// the last of them. A chain on time costs one `find_next`: its first slot is `next` itself. fn count_missed_occurrences( sched: &ScheduleType, - tz: &chrono_tz::Tz, + tz: &Tz, prev: DateTime, next: DateTime, ) -> Result<(u32, Option>)> { @@ -947,7 +947,7 @@ mod tests { fn counts_the_slots_missed_between_two_occurrences() { let every_30s = ScheduleType::from_str("*/30 * * * * *", None, false).unwrap(); let at = |s: &str| DateTime::parse_from_rfc3339(s).unwrap().with_timezone(&Utc); - let tz = chrono_tz::UTC; + let tz = Tz::UTC; let prev = at("2026-09-02T08:20:00Z"); let count = |next| count_missed_occurrences(&every_30s, &tz, prev, at(next)).unwrap(); assert_eq!(count("2026-09-02T08:20:30Z"), (0, None));