Skip to content

Commit d9eb2f7

Browse files
committed
Switch to 64bit timestamps in libc
1 parent 7617616 commit d9eb2f7

File tree

7 files changed

+40
-52
lines changed

7 files changed

+40
-52
lines changed

src/library.js

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -397,21 +397,6 @@ LibraryManager.library = {
397397
return ((Date.now() - _clock.start) * ({{{ cDefine('CLOCKS_PER_SEC') }}} / 1000))|0;
398398
},
399399

400-
time__sig: 'ii',
401-
time: function(ptr) {
402-
{{{ from64('ptr') }}};
403-
var ret = (Date.now()/1000)|0;
404-
if (ptr) {
405-
{{{ makeSetValue('ptr', 0, 'ret', 'i32') }}};
406-
}
407-
return ret;
408-
},
409-
410-
difftime__sig: 'dii',
411-
difftime: function(time1, time0) {
412-
return time1 - time0;
413-
},
414-
415400
_mktime_js__sig: 'ii',
416401
_mktime_js: function(tmPtr) {
417402
var date = new Date({{{ makeGetValue('tmPtr', C_STRUCTS.tm.tm_year, 'i32') }}} + 1900,
@@ -1275,7 +1260,8 @@ LibraryManager.library = {
12751260
setErrNo({{{ cDefine('EINVAL') }}});
12761261
return -1;
12771262
}
1278-
{{{ makeSetValue('tp', C_STRUCTS.timespec.tv_sec, '(now/1000)|0', 'i32') }}}; // seconds
1263+
// struct timespec { time_t tv_sec; long tv_nsec; };
1264+
{{{ makeSetValue('tp', C_STRUCTS.timespec.tv_sec, 'Math.floor(now/1000)', 'i64') }}}; // seconds
12791265
{{{ makeSetValue('tp', C_STRUCTS.timespec.tv_nsec, '((now % 1000)*1000*1000)|0', 'i32') }}}; // nanoseconds
12801266
return 0;
12811267
},
@@ -1293,16 +1279,18 @@ LibraryManager.library = {
12931279
setErrNo({{{ cDefine('EINVAL') }}});
12941280
return -1;
12951281
}
1296-
{{{ makeSetValue('res', C_STRUCTS.timespec.tv_sec, '(nsec/1000000000)|0', 'i32') }}};
1282+
// struct timespec { time_t tv_sec; long tv_nsec; };
1283+
{{{ makeSetValue('res', C_STRUCTS.timespec.tv_sec, 'Math.floor(nsec/1000000000)', 'i64') }}};
12971284
{{{ makeSetValue('res', C_STRUCTS.timespec.tv_nsec, 'nsec', 'i32') }}} // resolution is nanoseconds
12981285
return 0;
12991286
},
13001287
gettimeofday__sig: 'iii',
13011288
// http://pubs.opengroup.org/onlinepubs/000095399/basedefs/sys/time.h.html
13021289
gettimeofday: function(ptr) {
13031290
var now = Date.now();
1304-
{{{ makeSetValue('ptr', C_STRUCTS.timeval.tv_sec, '(now/1000)|0', 'i32') }}}; // seconds
1305-
{{{ makeSetValue('ptr', C_STRUCTS.timeval.tv_usec, '((now % 1000)*1000)|0', 'i32') }}}; // microseconds
1291+
// struct timeval { time_t tv_sec; suseconds_t tv_usec; };
1292+
{{{ makeSetValue('ptr', C_STRUCTS.timeval.tv_sec, 'Math.floor(now/1000)', 'i64') }}}; // seconds
1293+
{{{ makeSetValue('ptr', C_STRUCTS.timeval.tv_usec, 'Math.floor((now % 1000)*1000)', 'i64') }}}; // microseconds
13061294
return 0;
13071295
},
13081296

@@ -1312,7 +1300,8 @@ LibraryManager.library = {
13121300

13131301
ftime: function(p) {
13141302
var millis = Date.now();
1315-
{{{ makeSetValue('p', C_STRUCTS.timeb.time, '(millis/1000)|0', 'i32') }}};
1303+
// struct timeb { time_t time; unsigned short millitm; short timezone; short dstflag; };
1304+
{{{ makeSetValue('p', C_STRUCTS.timeb.time, 'Math.floor(millis/1000)', 'i64') }}};
13161305
{{{ makeSetValue('p', C_STRUCTS.timeb.millitm, 'millis % 1000', 'i16') }}};
13171306
{{{ makeSetValue('p', C_STRUCTS.timeb.timezone, '0', 'i16') }}}; // Obsolete field
13181307
{{{ makeSetValue('p', C_STRUCTS.timeb.dstflag, '0', 'i16') }}}; // Obsolete field

src/library_syscall.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ var SyscallsLibrary = {
6464
{{{ makeSetValue('buf', C_STRUCTS.stat.st_size, 'stat.size', 'i64') }}};
6565
{{{ makeSetValue('buf', C_STRUCTS.stat.st_blksize, '4096', 'i32') }}};
6666
{{{ makeSetValue('buf', C_STRUCTS.stat.st_blocks, 'stat.blocks', 'i32') }}};
67-
{{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_sec, '(stat.atime.getTime() / 1000)|0', 'i32') }}};
67+
{{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_sec, 'Math.floor(stat.atime.getTime() / 1000)', 'i64') }}};
6868
{{{ makeSetValue('buf', C_STRUCTS.stat.st_atim.tv_nsec, '0', 'i32') }}};
69-
{{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_sec, '(stat.mtime.getTime() / 1000)|0', 'i32') }}};
69+
{{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_sec, 'Math.floor(stat.mtime.getTime() / 1000)', 'i64') }}};
7070
{{{ makeSetValue('buf', C_STRUCTS.stat.st_mtim.tv_nsec, '0', 'i32') }}};
71-
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_sec, '(stat.ctime.getTime() / 1000)|0', 'i32') }}};
71+
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_sec, 'Math.floor(stat.ctime.getTime() / 1000)', 'i64') }}};
7272
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ctim.tv_nsec, '0', 'i32') }}};
7373
{{{ makeSetValue('buf', C_STRUCTS.stat.st_ino, 'stat.ino', 'i64') }}};
7474
return 0;

src/preamble_minimal.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@ function abort(what) {
1818
throw {{{ ASSERTIONS ? 'new Error(what)' : 'what' }}};
1919
}
2020

21-
#if SAFE_HEAP
2221
// Globals used by JS i64 conversions (see makeSetValue)
2322
var tempDouble;
2423
var tempI64;
25-
#endif
2624

2725
var tempRet0 = 0;
2826
var setTempRet0 = (value) => { tempRet0 = value };

system/lib/libc/musl/arch/emscripten/bits/alltypes.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,12 @@ typedef long double double_t;
7878
#endif
7979

8080
#if defined(__NEED_time_t) && !defined(__DEFINED_time_t)
81-
typedef long time_t;
81+
typedef long long time_t;
8282
#define __DEFINED_time_t
8383
#endif
8484

8585
#if defined(__NEED_suseconds_t) && !defined(__DEFINED_suseconds_t)
86-
typedef long suseconds_t;
86+
typedef long long suseconds_t;
8787
#define __DEFINED_suseconds_t
8888
#endif
8989

system/lib/libc/musl/arch/emscripten/bits/alltypes.h.in

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#define _REDIR_TIME64 1
12
#define _Addr __PTRDIFF_TYPE__
23
#define _Int64 __INT64_TYPE__
34
#define _Reg __PTRDIFF_TYPE__
@@ -14,8 +15,8 @@ TYPEDEF float float_t;
1415
TYPEDEF double double_t;
1516

1617
TYPEDEF struct { long long __ll; long double __ld; } max_align_t;
17-
TYPEDEF long time_t;
18-
TYPEDEF long suseconds_t;
18+
TYPEDEF long long time_t;
19+
TYPEDEF long long suseconds_t;
1920

2021
TYPEDEF struct { union { int __i[10]; unsigned __s[10]; } __u; } pthread_attr_t;
2122
TYPEDEF struct { union { int __i[7]; void *__p[7]; } __u; } pthread_mutex_t;

tests/reference_struct_info.json

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,30 +1392,30 @@
13921392
"sin6_scope_id": 24
13931393
},
13941394
"stat": {
1395-
"__size__": 88,
1395+
"__size__": 112,
13961396
"__st_dev_padding": 4,
13971397
"__st_ino_truncated": 8,
13981398
"__st_rdev_padding": 32,
13991399
"st_atim": {
1400-
"__size__": 8,
1401-
"tv_nsec": 60,
1400+
"__size__": 16,
1401+
"tv_nsec": 64,
14021402
"tv_sec": 56
14031403
},
14041404
"st_blksize": 48,
14051405
"st_blocks": 52,
14061406
"st_ctim": {
1407-
"__size__": 8,
1408-
"tv_nsec": 76,
1409-
"tv_sec": 72
1407+
"__size__": 16,
1408+
"tv_nsec": 96,
1409+
"tv_sec": 88
14101410
},
14111411
"st_dev": 0,
14121412
"st_gid": 24,
1413-
"st_ino": 80,
1413+
"st_ino": 104,
14141414
"st_mode": 12,
14151415
"st_mtim": {
1416-
"__size__": 8,
1417-
"tv_nsec": 68,
1418-
"tv_sec": 64
1416+
"__size__": 16,
1417+
"tv_nsec": 80,
1418+
"tv_sec": 72
14191419
},
14201420
"st_nlink": 16,
14211421
"st_rdev": 28,
@@ -1443,21 +1443,21 @@
14431443
"timeSpentInStatus": 16
14441444
},
14451445
"timeb": {
1446-
"__size__": 12,
1447-
"dstflag": 8,
1448-
"millitm": 4,
1446+
"__size__": 16,
1447+
"dstflag": 12,
1448+
"millitm": 8,
14491449
"time": 0,
1450-
"timezone": 6
1450+
"timezone": 10
14511451
},
14521452
"timespec": {
1453-
"__size__": 8,
1454-
"tv_nsec": 4,
1453+
"__size__": 16,
1454+
"tv_nsec": 8,
14551455
"tv_sec": 0
14561456
},
14571457
"timeval": {
1458-
"__size__": 8,
1458+
"__size__": 16,
14591459
"tv_sec": 0,
1460-
"tv_usec": 4
1460+
"tv_usec": 8
14611461
},
14621462
"tm": {
14631463
"__size__": 44,
@@ -1474,9 +1474,9 @@
14741474
"tm_zone": 40
14751475
},
14761476
"utimbuf": {
1477-
"__size__": 8,
1477+
"__size__": 16,
14781478
"actime": 0,
1479-
"modtime": 4
1479+
"modtime": 8
14801480
}
14811481
}
14821482
}

tools/system_libs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,8 @@ def get_files(self):
908908
'ctime.c',
909909
'gmtime.c',
910910
'localtime.c',
911+
'difftime.c',
912+
'time.c',
911913
'nanosleep.c',
912914
'clock_nanosleep.c',
913915
'ctime_r.c',
@@ -1600,13 +1602,11 @@ def get_files(self):
16001602
'__year_to_secs.c',
16011603
'clock.c',
16021604
'clock_gettime.c',
1603-
'difftime.c',
16041605
'gettimeofday.c',
16051606
'localtime_r.c',
16061607
'gmtime_r.c',
16071608
'mktime.c',
1608-
'timegm.c',
1609-
'time.c'])
1609+
'timegm.c'])
16101610
# It is more efficient to use JS for __assert_fail, as it avoids always
16111611
# including fprintf etc.
16121612
files += files_in_path(

0 commit comments

Comments
 (0)