summaryrefslogtreecommitdiff
path: root/Libraries/LibC/time.cpp
diff options
context:
space:
mode:
authorNico Weber <thakis@chromium.org>2020-08-22 22:15:00 -0400
committerAndreas Kling <kling@serenityos.org>2020-08-23 10:42:37 +0200
commit5f595e7e1b15791d32beb9737c3c0cf4fa7cbf38 (patch)
treed38fe617a52e1124a90fdd16009beca82b8a9415 /Libraries/LibC/time.cpp
parentfc28c9b085380240210a9b623a12232bf33c787b (diff)
downloadserenity-5f595e7e1b15791d32beb9737c3c0cf4fa7cbf38.zip
LibC: Make localtime() and gmtime() handle years before 1970
Year computation has to be based on seconds, not days, in case t is < 0 but t / __seconds_per_day is 0. Year computation also has to consider negative timestamps. With this, days is always positive and <= the number of days in the year, so base the tm_wday computation directly on the timestamp, and do it first, before t is modified in the year computation. In C, % can return a negative number if the left operand is negative, compensate for that. Tested via test-js. (Except for tm_wday, since we don't implement Date.prototype.getUTCDate() yet.)
Diffstat (limited to 'Libraries/LibC/time.cpp')
-rw-r--r--Libraries/LibC/time.cpp16
1 files changed, 12 insertions, 4 deletions
diff --git a/Libraries/LibC/time.cpp b/Libraries/LibC/time.cpp
index e06c79a6a7..6b1d0239a0 100644
--- a/Libraries/LibC/time.cpp
+++ b/Libraries/LibC/time.cpp
@@ -69,16 +69,24 @@ static const int __seconds_per_day = 60 * 60 * 24;
static void time_to_tm(struct tm* tm, time_t t)
{
+ tm->tm_wday = (4 * __seconds_per_day + t) % (7 * __seconds_per_day); // 1970-01-01 was a Thursday.
+ if (tm->tm_wday < 0)
+ tm->tm_wday += 7 * __seconds_per_day;
+ tm->tm_wday /= __seconds_per_day;
+
+ int year = 1970;
+ for (; t >= (365 + __is_leap_year(year)) * __seconds_per_day; ++year)
+ t -= (365 + __is_leap_year(year)) * __seconds_per_day;
+ for (; t < 0; --year)
+ t += (365 + __is_leap_year(year - 1)) * __seconds_per_day;
+ ASSERT(t >= 0);
+
int days = t / __seconds_per_day;
int remaining = t % __seconds_per_day;
tm->tm_sec = remaining % 60;
remaining /= 60;
tm->tm_min = remaining % 60;
tm->tm_hour = remaining / 60;
- tm->tm_wday = (4 + days) % 7;
- int year;
- for (year = 1970; days >= 365 + __is_leap_year(year); ++year)
- days -= 365 + __is_leap_year(year);
tm->tm_year = year - 1900;
tm->tm_yday = days;
tm->tm_mday = 1;