[PATCH] Allow to override build date with SOURCE_DATE_EPOCH

Matěj Cepl <[email protected]> Wed, 30 Apr 2025 12:16:04 +0200
Newsgroups gmane.comp.type-setting.lout
Message-ID <[email protected]>
From: "Bernhard M. Wiedemann" <[email protected]>

to make builds reproducible.

See https://reproducible-builds.org/ for why this is good
and https://reproducible-builds.org/specs/source-date-epoch/
for the definition of this variable.

This patch was done while working on reproducible builds for openSUSE.

Signed-off-by: Matěj Cepl <[email protected]>
---
 z35.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/z35.c b/z35.c
index 8adbec3..57cd5a0 100644
--- a/z35.c
+++ b/z35.c
@@ -101,8 +101,13 @@ void InitTime(void)
   dst       = load(KW_DAYLIGHTSAVING, NPAR,  MomentSym);
 
   /* get current time and convert to ASCII */
-  if( time(&raw_time) == -1 )
-    Error(35, 1, "unable to obtain the current time", WARN, no_fpos);
+  char *source_date_epoch;
+  /* This assumes that the SOURCE_DATE_EPOCH environment variable will contain
+   a correct, positive integer in the time_t range */
+  if ((source_date_epoch = getenv("SOURCE_DATE_EPOCH")) == NULL ||
+    (raw_time = (time_t)strtoll(source_date_epoch, NULL, 10)) <= 0)
+      if( time(&raw_time) == -1 )
+        Error(35, 1, "unable to obtain the current time", WARN, no_fpos);
   now = localtime(&raw_time);
   StringCopy(time_string, AsciiToFull(asctime(now)));
   time_string[StringLength(time_string) - 1] = '\0';
-- 
2.49.0