summaryrefslogtreecommitdiff
path: root/quickjs-libc.c
diff options
context:
space:
mode:
Diffstat (limited to 'quickjs-libc.c')
-rw-r--r--quickjs-libc.c21
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 ),