#include <assert.h>
#include <ctype.h>
#include <errno.h>
+#include <limits.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
time_for_ttx(void)
{
time_t now;
+ long long epochll;
struct tm *timeptr;
- char *timestr;
+ char *epochstr, *endptr, *timestr;
/* Work out what timestamp to use. */
- now = time(NULL);
- if (now == (time_t)-1) {
- fprintf(stderr, "Can't get current time\n");
- return NULL;
+ if ((epochstr = getenv("SOURCE_DATE_EPOCH")) != NULL) {
+ /*
+ * Assume that SOURCE_DATE_EPOCH is set only on
+ * systems where time_t is also in seconds since the
+ * epoch.
+ */
+ epochll = strtoll(epochstr, &endptr, 10);
+ if (endptr == epochstr || *endptr != '\0' ||
+ epochll == LLONG_MAX) {
+ fprintf(stderr, "Invalid SOURCE_DATE_EPOCH\n");
+ return NULL;
+ }
+ /*
+ * I can't find a way to range-check this assignment
+ * in standard C or even in POSIX.
+ */
+ now = epochll;
+ } else {
+ now = time(NULL);
+ if (now == (time_t)-1) {
+ fprintf(stderr, "Can't get current time\n");
+ return NULL;
+ }
}
timeptr = gmtime(&now);
if (timeptr == NULL) {