Skip to content

Commit

Permalink
Switch to 64bit timestamps in libc
Browse files Browse the repository at this point in the history
  • Loading branch information
cchudant committed Mar 2, 2022
1 parent 7617616 commit 16e4d0f
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 27 deletions.
29 changes: 9 additions & 20 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -397,21 +397,6 @@ LibraryManager.library = {
return ((Date.now() - _clock.start) * ({{{ cDefine('CLOCKS_PER_SEC') }}} / 1000))|0;
},

time__sig: 'ii',
time: function(ptr) {
{{{ from64('ptr') }}};
var ret = (Date.now()/1000)|0;
if (ptr) {
{{{ makeSetValue('ptr', 0, 'ret', 'i32') }}};
}
return ret;
},

difftime__sig: 'dii',
difftime: function(time1, time0) {
return time1 - time0;
},

_mktime_js__sig: 'ii',
_mktime_js: function(tmPtr) {
var date = new Date({{{ makeGetValue('tmPtr', C_STRUCTS.tm.tm_year, 'i32') }}} + 1900,
Expand Down Expand Up @@ -1275,7 +1260,8 @@ LibraryManager.library = {
setErrNo({{{ cDefine('EINVAL') }}});
return -1;
}
{{{ makeSetValue('tp', C_STRUCTS.timespec.tv_sec, '(now/1000)|0', 'i32') }}}; // seconds
// struct timespec { time_t tv_sec; long tv_nsec; };
{{{ makeSetValue('tp', C_STRUCTS.timespec.tv_sec, '(now/1000)|0', 'i64') }}}; // seconds
{{{ makeSetValue('tp', C_STRUCTS.timespec.tv_nsec, '((now % 1000)*1000*1000)|0', 'i32') }}}; // nanoseconds
return 0;
},
Expand All @@ -1293,16 +1279,18 @@ LibraryManager.library = {
setErrNo({{{ cDefine('EINVAL') }}});
return -1;
}
{{{ makeSetValue('res', C_STRUCTS.timespec.tv_sec, '(nsec/1000000000)|0', 'i32') }}};
// struct timespec { time_t tv_sec; long tv_nsec; };
{{{ makeSetValue('res', C_STRUCTS.timespec.tv_sec, '(nsec/1000000000)|0', 'i64') }}};
{{{ makeSetValue('res', C_STRUCTS.timespec.tv_nsec, 'nsec', 'i32') }}} // resolution is nanoseconds
return 0;
},
gettimeofday__sig: 'iii',
// http://pubs.opengroup.org/onlinepubs/000095399/basedefs/sys/time.h.html
gettimeofday: function(ptr) {
var now = Date.now();
{{{ makeSetValue('ptr', C_STRUCTS.timeval.tv_sec, '(now/1000)|0', 'i32') }}}; // seconds
{{{ makeSetValue('ptr', C_STRUCTS.timeval.tv_usec, '((now % 1000)*1000)|0', 'i32') }}}; // microseconds
// struct timeval { time_t tv_sec; suseconds_t tv_usec; };
{{{ makeSetValue('ptr', C_STRUCTS.timeval.tv_sec, '(now/1000)|0', 'i64') }}}; // seconds
{{{ makeSetValue('ptr', C_STRUCTS.timeval.tv_usec, '((now % 1000)*1000)|0', 'i64') }}}; // microseconds
return 0;
},

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

ftime: function(p) {
var millis = Date.now();
{{{ makeSetValue('p', C_STRUCTS.timeb.time, '(millis/1000)|0', 'i32') }}};
// struct timeb { time_t time; unsigned short millitm; short timezone; short dstflag; };
{{{ makeSetValue('p', C_STRUCTS.timeb.time, '(millis/1000)|0', 'i64') }}};
{{{ makeSetValue('p', C_STRUCTS.timeb.millitm, 'millis % 1000', 'i16') }}};
{{{ makeSetValue('p', C_STRUCTS.timeb.timezone, '0', 'i16') }}}; // Obsolete field
{{{ makeSetValue('p', C_STRUCTS.timeb.dstflag, '0', 'i16') }}}; // Obsolete field
Expand Down
4 changes: 2 additions & 2 deletions system/lib/libc/musl/arch/emscripten/bits/alltypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ typedef long double double_t;
#endif

#if defined(__NEED_time_t) && !defined(__DEFINED_time_t)
typedef long time_t;
typedef long long time_t;
#define __DEFINED_time_t
#endif

#if defined(__NEED_suseconds_t) && !defined(__DEFINED_suseconds_t)
typedef long suseconds_t;
typedef long long suseconds_t;
#define __DEFINED_suseconds_t
#endif

Expand Down
5 changes: 3 additions & 2 deletions system/lib/libc/musl/arch/emscripten/bits/alltypes.h.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#define _REDIR_TIME64 1
#define _Addr __PTRDIFF_TYPE__
#define _Int64 __INT64_TYPE__
#define _Reg __PTRDIFF_TYPE__
Expand All @@ -14,8 +15,8 @@ TYPEDEF float float_t;
TYPEDEF double double_t;

TYPEDEF struct { long long __ll; long double __ld; } max_align_t;
TYPEDEF long time_t;
TYPEDEF long suseconds_t;
TYPEDEF long long time_t;
TYPEDEF long long suseconds_t;

TYPEDEF struct { union { int __i[10]; unsigned __s[10]; } __u; } pthread_attr_t;
TYPEDEF struct { union { int __i[7]; void *__p[7]; } __u; } pthread_mutex_t;
Expand Down
6 changes: 3 additions & 3 deletions tools/system_libs.py
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,8 @@ def get_files(self):
'ctime.c',
'gmtime.c',
'localtime.c',
'difftime.c',
'time.c',
'nanosleep.c',
'clock_nanosleep.c',
'ctime_r.c',
Expand Down Expand Up @@ -1600,13 +1602,11 @@ def get_files(self):
'__year_to_secs.c',
'clock.c',
'clock_gettime.c',
'difftime.c',
'gettimeofday.c',
'localtime_r.c',
'gmtime_r.c',
'mktime.c',
'timegm.c',
'time.c'])
'timegm.c'])
# It is more efficient to use JS for __assert_fail, as it avoids always
# including fprintf etc.
files += files_in_path(
Expand Down

0 comments on commit 16e4d0f

Please sign in to comment.