Skip to content

Commit

Permalink
Fix localtime() and gmtime() with 64bit time_t
Browse files Browse the repository at this point in the history
  • Loading branch information
tiran committed Jul 19, 2022
1 parent 73c1d12 commit 9944d90
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -510,9 +510,10 @@ mergeInto(LibraryManager.library, {
return (date.getTime() / 1000)|0;
},

_gmtime_js__deps: ['$readI53FromI64'],
_gmtime_js__sig: 'ipp',
_gmtime_js: function(time, tmPtr) {
var date = new Date({{{ makeGetValue('time', 0, 'i32') }}}*1000);
var date = new Date({{{ makeGetValue('time', 0, 'i53') }}}*1000);
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_sec, 'date.getUTCSeconds()', 'i32') }}};
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_min, 'date.getUTCMinutes()', 'i32') }}};
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_hour, 'date.getUTCHours()', 'i32') }}};
Expand Down Expand Up @@ -544,9 +545,10 @@ mergeInto(LibraryManager.library, {
return (date.getTime() / 1000)|0;
},

_localtime_js__deps: ['$readI53FromI64'],
_localtime_js__sig: 'ipp',
_localtime_js: function(time, tmPtr) {
var date = new Date({{{ makeGetValue('time', 0, 'i32') }}}*1000);
var date = new Date({{{ makeGetValue('time', 0, 'i53') }}}*1000);
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_sec, 'date.getSeconds()', 'i32') }}};
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_min, 'date.getMinutes()', 'i32') }}};
{{{ makeSetValue('tmPtr', C_STRUCTS.tm.tm_hour, 'date.getHours()', 'i32') }}};
Expand Down
37 changes: 37 additions & 0 deletions tests/core/test_gmtime_localtime.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2022 The Emscripten Authors. All rights reserved.
* Emscripten is available under two separate licenses, the MIT license and the
* University of Illinois/NCSA Open Source License. Both these licenses can be
* found in the LICENSE file.
*/

#include <assert.h>
#include <time.h>
#include <stdio.h>

void
print_time(time_t time)
{
char gmbuf[32], locbuf[32];
const char fmt[] = "%Y-%m-%d %H:%M:%S";
struct tm *gm;
struct tm *loc;

gm = gmtime(&time);
assert(strftime(gmbuf, sizeof(gmbuf) - 1, fmt, gm) > 0);
loc = localtime(&time);
assert(strftime(locbuf, sizeof(locbuf) - 1, fmt, loc) > 0);
printf("time: %lld, gmtime: %s, localtime: %s\n", time, gmbuf, locbuf);
}

int main() {
// test assumes EST+05
print_time(0);
print_time(2147483647); // int8_t max, Y2K38
print_time(2147483648);
print_time(-2147483648); // int8_t min
print_time(-2147483649);
print_time(253402300799);
print_time(-62135596800 + 5 * 3600);
puts("success");
}
8 changes: 8 additions & 0 deletions tests/core/test_gmtime_localtime.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
time: 0, gmtime: 1970-01-01 00:00:00, localtime: 1969-12-31 19:00:00
time: 2147483647, gmtime: 2038-01-19 03:14:07, localtime: 2038-01-18 22:14:07
time: 2147483648, gmtime: 2038-01-19 03:14:08, localtime: 2038-01-18 22:14:08
time: -2147483648, gmtime: 1901-12-13 20:45:52, localtime: 1901-12-13 15:45:52
time: -2147483649, gmtime: 1901-12-13 20:45:51, localtime: 1901-12-13 15:45:51
time: 253402300799, gmtime: 9999-12-31 23:59:59, localtime: 9999-12-31 18:59:59
time: -62135578800, gmtime: 1-01-01 05:00:00, localtime: 1-01-01 00:00:00
success
5 changes: 5 additions & 0 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2863,6 +2863,11 @@ def test_time_c(self):
def test_gmtime(self):
self.do_core_test('test_gmtime.c')

def test_gmtime_localtime(self):
tz = 'EST+05'
with env_modify({'TZ': tz}):
self.do_core_test('test_gmtime_localtime.c')

def test_strptime_tm(self):
self.do_core_test('test_strptime_tm.c')

Expand Down

0 comments on commit 9944d90

Please sign in to comment.