diff --git a/src/bin/uudoc.rs b/src/bin/uudoc.rs index c0f6643bea8..992526b2e17 100644 --- a/src/bin/uudoc.rs +++ b/src/bin/uudoc.rs @@ -23,7 +23,7 @@ use fluent_syntax::{ }, parser, }; -use jiff::Zoned; +use jiff::{Timestamp, Zoned, tz::TimeZone}; use regex::Regex; use textwrap::{fill, indent, termwidth}; use zip::ZipArchive; @@ -167,8 +167,15 @@ fn gen_manpage( // Convert to string for processing let manpage = String::from_utf8(buffer).expect("Invalid UTF-8 in manpage"); + // Use `SOURCE_DATE_EPOCH` for reproducible builds if set and valid; otherwise use the current time. + let now = std::env::var("SOURCE_DATE_EPOCH") + .ok() + .and_then(|s| s.parse().ok()) + .and_then(|s| Timestamp::new(s, 0).ok()) + .map_or_else(Zoned::now, |t| t.to_zoned(TimeZone::UTC)); + // Post-process the manpage to fix mandoc lint issues - let date = Zoned::now().strftime("%Y-%m-%d").to_string(); + let date = now.strftime("%Y-%m-%d").to_string(); let processed_manpage = post_process_manpage(manpage, &date); // Write the processed manpage to stdout diff --git a/tests/uudoc/mod.rs b/tests/uudoc/mod.rs index 4a96a532137..cf852c65794 100644 --- a/tests/uudoc/mod.rs +++ b/tests/uudoc/mod.rs @@ -174,3 +174,31 @@ fn test_markdown_header_format() { } } } + +#[test] +fn test_source_date_epoch() { + let output = get_uudoc_command() + .arg("manpage") + .arg("ls") + .env("SOURCE_DATE_EPOCH", "123456") + .output() + .expect("Failed to execute command"); + + assert!( + output.status.success(), + "Command failed with status: {}", + output.status + ); + assert!( + output.stderr.is_empty(), + "stderr should be empty but got: {}", + String::from_utf8_lossy(&output.stderr) + ); + + let output_str = String::from_utf8_lossy(&output.stdout); + let first_line = output_str + .lines() + .find(|line| line.starts_with(".TH")) + .unwrap(); + assert_eq!(first_line, ".TH LS 1 \"1970-01-02\""); +}