diff --git a/Cargo.lock b/Cargo.lock index eeb894a4cd..91ccd7a196 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3576,6 +3576,58 @@ version = "1.0.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" +[[package]] +name = "jiff" +version = "0.2.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "175dc66334db3b9ab9985bf8b61cfed50dc16d20964bb47f9e479adcae984a7f" +dependencies = [ + "jiff-core", + "jiff-static", + "jiff-tzdb-platform", + "log", + "portable-atomic", + "portable-atomic-util", + "serde_core", + "windows-link", +] + +[[package]] +name = "jiff-core" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e599b80292e597019b8b3e392de3f4bea2959ddfb8c7dff60e035e9e311a2c1b" +dependencies = [ + "log", +] + +[[package]] +name = "jiff-static" +version = "0.2.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c7b1af7547d47354bc438d797e3854601bde8f8d185780c5ba4ab27afa54f1ba" +dependencies = [ + "jiff-core", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "jiff-tzdb" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0980fca2cc272465bd60e82397607ba9f33add9e48defd77923301bbd03808f2" + +[[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" @@ -4130,6 +4182,7 @@ dependencies = [ "crc32fast", "fs4", "harfrust 0.13.3", + "jiff", "libc", "lightcraft-catalog", "lightcraft-codecs", diff --git a/Cargo.toml b/Cargo.toml index d358ca5c3c..99c5ee3847 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -101,6 +101,12 @@ libm = "0.2" # pure-Rust maths for lightcraft-film (target-independent results) 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 miniz_oxide = "0.9" diff --git a/crates/engine/Cargo.toml b/crates/engine/Cargo.toml index 93ad46a820..6f6e25f25e 100644 --- a/crates/engine/Cargo.toml +++ b/crates/engine/Cargo.toml @@ -52,6 +52,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 } # capture-time parsing for face recognition (native only) chrono = { workspace = true } # free space of the drive holding the preview cache (the low-disk alert; pure Rust: rustix / windows-sys) diff --git a/crates/engine/src/files/fs.rs b/crates/engine/src/files/fs.rs index bb25aff888..b547c18033 100644 --- a/crates/engine/src/files/fs.rs +++ b/crates/engine/src/files/fs.rs @@ -117,11 +117,12 @@ fn fs_load(path: &str, max_edge: usize, load: impl FnOnce(Vec) -> Result< } /// The modification time in `meta` 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 modified_iso(meta: &std::fs::Metadata) -> Option { let t = meta.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 { diff --git a/crates/engine/src/files/tests.rs b/crates/engine/src/files/tests.rs index f768e5af15..1f724fdd1c 100644 --- a/crates/engine/src/files/tests.rs +++ b/crates/engine/src/files/tests.rs @@ -469,8 +469,16 @@ fn undated_files_take_the_file_date() { 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 c90035eb6c..8136cc4db3 100644 --- a/crates/engine/src/import.rs +++ b/crates/engine/src/import.rs @@ -46,7 +46,7 @@ mod types; pub(crate) use commit::start_from_file; #[cfg(not(target_arch = "wasm32"))] pub(crate) use commit::workers; -pub use commit::{batch_size, civil, commit_prepared, system_clock}; +pub use commit::{batch_size, civil, commit_prepared, local_civil, local_instant, system_clock}; pub use defaults::{CameraDefault, ImportDefaults, apply_import_defaults, has_import_look, import_defaults}; #[cfg(test)] use files::expand_knowing; diff --git a/crates/engine/src/import/commit.rs b/crates/engine/src/import/commit.rs index b6eae27a1e..bcef685b4c 100644 --- a/crates/engine/src/import/commit.rs +++ b/crates/engine/src/import/commit.rs @@ -388,7 +388,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")] { @@ -400,3 +400,146 @@ pub fn system_clock() -> String { 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 { + civil_in(secs, &system_zone()) +} + +/// 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() -> jiff::tz::TimeZone { + 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`]. 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()?; + 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")] +pub fn local_instant(civil: &str) -> Option { + lightcraft_catalog::dates::iso_seconds(civil) +} + +// (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::*; + + /// 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); + } + + /// 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] + 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())); + } +} diff --git a/crates/engine/src/tests/import.rs b/crates/engine/src/tests/import.rs index a5aac892dc..fbd5f220c3 100644 --- a/crates/engine/src/tests/import.rs +++ b/crates/engine/src/tests/import.rs @@ -20,6 +20,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); @@ -28,8 +31,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(); } @@ -57,7 +62,8 @@ fn write_png_with_metadata(path: &Path, seed: u8, exif: Option<&[u8]>, xmp: Opti std::fs::create_dir_all(path.parent().unwrap()).unwrap(); std::fs::write(path, bytes).unwrap(); // Keep the same known file-date fallback as write_png, distinct from the regression timestamp. - let t = std::time::UNIX_EPOCH + std::time::Duration::from_secs(1_768_370_328); + 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 e35f853ba8..c4e9c52522 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"; + pub(super) 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 @@ pub(super) 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/tests.rs b/crates/ui-egui/src/headless/tests.rs index b378273003..1b0eb32fc5 100644 --- a/crates/ui-egui/src/headless/tests.rs +++ b/crates/ui-egui/src/headless/tests.rs @@ -4176,6 +4176,12 @@ fn import_copy_renames_into_destination() { 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. @@ -4188,13 +4194,10 @@ fn import_copy_into_a_custom_folder_template() { 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(); @@ -4247,12 +4250,8 @@ fn import_move_into_a_folder_template() { 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() };