diff --git a/src/main.c b/src/main.c index 08957bf..72173c6 100644 --- a/src/main.c +++ b/src/main.c @@ -1,3 +1,4 @@ +#define _XOPEN_SOURCE #include #include #include @@ -67,10 +68,22 @@ static void parse_preamble(struct parser *p) { int section = -1; uint32_t ch; char date[256]; - time_t now; - time(&now); - struct tm *now_tm = localtime(&now); - strftime(date, sizeof(date), "%F", now_tm); + char *source_date_epoch = getenv("SOURCE_DATE_EPOCH"); + if (source_date_epoch != NULL) { + struct tm source_date_epoch_tm; + char *ret = strptime(source_date_epoch, "%s", &source_date_epoch_tm); + if (ret == NULL || *ret != '\0') { + fprintf(stderr, + "Error: $SOURCE_DATE_EPOCH is set but malformed.\n"); + exit(1); + } + strftime(date, sizeof(date), "%F", &source_date_epoch_tm); + } else { + time_t now; + time(&now); + struct tm *now_tm = localtime(&now); + strftime(date, sizeof(date), "%F", now_tm); + } while ((ch = parser_getch(p)) != UTF8_INVALID) { if ((ch < 0x80 && isalnum(ch)) || ch == '_' || ch == '-' || ch == '.') { int ret = str_append_ch(name, ch); diff --git a/test/preamble b/test/preamble index 03e2d0c..e46dc32 100755 --- a/test/preamble +++ b/test/preamble @@ -37,6 +37,9 @@ test(8) EOF end 0 +# Make sure SOURCE_DATE_EPOCH is not set for the next tests +unset SOURCE_DATE_EPOCH + begin "Writes the appropriate header" scdoc </dev/null test(8) @@ -66,3 +69,11 @@ scdoc </d test-manual(8) "" "Header" EOF end 0 + +export SOURCE_DATE_EPOCH=$(date --date="2017-12-09 23:18:57 -0500" +'%s') + +begin "Supports \$SOURCE_DATE_EPOCH" +scdoc </dev/null +reproducible-manual(8) +EOF +end 0