chiark / gitweb /
Slightly simplify SOURCE_DATE_EPOCH handling
authorBen Harris <bjh21@bjh21.me.uk>
Thu, 27 Feb 2025 22:32:19 +0000 (22:32 +0000)
committerBen Harris <bjh21@bjh21.me.uk>
Thu, 27 Feb 2025 22:32:19 +0000 (22:32 +0000)
Subtracting 30 years' worth of seconds from the date and then adding
2000 years is unnecessary when we can just add 1970 years instead.

bedstead.c

index 218523ac7b10ac8465dddc2daec10510ccfe0c77..fe87a1a89b1e0084aee4ff6c1fd7af65f127038e 100644 (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 (;;) {