diff --git a/crates/doc/src/metadata.rs b/crates/doc/src/metadata.rs index 1a84ef219..151365c5d 100644 --- a/crates/doc/src/metadata.rs +++ b/crates/doc/src/metadata.rs @@ -105,7 +105,13 @@ fn pinned_or(get: impl Fn(&str) -> Option, clock: impl Fn() -> Option Option) -> Option { - get("SOURCE_DATE_EPOCH")?.trim().parse::().ok().filter(|t| *t >= 0) + let value = get("SOURCE_DATE_EPOCH")?; + let value = value.trim(); + // `i64::from_str` also takes a leading `+`, which `date +%s` never prints. + if value.is_empty() || !value.bytes().all(|b| b.is_ascii_digit()) { + return None; + } + value.parse::().ok() } /// The current time as Unix seconds (UTC); `None` where the platform has no clock (the web build). @@ -193,7 +199,7 @@ mod tests { assert_eq!(stamp_from_env(env(Some("1791208980"))), Some(1_791_208_980)); assert_eq!(stamp_from_env(env(Some("0"))), Some(0)); assert_eq!(stamp_from_env(env(Some(" 1791208980 "))), Some(1_791_208_980)); - for bad in ["", " ", "-1", "later", "1791208980.5", "1e9", "99999999999999999999"] { + for bad in ["", " ", "-1", "later", "1791208980.5", "1e9", "+1791208980", "+0", "99999999999999999999"] { assert_eq!(stamp_from_env(env(Some(bad))), None, "{bad:?}"); } assert_eq!(stamp_from_env(env(None)), None, "unset");