From: Maria Valentina Marin <marivalenm@gmail.com>
Date: Wed, 19 Aug 2015 17:51:02 +0000
Subject: 0035 source date epoch

Make man2html use the latest date in debian/changelog as the timestamp for its 
html output if $SOURCE_DATE_EPOCH is exported. Otherwise man2html continues with
its default behaviour of using the date of today to produce its timestamps.

Bug-Debian: https://bugs.debian.org/796130
---
 man2html/cgibase.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/man2html/cgibase.c b/man2html/cgibase.c
index b296c1d..a677473 100644
--- a/man2html/cgibase.c
+++ b/man2html/cgibase.c
@@ -8,6 +8,8 @@
 #include <ctype.h>		/* tolower() */
 #include <string.h>		/* strlen() */
 #include "defs.h"
+#include <errno.h>
+#include <limits.h>
 
 /*
  * The default is to use cgibase. With relative html style
@@ -83,11 +85,39 @@ void print_sig()
     char timebuf[TIMEBUFSZ];
     struct tm *timetm;
     time_t now;
+    char *source_date_epoch;
+    unsigned long long epoch;
+    char *endptr;
 
     timebuf[0] = 0;
 #ifdef TIMEFORMAT
     sprintf(timebuf, "Time: ");
+    source_date_epoch = getenv("SOURCE_DATE_EPOCH");
+    if (source_date_epoch) {
+		errno = 0;
+		epoch = strtoull(source_date_epoch, &endptr, 10);
+		if ((errno == ERANGE && (epoch == ULLONG_MAX || epoch == 0))
+				|| (errno != 0 && epoch == 0)) {
+			fprintf(stderr, "Environment variable $SOURCE_DATE_EPOCH: strtoull: %s\n", strerror(errno));
+			exit(EXIT_FAILURE);
+		}
+		if (endptr == source_date_epoch) {
+			fprintf(stderr, "Environment variable $SOURCE_DATE_EPOCH: No digits were found: %s\n", endptr);
+			exit(EXIT_FAILURE);
+		}
+		if (*endptr != '\0') {
+			fprintf(stderr, "Environment variable $SOURCE_DATE_EPOCH: Trailing garbage: %s\n", endptr);
+			exit(EXIT_FAILURE);
+		}
+		if (epoch > ULONG_MAX) {
+			fprintf(stderr, "Environment variable $SOURCE_DATE_EPOCH: value must be smaller than or equal to: %lu but was found to be: %llu \n", ULONG_MAX  ,epoch);
+			exit(EXIT_FAILURE);
+		}
+		now=epoch;
+	}
+	else {
     now=time(NULL);
+	}
     timetm=gmtime(&now);
     strftime(timebuf+6, TIMEBUFSZ-6, TIMEFORMAT, timetm);
     timebuf[TIMEBUFSZ-1] = 0;
