Skip to content
Merged
86 changes: 86 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions crates/engine/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 12 additions & 3 deletions crates/engine/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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 {
Expand Down Expand Up @@ -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");
Expand Down
142 changes: 141 additions & 1 deletion crates/engine/src/import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
{
Expand All @@ -1528,6 +1528,68 @@ 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, 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<jiff::tz::TimeZone> = std::sync::OnceLock::new();
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`]. 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<i64> {
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<i64> {
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<i64> {
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`).
Expand Down Expand Up @@ -1585,6 +1647,84 @@ impl Session {
#[path = "import/tests_known.rs"]
mod tests_known;

#[cfg(test)]
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
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()));
}
}

#[cfg(test)]
mod prepared_tests {
use super::*;
Expand Down
9 changes: 7 additions & 2 deletions crates/engine/src/tests_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
}

Expand Down
9 changes: 7 additions & 2 deletions crates/engine/src/tests_import_move.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,20 @@ 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();
let img = lightcraft_raster::Rgba8 { width: w, height: h, data };
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();
}

Expand Down
Loading
Loading