]> git.kaiwu.me - quickjs.git/commitdiff
added os.now()
authorFabrice Bellard <fabrice@bellard.org>
Wed, 27 Dec 2023 18:09:29 +0000 (19:09 +0100)
committerFabrice Bellard <fabrice@bellard.org>
Wed, 27 Dec 2023 18:09:29 +0000 (19:09 +0100)
doc/quickjs.texi
quickjs-libc.c
quickjs.c
tests/microbench.js

index 9eb6354b3b1067c4c422aa4fad7737dbc7431196..5731f04bb5b573300e59cf7563004ed96027d626 100644 (file)
@@ -769,6 +769,11 @@ write_fd]} or null in case of error.
 @item sleep(delay_ms)
 Sleep during @code{delay_ms} milliseconds.
 
+@item now()
+Return a timestamp in milliseconds with more precision than
+@code{Date.now()}. The time origin is unspecified and is normally not
+impacted by system clock adjustments.
+
 @item setTimeout(func, delay)
 Call the function @code{func} after @code{delay} ms. Return a handle
 to the timer.
index f91631421391d5cc8a8109ec388d3dd6507737af..d99bbf403c1f89945d756ea8f871f146aef51b0f 100644 (file)
@@ -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 ),
index a95340dc4a2f9a58883cd6f2854a436b3e6ebfd6..719fde128b159a06a1fa6fa4dba8450732018ee0 100644 (file)
--- a/quickjs.c
+++ b/quickjs.c
@@ -42791,30 +42791,6 @@ static const JSCFunctionListEntry js_math_obj[] = {
 
 /* Date */
 
-#if 0
-/* OS dependent: return the UTC time in ms since 1970. */
-static JSValue js___date_now(JSContext *ctx, JSValueConst this_val,
-                             int argc, JSValueConst *argv)
-{
-    int64_t d;
-    struct timeval tv;
-    gettimeofday(&tv, NULL);
-    d = (int64_t)tv.tv_sec * 1000 + (tv.tv_usec / 1000);
-    return JS_NewInt64(ctx, d);
-}
-#endif
-
-/* OS dependent: return the UTC time in microseconds since 1970. */
-static JSValue js___date_clock(JSContext *ctx, JSValueConst this_val,
-                               int argc, JSValueConst *argv)
-{
-    int64_t d;
-    struct timeval tv;
-    gettimeofday(&tv, NULL);
-    d = (int64_t)tv.tv_sec * 1000000 + tv.tv_usec;
-    return JS_NewInt64(ctx, d);
-}
-
 /* OS dependent. d = argv[0] is in ms from 1970. Return the difference
    between UTC time and local time 'd' in minutes */
 static int getTimezoneOffset(int64_t time) {
@@ -48899,12 +48875,6 @@ static const JSCFunctionListEntry js_global_funcs[] = {
     JS_PROP_DOUBLE_DEF("Infinity", 1.0 / 0.0, 0 ),
     JS_PROP_DOUBLE_DEF("NaN", NAN, 0 ),
     JS_PROP_UNDEFINED_DEF("undefined", 0 ),
-
-    /* for the 'Date' implementation */
-    JS_CFUNC_DEF("__date_clock", 0, js___date_clock ),
-    //JS_CFUNC_DEF("__date_now", 0, js___date_now ),
-    //JS_CFUNC_DEF("__date_getTimezoneOffset", 1, js___date_getTimezoneOffset ),
-    //JS_CFUNC_DEF("__date_create", 3, js___date_create ),
 };
 
 /* Date */
index 1c5f52d2ea70b9b84ebf8551ad9701909e031672..c1b57bb216a8c8747d3d7f32e4b57eaff19a63f2 100644 (file)
@@ -23,6 +23,7 @@
  * THE SOFTWARE.
  */
 import * as std from "std";
+import * as os from "os";
 
 function pad(str, n) {
     str += "";
@@ -93,21 +94,12 @@ function log_line() {
     console.log(s);
 }
 
-var clocks_per_sec = 1000000;
-var max_iterations = 100;
-var clock_threshold = 2000;  /* favoring short measuring spans */
+var clocks_per_sec = 1000;
+var max_iterations = 10;
+var clock_threshold = 100;  /* favoring short measuring spans */
 var min_n_argument = 1;
-var get_clock;
-
-if (typeof globalThis.__date_clock != "function") {
-    console.log("using fallback millisecond clock");
-    clocks_per_sec = 1000;
-    max_iterations = 10;
-    clock_threshold = 100;
-    get_clock = Date.now;
-} else {
-    get_clock = globalThis.__date_clock;
-}
+//var get_clock = Date.now;
+var get_clock = os.now;
 
 function log_one(text, n, ti) {
     var ref;