Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Merge PR #2530: Return None from shift_iso for impossible calendar dates

aiongg87956cf4eac2
diff --cc crates/catalog/src/dates.rs
index 2dfebbf90,a838edd49..3635bfe33
--- a/crates/catalog/src/dates.rs
+++ b/crates/catalog/src/dates.rs
@@@ -43,13 -43,14 +43,18 @@@ pub fn weekday(iso: &str) -> Option<&'s
/// Shift an ISO time by `secs`, keeping anything after the seconds (fractions, zone) as it was.
/// A date without a time gets one. `None` if it doesn't parse, or if the result leaves years
/// 0000–9999 (an ISO date here has four year digits, so it couldn't be read back); `secs` may be
-/// anything, it never overflows.
+/// anything, it never overflows. Stricter than [`iso_seconds`], which reads what it can for sorting:
+/// a time cut short, seconds that aren't digits or anything after the zone is `None` here, not
+/// written back as a clean-looking time.
pub fn shift_iso(iso: &str, secs: i64) -> Option<String> {
+ let iso = iso.trim();
+ let tail = iso.get(tail_start(iso)?..)?;
- let t = iso_seconds(iso)?.checked_add(secs)?;
+ let base = iso_seconds(iso)?;
+ // reject impossible dates that the arithmetic silently rolls over (2026-02-30)
- if civil(base).get(..10) != iso.trim().get(..10) {
++ if civil(base).get(..10) != iso.get(..10) {
+ return None;
+ }
+ let t = base.checked_add(secs)?;
let (first, last) = (iso_seconds("0000-01-01T00:00:00")?, iso_seconds("9999-12-31T23:59:59")?);
if !(first..=last).contains(&t) {
return None;