From da275f9c6c0ea197025d278e9a76f80d5e70a250 Mon Sep 17 00:00:00 2001 From: tecari0 <121329615+tecari0@users.noreply.github.com> Date: Mon, 19 Oct 2026 19:40:35 -0700 Subject: [PATCH 1/4] #2284: date files and the session clock in the host's time zone The catalog stores local civil times, but `system_clock` and `file_modified` both formatted an instant as UTC. A JPEG that carries no capture date is dated by its modification time, so one modified at 20:15 in UTC-5 was catalogued as 01:15 the next day; the same offset went into every `imported`, album `created` and `edited` stamp, and into the "added in the last N days" rules. `import::local_civil` converts an instant with the offset in force *then*, so a file from July keeps summer time when it is read in January. The zone comes from `jiff`, a native-only dependency: it has no dependencies of its own on Unix, where it reads the system database, and embeds one on Windows, which has no canonical copy. The web build keeps its fixed clock. `import::local_instant` is the inverse, so a test can pin a file's modification time to the local date it means whatever zone the machine is in; the fixtures that pinned a fixed epoch for that now use it. Existing catalogs keep the stamps they already hold: the zone they were written in is not recorded, so they are left alone rather than migrated. Co-Authored-By: Claude Opus 5 --- Cargo.lock | 86 +++++++++++++++++++++ Cargo.toml | 6 ++ crates/engine/Cargo.toml | 1 + crates/engine/src/files.rs | 15 +++- crates/engine/src/import.rs | 101 ++++++++++++++++++++++++- crates/engine/src/tests_import.rs | 9 ++- crates/engine/src/tests_import_move.rs | 9 ++- crates/ui-egui/src/headless.rs | 25 +++--- 8 files changed, 231 insertions(+), 21 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 5d7f5111ac..386f5840a8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1199,6 +1199,37 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "defmt" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4a0795ac740cb54d7b3775c9c17345f6aaecf884c01733b39d6017738e0e3e1" +dependencies = [ + "bitflags 1.3.2", + "defmt-macros", +] + +[[package]] +name = "defmt-macros" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "840bb056bd99c495c9188198a258ab787c79f306d864db3e6e8451442d6d8366" +dependencies = [ + "defmt-parser", + "proc-macro2", + "quote", + "syn 3.0.6", +] + +[[package]] +name = "defmt-parser" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "71958f69fb2de856c5dd51ac9377e986ca1aa5919f931bf5395a807277f889a0" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "der" version = "0.7.10" @@ -2698,6 +2729,60 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" +[[package]] +name = "jiff" +version = "0.2.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b2b005715dcbeb0089a3c0dab99f2ff1cc3b2525323552703d648585d342a383" +dependencies = [ + "defmt", + "jiff-core", + "jiff-static", + "jiff-tzdb-platform", + "log", + "portable-atomic", + "portable-atomic-util", + "serde_core", + "windows-link", +] + +[[package]] +name = "jiff-core" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e52fe76043ccecc9005d2305ebaadf7d7fc0cc89ca6baa10a94d6bc68c7128c" +dependencies = [ + "defmt", + "log", +] + +[[package]] +name = "jiff-static" +version = "0.2.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2cc9817253cf7c7ee4684451bd327e88d6f3658014e54a29198625590650695c" +dependencies = [ + "jiff-core", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "jiff-tzdb" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa8377070c6bae868759445e5a77f66d84f0b72f3a054bfb00e6d038b8282da7" + +[[package]] +name = "jiff-tzdb-platform" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "875a5a69ac2bab1a891711cf5eccbec1ce0341ea805560dcd90b7a2e925132e8" +dependencies = [ + "jiff-tzdb", +] + [[package]] name = "jixel" version = "0.3.4" @@ -3225,6 +3310,7 @@ dependencies = [ "ab_glyph", "craft-docpath", "harfrust", + "jiff", "jxl-oxide", "libc", "lightcraft-catalog", diff --git a/Cargo.toml b/Cargo.toml index cf21739d89..0e9dd8ecef 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -57,6 +57,12 @@ siphasher = "1" libc = "0.2" # dev-only: process CPU time in benches dhat = "0.3" # opt-in heap profiling (lightcraft-cli --features dhat-heap) and heap tests; never in product builds +# The host's time zone: the catalog stores local civil times, so a file's modification time and +# the session clock need the UTC offset in force at that instant (DST included). Native only; the +# web build keeps its fixed clock. Zero dependencies on Unix (it reads the system tzdb); on Windows +# it embeds one, since Windows has no canonical copy. +jiff = { version = "0.2", default-features = false, features = ["std", "tz-system", "tzdb-zoneinfo", "tzdb-bundle-platform"] } + # formats quick-xml = "0.38" miniz_oxide = "0.8" diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 091efe0661..8d3a445ed2 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -46,6 +46,7 @@ lightcraft-segment = { workspace = true, optional = true } # model downloads run natively only (the web build fetches no models) [target.'cfg(not(target_arch = "wasm32"))'.dependencies] lightcraft-fetch = { workspace = true } +jiff = { workspace = true } [features] # AI masks with SAM 3 (Object / Describe). Off for the web and CLI builds. diff --git a/crates/engine/src/files.rs b/crates/engine/src/files.rs index 31bb930fbf..600c72b1a8 100644 --- a/crates/engine/src/files.rs +++ b/crates/engine/src/files.rs @@ -1083,11 +1083,12 @@ pub fn fs_hooks() -> (FileLoader, FileProbe) { } /// `path`'s modification time as ISO 8601 (see [`ProbeInfo::file_modified`]); `None` when the -/// filesystem doesn't say or the time is before 1970. +/// filesystem doesn't say or the time is before 1970. In the host's time zone, like every date the +/// catalog stores: this is the capture date of a file that carries none of its own. fn file_modified(path: &str) -> Option { let t = std::fs::metadata(path).ok()?.modified().ok()?; let secs = i64::try_from(t.duration_since(std::time::UNIX_EPOCH).ok()?.as_secs()).ok()?; - Some(crate::import::civil(secs)) + Some(crate::import::local_civil(secs)) } impl crate::Session { @@ -1498,8 +1499,16 @@ pub(crate) mod tests { let path = dir.join("export.jpg"); std::fs::write(&path, &jpeg).unwrap(); let path = path.to_string_lossy().to_string(); + // The case reported in issue #2284: a JPEG that went through a messenger keeps no capture + // date, only a modification time, here 2026-10-18 20:15:11 in UTC-5. Pinning it also dates + // the file in the other half of the year from "now", so a zone with summer time is read at + // the right instant and not at today's offset. + let shot = lightcraft_meta::DateTime::parse_iso("2026-10-19T01:15:11Z").unwrap().unix_seconds(); + let when = std::time::UNIX_EPOCH + std::time::Duration::from_secs(u64::try_from(shot).unwrap()); + std::fs::File::options().write(true).open(&path).unwrap().set_modified(when).unwrap(); let mtime = std::fs::metadata(&path).unwrap().modified().unwrap().duration_since(std::time::UNIX_EPOCH).unwrap().as_secs(); - let want = crate::import::civil(mtime as i64); + assert_eq!(mtime as i64, shot, "the file date is the one the test set"); + let want = crate::import::local_civil(mtime as i64); let (_, probe) = fs_hooks(); let info = probe(&path).unwrap(); assert_eq!(info.captured, None, "the file itself has no date"); diff --git a/crates/engine/src/import.rs b/crates/engine/src/import.rs index 9aa847b01f..7e26027cf6 100644 --- a/crates/engine/src/import.rs +++ b/crates/engine/src/import.rs @@ -1515,7 +1515,7 @@ pub fn system_clock() -> String { #[cfg(not(target_arch = "wasm32"))] { let secs = std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map(|d| d.as_secs() as i64).unwrap_or(0); - civil(secs) + local_civil(secs) } #[cfg(target_arch = "wasm32")] { @@ -1528,6 +1528,50 @@ pub fn civil(secs: i64) -> String { lightcraft_catalog::dates::civil(secs) } +/// Unix seconds → `YYYY-MM-DDTHH:MM:SS` in the host's time zone: the local civil time every date +/// the catalog stores is in (`captured`, `imported`, an album's `created`, an edit's `edited`). +/// +/// The offset is the one in force *at that instant*, not today's, so a photo from July keeps +/// summer time when it is read in January. A host whose zone cannot be read falls back to UTC, as +/// the web build does (issue #2284: a file with no capture date was dated by its modification time +/// in UTC, which put an evening photo on the next day). +#[cfg(not(target_arch = "wasm32"))] +pub fn local_civil(secs: i64) -> String { + // The zone is read once: it holds the whole rule set, so each instant still gets the offset + // that was in force then, and an import does not re-read the system database per file. + static ZONE: std::sync::OnceLock = std::sync::OnceLock::new(); + civil_in(secs, ZONE.get_or_init(jiff::tz::TimeZone::system)) +} + +/// [`local_civil`] in a given zone. `jiff::TimeZone::system` falls back to a zone that behaves +/// like UTC when the host has none to read, and an instant outside the range it can represent +/// (far outside the range of a file date) keeps its UTC reading rather than failing an import. +#[cfg(not(target_arch = "wasm32"))] +fn civil_in(secs: i64, zone: &jiff::tz::TimeZone) -> String { + let offset = jiff::Timestamp::from_second(secs).map(|t| i64::from(zone.to_offset_info(t).offset().seconds())).unwrap_or(0); + civil(secs.saturating_add(offset)) +} + +#[cfg(target_arch = "wasm32")] +pub fn local_civil(secs: i64) -> String { + civil(secs) +} + +/// The instant at which the host's zone reads `civil` (`YYYY-MM-DDTHH:MM:SS`): the inverse of +/// [`local_civil`]. `None` when the text is not a civil date-time, or when the host's zone has no +/// such moment (the hour a spring-forward skips). Tests pin a file's modification time with it, so +/// that an undated file takes the local date they mean whatever zone the machine is in. +#[cfg(not(target_arch = "wasm32"))] +pub fn local_instant(civil: &str) -> Option { + let when: jiff::civil::DateTime = civil.trim().parse().ok()?; + when.to_zoned(jiff::tz::TimeZone::system()).ok().map(|z| z.timestamp().as_second()) +} + +#[cfg(target_arch = "wasm32")] +pub fn local_instant(civil: &str) -> Option { + lightcraft_catalog::dates::iso_seconds(civil) +} + impl Session { /// A thumbnail for an import candidate (not in the catalog): a raw's embedded preview, else a /// small render of the file. `slot` makes the job's photo id unique (`u64::MAX - slot`). @@ -1585,6 +1629,61 @@ impl Session { #[path = "import/tests_known.rs"] mod tests_known; +#[cfg(test)] +mod clock_tests { + use super::*; + + /// Issue #2284: an instant becomes the civil time the host's own zone shows for it, with the + /// offset in force *then*. The reported case is a JPEG with no capture date whose modification + /// time is 2026-10-18 20:15:11 in UTC-5: dated by `civil` it read 01:15 the next day. + #[test] + fn local_civil_reads_the_hosts_zone_at_that_instant() { + let zone = jiff::tz::TimeZone::system(); + // the report, then one in each half of the year (so a zone with DST uses both offsets), + // one on a year boundary, and one before the epoch + for iso in ["2026-10-19T01:15:11Z", "2026-07-04T12:00:00Z", "2026-01-15T23:30:00Z", "2027-01-01T00:00:00Z", "1969-12-31T23:59:59Z"] { + let secs = lightcraft_meta::DateTime::parse_iso(iso).expect("a test timestamp").unix_seconds(); + let zoned = jiff::Timestamp::from_second(secs).expect("in range").to_zoned(zone.clone()); + assert_eq!(local_civil(secs), zoned.strftime("%Y-%m-%dT%H:%M:%S").to_string(), "{iso}"); + } + } + + /// The reported numbers, against a fixed UTC-5 zone, so they are checked on any host: a CI + /// runner set to UTC would make the test above agree with itself and prove nothing. + #[test] + fn an_evening_in_utc_minus_5_stays_that_evening() { + let secs = lightcraft_meta::DateTime::parse_iso("2026-10-19T01:15:11Z").expect("a test timestamp").unix_seconds(); + assert_eq!(civil(secs), "2026-10-19T01:15:11", "the UTC reading the file used to be dated by"); + let zone = jiff::tz::TimeZone::fixed(jiff::tz::offset(-5)); + assert_eq!(civil_in(secs, &zone), "2026-10-18T20:15:11"); + // and the other way, so the sign cannot be read backwards + assert_eq!(civil_in(secs, &jiff::tz::TimeZone::fixed(jiff::tz::offset(2))), "2026-10-19T03:15:11"); + } + + /// `local_instant` undoes `local_civil`, so a fixture can pin a file to a local date. + #[test] + fn local_instant_is_the_inverse_of_local_civil() { + for iso in ["2026-01-14T05:58:48", "2026-07-04T12:00:00", "2026-10-18T20:15:11"] { + let secs = local_instant(iso).expect("a local instant"); + assert_eq!(local_civil(secs), iso); + } + assert_eq!(local_instant("not a date"), None); + } + + /// Whatever the host's zone, what it produces is a date the rest of the catalog can read back, + /// and it differs from UTC only by a whole number of minutes no larger than a real offset. + #[test] + fn local_civil_is_a_catalog_date_within_a_real_offset_of_utc() { + let secs = lightcraft_meta::DateTime::parse_iso("2026-10-19T01:15:11Z").expect("a test timestamp").unix_seconds(); + let local = local_civil(secs); + let read = lightcraft_catalog::dates::iso_seconds(&local).expect("a date the catalog can read"); + let shift = read - secs; + assert_eq!(shift % 60, 0, "{local}"); + assert!(shift.abs() <= 18 * 3600, "{local}"); + assert_eq!(lightcraft_catalog::dates::normalize_iso(&local).as_deref(), Some(local.as_str())); + } +} + #[cfg(test)] mod prepared_tests { use super::*; diff --git a/crates/engine/src/tests_import.rs b/crates/engine/src/tests_import.rs index 13f02ce778..07b8634ad1 100644 --- a/crates/engine/src/tests_import.rs +++ b/crates/engine/src/tests_import.rs @@ -13,6 +13,9 @@ fn temp_dir(tag: &str) -> std::path::PathBuf { d } +/// The clock these tests run on; an undated file is pinned to it too (see `write_png`). +const CLOCK: &str = "2026-01-14T05:58:48"; + /// A small procedural PNG (distinct per seed). fn write_png(path: &Path, seed: u8) { let (w, h) = (48usize, 32usize); @@ -21,8 +24,10 @@ fn write_png(path: &Path, seed: u8) { let bytes = lightcraft_codecs::encode_png(&lightcraft_codecs::EncodeImage::rgba8(&img), &lightcraft_codecs::EncodeMeta::default()).unwrap(); std::fs::create_dir_all(path.parent().unwrap()).unwrap(); std::fs::write(path, bytes).unwrap(); - // undated files take the file's date: the tests' clock time (2026-01-14T05:58:48) - let t = std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_768_370_328); + // Undated files take the file's date, read in the host's zone (issue #2284), so the file is + // pinned to the instant this machine reads as the tests' clock time rather than to a fixed one. + let secs = crate::import::local_instant(CLOCK).and_then(|s| u64::try_from(s).ok()).unwrap_or(0); + let t = std::time::UNIX_EPOCH + std::time::Duration::from_secs(secs); std::fs::File::options().write(true).open(path).unwrap().set_modified(t).unwrap(); } diff --git a/crates/engine/src/tests_import_move.rs b/crates/engine/src/tests_import_move.rs index ce249df161..2b328c5bc6 100644 --- a/crates/engine/src/tests_import_move.rs +++ b/crates/engine/src/tests_import_move.rs @@ -15,6 +15,9 @@ fn temp_dir(tag: &str) -> PathBuf { d } +/// The clock these tests run on; an undated file is pinned to it too (see `write_png`). +const CLOCK: &str = "2026-01-14T05:58:48"; + fn write_png(path: &Path, seed: u8) { let (w, h) = (40usize, 24usize); let data: Vec<[u8; 4]> = (0..w * h).map(|i| [(i % w * 5) as u8, (i / w * 7) as u8, seed, 255]).collect(); @@ -22,8 +25,10 @@ fn write_png(path: &Path, seed: u8) { let bytes = lightcraft_codecs::encode_png(&lightcraft_codecs::EncodeImage::rgba8(&img), &lightcraft_codecs::EncodeMeta::default()).unwrap(); std::fs::create_dir_all(path.parent().unwrap()).unwrap(); std::fs::write(path, bytes).unwrap(); - // undated files take the file's date: the tests' clock time (2026-01-14T05:58:48) - let t = std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_768_370_328); + // Undated files take the file's date, read in the host's zone (issue #2284), so the file is + // pinned to the instant this machine reads as the tests' clock time rather than to a fixed one. + let secs = crate::import::local_instant(CLOCK).and_then(|s| u64::try_from(s).ok()).unwrap_or(0); + let t = std::time::UNIX_EPOCH + std::time::Duration::from_secs(secs); std::fs::File::options().write(true).open(path).unwrap().set_modified(t).unwrap(); } diff --git a/crates/ui-egui/src/headless.rs b/crates/ui-egui/src/headless.rs index 1d8b584010..173c4f2fb8 100644 --- a/crates/ui-egui/src/headless.rs +++ b/crates/ui-egui/src/headless.rs @@ -3862,6 +3862,12 @@ mod tests { let _ = std::fs::remove_dir_all(&base); } + /// The instant this machine reads as the tests' clock time: an undated file takes the file's + /// date in the host's zone, so a fixed epoch would file it on the day before west of UTC. + fn filed_at() -> u64 { + lightcraft_engine::import::local_instant("2026-01-14T05:58:48").and_then(|s| u64::try_from(s).ok()).unwrap_or(0) + } + /// Import review, Copy, Folders → Custom template…: the default `{date:%Y}/{date:%Y%m%d}` /// files copies under `2026/20260114/`, the example line shows the full destination, and a /// template that would climb out of the destination is refused. @@ -3874,13 +3880,10 @@ mod tests { let img = lightcraft_raster::Rgba8 { width: 8, height: 8, data: vec![[40, 3, 9, 255]; 64] }; let png = lightcraft_codecs::encode_png(&lightcraft_codecs::EncodeImage::rgba8(&img), &lightcraft_codecs::EncodeMeta::default()).unwrap(); std::fs::write(src.join("IMG_01.png"), png).unwrap(); - // undated files are filed by the file's date (2026-01-14T05:58:48) - std::fs::File::options() - .write(true) - .open(src.join("IMG_01.png")) - .unwrap() - .set_modified(std::time::UNIX_EPOCH + Duration::from_secs(1_768_370_328)) - .unwrap(); + // Undated files are filed by the file's date, read in the host's zone (issue #2284), so + // the file is pinned to the instant this machine reads as 2026-01-14T05:58:48. + let when = std::time::UNIX_EPOCH + Duration::from_secs(filed_at()); + std::fs::File::options().write(true).open(src.join("IMG_01.png")).unwrap().set_modified(when).unwrap(); let dest_s = dest.to_string_lossy().to_string(); let services = crate::Services { png: None, pick_folder: Some(Box::new(move || Some(dest_s.clone()))), ..Default::default() }; let mut session = lightcraft_engine::Session::with_demo().with_fs(); @@ -3933,12 +3936,8 @@ mod tests { let img = lightcraft_raster::Rgba8 { width: 8, height: 8, data: vec![[40 + i, 3, 9, 255]; 64] }; let png = lightcraft_codecs::encode_png(&lightcraft_codecs::EncodeImage::rgba8(&img), &lightcraft_codecs::EncodeMeta::default()).unwrap(); std::fs::write(src.join(format!("IMG_0{i}.png")), png).unwrap(); - std::fs::File::options() - .write(true) - .open(src.join(format!("IMG_0{i}.png"))) - .unwrap() - .set_modified(std::time::UNIX_EPOCH + Duration::from_secs(1_768_370_328)) - .unwrap(); + let when = std::time::UNIX_EPOCH + Duration::from_secs(filed_at()); + std::fs::File::options().write(true).open(src.join(format!("IMG_0{i}.png"))).unwrap().set_modified(when).unwrap(); } let dest_s = dest.to_string_lossy().to_string(); let services = crate::Services { png: None, pick_folder: Some(Box::new(move || Some(dest_s.clone()))), ..Default::default() }; From 8a3d51b78bd5dc979ffcb43ccacf13cac87a168c Mon Sep 17 00:00:00 2001 From: tecari0 <121329615+tecari0@users.noreply.github.com> Date: Mon, 19 Oct 2026 20:10:01 -0700 Subject: [PATCH 2/4] #2284: local_instant answers nothing for an hour a spring-forward removed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The doc comment claimed it, the code did not do it: jiff's ordinary civil → instant conversion resolves a gap by moving past it, so 02:30 on a spring-forward morning came back as the instant of 03:30 — a time that reads back as something else, which is no inverse at all. It now asks the zone first and answers `None` for a gap. An hour a fall-back repeats keeps the first of the two, which is what the conversion already did and is now written down. The zone lookup moves into `system_zone`, and `instant_in` takes the zone so the gap and the fold can be tested against America/New_York instead of whatever zone the machine is in. Reported by CodeRabbit on PR #2381. Co-Authored-By: Claude Opus 5 --- crates/engine/src/import.rs | 55 ++++++++++++++++++++++++++++++++----- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/crates/engine/src/import.rs b/crates/engine/src/import.rs index 7e26027cf6..8013756791 100644 --- a/crates/engine/src/import.rs +++ b/crates/engine/src/import.rs @@ -1537,10 +1537,15 @@ pub fn civil(secs: i64) -> String { /// in UTC, which put an evening photo on the next day). #[cfg(not(target_arch = "wasm32"))] pub fn local_civil(secs: i64) -> String { - // The zone is read once: it holds the whole rule set, so each instant still gets the offset - // that was in force then, and an import does not re-read the system database per file. + civil_in(secs, system_zone()) +} + +/// The host's zone, read once: it holds the whole rule set, so each instant still gets the offset +/// that was in force then, and an import does not re-read the system database per file. +#[cfg(not(target_arch = "wasm32"))] +fn system_zone() -> &'static jiff::tz::TimeZone { static ZONE: std::sync::OnceLock = std::sync::OnceLock::new(); - civil_in(secs, ZONE.get_or_init(jiff::tz::TimeZone::system)) + ZONE.get_or_init(jiff::tz::TimeZone::system) } /// [`local_civil`] in a given zone. `jiff::TimeZone::system` falls back to a zone that behaves @@ -1558,13 +1563,26 @@ pub fn local_civil(secs: i64) -> String { } /// The instant at which the host's zone reads `civil` (`YYYY-MM-DDTHH:MM:SS`): the inverse of -/// [`local_civil`]. `None` when the text is not a civil date-time, or when the host's zone has no -/// such moment (the hour a spring-forward skips). Tests pin a file's modification time with it, so -/// that an undated file takes the local date they mean whatever zone the machine is in. +/// [`local_civil`]. Tests pin a file's modification time with it, so that an undated file takes +/// the local date they mean whatever zone the machine is in. #[cfg(not(target_arch = "wasm32"))] pub fn local_instant(civil: &str) -> Option { + instant_in(civil, system_zone()) +} + +/// [`local_instant`] in a given zone. `None` when the text is not a civil date-time, or when the +/// zone has no such moment: an hour a spring-forward removes is not a time anything happened at, +/// and jiff's ordinary conversion would answer with the instant an hour later, which reads back as +/// a different time and so is not an inverse at all. An hour a fall-back repeats takes the first +/// of the two. +#[cfg(not(target_arch = "wasm32"))] +fn instant_in(civil: &str, zone: &jiff::tz::TimeZone) -> Option { let when: jiff::civil::DateTime = civil.trim().parse().ok()?; - when.to_zoned(jiff::tz::TimeZone::system()).ok().map(|z| z.timestamp().as_second()) + let ambiguous = zone.to_ambiguous_zoned(when); + match ambiguous.offset() { + jiff::tz::AmbiguousOffset::Gap { .. } => None, + _ => ambiguous.earlier().ok().map(|z| z.timestamp().as_second()), + } } #[cfg(target_arch = "wasm32")] @@ -1670,6 +1688,29 @@ mod clock_tests { assert_eq!(local_instant("not a date"), None); } + /// The hour a spring-forward removes is no instant at all, so there is nothing to answer with: + /// jiff's ordinary conversion returns the hour after the gap, which would pin a file to a time + /// it does not read back as. The hour a fall-back repeats takes the first of the two. + #[test] + fn a_local_time_a_spring_forward_removed_has_no_instant() { + let Ok(zone) = jiff::tz::TimeZone::get("America/New_York") else { + lightcraft_testkit::skip!(System, "no IANA time zone database to read America/New_York from"); + return; + }; + // 2026-03-08: 02:00 becomes 03:00, so nothing happened at 02:30 + assert_eq!(instant_in("2026-03-08T02:30:00", &zone), None); + // the hours either side of the gap are ordinary, and round-trip + for iso in ["2026-03-08T01:30:00", "2026-03-08T03:30:00"] { + let secs = instant_in(iso, &zone).expect("either side of the gap"); + assert_eq!(civil_in(secs, &zone), iso); + } + // 2026-11-01: 02:00 becomes 01:00, so 01:30 happens twice; the first is the one in summer + // time, 05:30 UTC, and it reads back as what was asked for + let fold = instant_in("2026-11-01T01:30:00", &zone).expect("the repeated hour"); + assert_eq!(fold, lightcraft_meta::DateTime::parse_iso("2026-11-01T05:30:00Z").expect("a test timestamp").unix_seconds()); + assert_eq!(civil_in(fold, &zone), "2026-11-01T01:30:00"); + } + /// Whatever the host's zone, what it produces is a date the rest of the catalog can read back, /// and it differs from UTC only by a whole number of minutes no larger than a real offset. #[test] From 5a141b43a5760c1c691fea7a641cb09fb80adf5c Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Thu, 26 Nov 2026 22:31:36 +0700 Subject: [PATCH 3/4] Format import/job.rs as main's gate requires (rustfmt) The #1995 merge left crates/engine/src/import/job.rs unformatted on main, so the gate fails at fmt for every branch; this is that file run through rustfmt, nothing else. --- crates/engine/src/import/job.rs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/crates/engine/src/import/job.rs b/crates/engine/src/import/job.rs index 7ff684a75a..1d6761531b 100644 --- a/crates/engine/src/import/job.rs +++ b/crates/engine/src/import/job.rs @@ -398,15 +398,13 @@ impl ImportJob { let external_sidecar = if skip_sidecar { None } else { - found_sidecar - .and_then(|f| std::fs::read_to_string(f).ok()) - .and_then(|x| match crate::sidecar::parse_sidecar(&x, target) { - Ok(sc) => Some(sc), - Err(e) => { - log::warn!("import {path}: XMP: {e}"); - None - } - }) + found_sidecar.and_then(|f| std::fs::read_to_string(f).ok()).and_then(|x| match crate::sidecar::parse_sidecar(&x, target) { + Ok(sc) => Some(sc), + Err(e) => { + log::warn!("import {path}: XMP: {e}"); + None + } + }) }; let sidecar = match (embedded_sidecar, external_sidecar) { (Some(mut embedded), Some(external)) => { From 5860acd5aa8922494fae015003276733c41a2061 Mon Sep 17 00:00:00 2001 From: FredApps <153173854+FredApps@users.noreply.github.com> Date: Thu, 26 Nov 2026 16:46:58 +0100 Subject: [PATCH 4/4] Clock: follow a host zone change (jiff's 5-minute system-zone cache), gate the clock tests to native (CodeRabbit on #2381) --- crates/engine/src/import/commit.rs | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/crates/engine/src/import/commit.rs b/crates/engine/src/import/commit.rs index d8e28eb361..bcef685b4c 100644 --- a/crates/engine/src/import/commit.rs +++ b/crates/engine/src/import/commit.rs @@ -410,15 +410,17 @@ pub fn civil(secs: i64) -> String { /// in UTC, which put an evening photo on the next day). #[cfg(not(target_arch = "wasm32"))] pub fn local_civil(secs: i64) -> String { - civil_in(secs, system_zone()) + civil_in(secs, &system_zone()) } -/// The host's zone, read once: it holds the whole rule set, so each instant still gets the offset -/// that was in force then, and an import does not re-read the system database per file. +/// The host's zone. It holds the whole rule set, so each instant still gets the offset that was in +/// force then. jiff keeps the zone it read for five minutes (a cheap shared handle, so an import +/// does not re-read the system database per file) and reads it again after that, so a host whose +/// zone is changed while LightCraft runs dates its files in the new zone from then on, not in the +/// one it started in. #[cfg(not(target_arch = "wasm32"))] -fn system_zone() -> &'static jiff::tz::TimeZone { - static ZONE: std::sync::OnceLock = std::sync::OnceLock::new(); - ZONE.get_or_init(jiff::tz::TimeZone::system) +fn system_zone() -> jiff::tz::TimeZone { + jiff::tz::TimeZone::system() } /// [`local_civil`] in a given zone. `jiff::TimeZone::system` falls back to a zone that behaves @@ -440,7 +442,7 @@ pub fn local_civil(secs: i64) -> String { /// the local date they mean whatever zone the machine is in. #[cfg(not(target_arch = "wasm32"))] pub fn local_instant(civil: &str) -> Option { - instant_in(civil, system_zone()) + instant_in(civil, &system_zone()) } /// [`local_instant`] in a given zone. `None` when the text is not a civil date-time, or when the @@ -463,7 +465,8 @@ pub fn local_instant(civil: &str) -> Option { lightcraft_catalog::dates::iso_seconds(civil) } -#[cfg(test)] +// (the clock is the host's only natively: the web build's is UTC, and has no jiff) +#[cfg(all(test, not(target_arch = "wasm32")))] mod clock_tests { use super::*;