diff options
author | Fabrice Bellard <fabrice@bellard.org> | 2023-12-27 19:09:29 +0100 |
---|---|---|
committer | Fabrice Bellard <fabrice@bellard.org> | 2023-12-27 19:09:29 +0100 |
commit | 2ee6be705fde0eb68acec25915d2947de1207abb (patch) | |
tree | 05bb2acdbf538dad3603fc3bd1bbed377ac1fd50 /quickjs-libc.c | |
parent | ffe81419fff5798b1983e30702c66d9ceb1f8a7a (diff) | |
download | quickjs-2ee6be705fde0eb68acec25915d2947de1207abb.tar.gz quickjs-2ee6be705fde0eb68acec25915d2947de1207abb.zip |
added os.now()
Diffstat (limited to 'quickjs-libc.c')
-rw-r--r-- | quickjs-libc.c | 21 |
1 files changed, 21 insertions, 0 deletions
diff --git a/quickjs-libc.c b/quickjs-libc.c index f916314..d99bbf4 100644 --- a/quickjs-libc.c +++ b/quickjs-libc.c @@ -1970,6 +1970,13 @@ static int64_t get_time_ms(void) clock_gettime(CLOCK_MONOTONIC, &ts); return (uint64_t)ts.tv_sec * 1000 + (ts.tv_nsec / 1000000); } + +static int64_t get_time_ns(void) +{ + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (uint64_t)ts.tv_sec * 1000000000 + ts.tv_nsec; +} #else /* more portable, but does not work if the date is updated */ static int64_t get_time_ms(void) @@ -1978,8 +1985,21 @@ static int64_t get_time_ms(void) gettimeofday(&tv, NULL); return (int64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000); } + +static int64_t get_time_ns(void) +{ + struct timeval tv; + gettimeofday(&tv, NULL); + return (int64_t)tv.tv_sec * 1000000000 + (tv.tv_usec * 1000); +} #endif +static JSValue js_os_now(JSContext *ctx, JSValue this_val, + int argc, JSValue *argv) +{ + return JS_NewFloat64(ctx, (double)get_time_ns() / 1e6); +} + static void unlink_timer(JSRuntime *rt, JSOSTimer *th) { if (th->link.prev) { @@ -3625,6 +3645,7 @@ static const JSCFunctionListEntry js_os_funcs[] = { OS_FLAG(SIGTTIN), OS_FLAG(SIGTTOU), #endif + JS_CFUNC_DEF("now", 0, js_os_now ), JS_CFUNC_DEF("setTimeout", 2, js_os_setTimeout ), JS_CFUNC_DEF("clearTimeout", 1, js_os_clearTimeout ), JS_PROP_STRING_DEF("platform", OS_PLATFORM, 0 ), |