Slightly simplify SOURCE_DATE_EPOCH handling

Subtracting 30 years' worth of seconds from the date and then adding
2000 years is unnecessary when we can just add 1970 years instead.
This commit is contained in:
Ben Harris 2025-02-27 22:32:19 +00:00
parent 1ebb828fbc
commit 60136bf337

View File

@ -3155,19 +3155,18 @@ time_for_ttx(void)
if (!isdigit((unsigned char)epochstr[0]) ||
endptr == epochstr || *endptr != '\0' ||
errno == ERANGE ||
/* Limit ourselves to 2000 to 10000. */
epochumax < 946684800 || epochumax >= 253402300800) {
/* Allow years up to 9999. */
epochumax >= 253402300800) {
fprintf(stderr, "Invalid SOURCE_DATE_EPOCH\n");
return NULL;
}
epochumax -= 946684800; /* Rebase to 2000. */
tm.tm_isdst = -1;
tm.tm_sec = epochumax % 60;
tm.tm_min = epochumax / 60 % 60;
tm.tm_hour = epochumax / 3600 % 24;
long day = epochumax / 86400;
tm.tm_wday = (day - 1) % 7;
int y = 2000 + (day / 146097) * 400;
tm.tm_wday = (day + 4) % 7; /* 1970-01-01 was a Thursday. */
int y = 1970 + (day / 146097) * 400;
day = day % 146097;
bool ly;
for (;;) {