Skip to content

Commit

Permalink
EFUNs [AIX,NT]: Improved support for time beyond 2038-01-19 in mktime().
Browse files Browse the repository at this point in the history
Fixes calculation for dates past 2038-01-19T03:14:07 UTC.

The issue was caused by the C division operator rounding negative
values toward zero rather than toward negative infinity. Now the
dividend should stay positive to avoid surprises.

Note that mktime() is best-effort for timezone offsets other than 0.
  • Loading branch information
grubba committed Oct 16, 2024
1 parent 631090b commit 9505b42
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/builtin_functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -5994,11 +5994,17 @@ time_t mktime_zone(struct tm *date, int other_timezone, int tz)
* The calendar repeats every 28 years, so offset it appropriately.
* Offset years before 1971 in order to avoid issues near 1970-01-01.
*/
if ((date->tm_year <= -300) || (date->tm_year > 130)) {
if (date->tm_year <= -300) {
int y = (100 - date->tm_year)/400;
date->tm_year += y * 400;
ydelta += y * 400;
retval -= y * 12622780800LL; /* 400 years in seconds. */
} else if (date->tm_year > 130) {
/* NB: Duplicate of the above due to rounding to zero. */
int y = (date->tm_year + 300)/400;
date->tm_year -= y * 400;
ydelta -= y * 400;
retval += y * 12622780800LL; /* 400 years in seconds. */
}
if (date->tm_year <= -30) { /* Before 1870. */
int y = -date->tm_year/100;
Expand Down

0 comments on commit 9505b42

Please sign in to comment.