pax_global_header00006660000000000000000000000064146164733610014525gustar00rootroot0000000000000052 comment=112f3885262b236a36c0dce1b6790c0561ded7ca time_now-0.17.0/000077500000000000000000000000001461647336100134335ustar00rootroot00000000000000time_now-0.17.0/.gitignore000066400000000000000000000000411461647336100154160ustar00rootroot00000000000000_build *.install *.merlin _opam time_now-0.17.0/.ocamlformat000066400000000000000000000000231461647336100157330ustar00rootroot00000000000000profile=janestreet time_now-0.17.0/CHANGES.md000066400000000000000000000001771461647336100150320ustar00rootroot00000000000000## Release v0.17.0 * Added `nanosecond_counter_for_timing` to support higher resolution time measurements on `js_of_ocaml`. time_now-0.17.0/CONTRIBUTING.md000066400000000000000000000044101461647336100156630ustar00rootroot00000000000000This repository contains open source software that is developed and maintained by [Jane Street][js]. Contributions to this project are welcome and should be submitted via GitHub pull requests. Signing contributions --------------------- We require that you sign your contributions. Your signature certifies that you wrote the patch or otherwise have the right to pass it on as an open-source patch. The rules are pretty simple: if you can certify the below (from [developercertificate.org][dco]): ``` Developer Certificate of Origin Version 1.1 Copyright (C) 2004, 2006 The Linux Foundation and its contributors. 1 Letterman Drive Suite D4700 San Francisco, CA, 94129 Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed. Developer's Certificate of Origin 1.1 By making a contribution to this project, I certify that: (a) The contribution was created in whole or in part by me and I have the right to submit it under the open source license indicated in the file; or (b) The contribution is based upon previous work that, to the best of my knowledge, is covered under an appropriate open source license and I have the right under that license to submit that work with modifications, whether created in whole or in part by me, under the same open source license (unless I am permitted to submit under a different license), as indicated in the file; or (c) The contribution was provided directly to me by some other person who certified (a), (b) or (c) and I have not modified it. (d) I understand and agree that this project and the contribution are public and that a record of the contribution (including all personal information I submit with it, including my sign-off) is maintained indefinitely and may be redistributed consistent with this project or the open source license(s) involved. ``` Then you just add a line to every git commit message: ``` Signed-off-by: Joe Smith ``` Use your real name (sorry, no pseudonyms or anonymous contributions.) If you set your `user.name` and `user.email` git configs, you can sign your commit automatically with git commit -s. [dco]: http://developercertificate.org/ [js]: https://opensource.janestreet.com/ time_now-0.17.0/LICENSE.md000066400000000000000000000021461461647336100150420ustar00rootroot00000000000000The MIT License Copyright (c) 2019--2024 Jane Street Group, LLC Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. time_now-0.17.0/Makefile000066400000000000000000000004031461647336100150700ustar00rootroot00000000000000INSTALL_ARGS := $(if $(PREFIX),--prefix $(PREFIX),) default: dune build install: dune install $(INSTALL_ARGS) uninstall: dune uninstall $(INSTALL_ARGS) reinstall: uninstall install clean: dune clean .PHONY: default install uninstall reinstall clean time_now-0.17.0/dune-project000066400000000000000000000000211461647336100157460ustar00rootroot00000000000000(lang dune 3.11) time_now-0.17.0/src/000077500000000000000000000000001461647336100142225ustar00rootroot00000000000000time_now-0.17.0/src/dune000066400000000000000000000005761461647336100151100ustar00rootroot00000000000000(library (foreign_stubs (language c) (names time_now_stubs) (flags -std=c11 -fPIC)) (name time_now) (public_name time_now) (libraries base jane-street-headers) (preprocess (pps ppx_base ppx_optcomp)) (preprocessor_deps config.h) (js_of_ocaml (javascript_files runtime.js))) (rule (targets config.h) (deps) (action (copy %{lib:jst-config:config.h} config.h))) time_now-0.17.0/src/runtime.js000066400000000000000000000032541461647336100162470ustar00rootroot00000000000000///////// TIME_NOW //Provides: time_now_nanoseconds_since_unix_epoch_or_zero //Requires: caml_int64_mul, caml_int64_of_float, caml_int64_of_int32 var ms_to_nano = caml_int64_of_int32(1000*1000); function time_now_nanoseconds_since_unix_epoch_or_zero(){ var ms = Date.now(); // multiple by two - int63 integers are shifted to the left var ms_i63 = caml_int64_of_float(ms*2); // doing the multiplication in int64 space would drop all the values // after the decimal point, but because [Date.now()] only provides // 1ms resolution anyway, there's never any value after the decimal point. return caml_int64_mul(ms_i63,ms_to_nano); } //Provides: ms_float_to_ns_int64 //Requires: caml_int64_of_float function ms_float_to_ns_int64(ms) { // multiply by 1_000_000 while still a float so that sub-millisecond // values don't get truncated by the float->int conversion. return caml_int64_of_float(ms * (1000.0 * 1000.0)); } /* The reason we can't share all the code between [*_counter_for_timing] and * [*_unix_epoch] is because [performance.now()] breaks when the computer goes * to sleep, and when a tab is backgrounded. * https://github.com/w3c/hr-time/issues/115 */ //Provides: time_now_nanosecond_counter_for_timing //Requires: caml_int64_add, ms_float_to_ns_int64, caml_int64_shift_left function time_now_nanosecond_counter_for_timing(){ var ms_since_program_started = performance.now(); var ns_since_program_started = ms_float_to_ns_int64(ms_since_program_started); // shift left by 1 because in javascript, an int63 is implemented as an // int64 with the least significant bit set to zero return caml_int64_shift_left(ns_since_program_started, 1); } time_now-0.17.0/src/time_now.ml000066400000000000000000000016301461647336100163750ustar00rootroot00000000000000[%%import "config.h"] open! Base [%%ifdef JSC_ARCH_SIXTYFOUR] external nanoseconds_since_unix_epoch_or_zero : unit -> Int63.t = "time_now_nanoseconds_since_unix_epoch_or_zero" [@@noalloc] external nanosecond_counter_for_timing : unit -> Int63.t = "time_now_nanosecond_counter_for_timing" [@@noalloc] [%%else] external nanoseconds_since_unix_epoch_or_zero : unit -> Int63.t = "time_now_nanoseconds_since_unix_epoch_or_zero" external nanosecond_counter_for_timing : unit -> Int63.t = "time_now_nanosecond_counter_for_timing" [%%endif] [%%ifdef JSC_POSIX_TIMERS] let[@cold] gettime_failed () = failwith "clock_gettime(CLOCK_REALTIME) failed" [%%else] let[@cold] gettime_failed () = failwith "gettimeofday failed" [%%endif] let nanoseconds_since_unix_epoch () = let t = nanoseconds_since_unix_epoch_or_zero () in if Int63.( <> ) t Int63.zero then t else gettime_failed () ;; time_now-0.17.0/src/time_now.mli000066400000000000000000000016531461647336100165530ustar00rootroot00000000000000open! Base (** Returns the number of nanoseconds that have passed since the unix epoch. In Javascript, the resolution of the timestamp returned by this function is 1ms. *) val nanoseconds_since_unix_epoch : unit -> Int63.t (** [nanosecond_counter_for_timing] returns the number of nanos since an arbitrary epoch that is fixed at program startup. On some platforms the epoch is the unix epoch, and on other platforms the epoch is when the program starts running. The resulting timestamps are useful when compared to or subtracted from other timestamps produced by the same function in the same process. This is intended for use in benchmarks. In Javascript, the resolution of the timestamp returned by this function is not formally defined, but in practice is significantly higher than the 1ms resolution produced by [nanoseconds_since_unix_epoch]. *) val nanosecond_counter_for_timing : unit -> Int63.t time_now-0.17.0/src/time_now_stubs.c000066400000000000000000000020721461647336100174300ustar00rootroot00000000000000#include "config.h" #include "ocaml_utils.h" #include #include #define NANOS_PER_SECOND 1000000000 #if defined(JSC_TIMESPEC) /* Note: this is imported noalloc if (and only if) ARCH_SIXTYFOUR is defined. * This is OK because caml_alloc_int63 doesn't actually allocate in that case. */ CAMLprim value time_now_nanoseconds_since_unix_epoch_or_zero() { struct timespec ts; if (timespec_get(&ts, TIME_UTC) != TIME_UTC) return caml_alloc_int63(0); else return caml_alloc_int63(NANOS_PER_SECOND * (uint64_t)ts.tv_sec + (uint64_t)ts.tv_nsec); } #else #include #include CAMLprim value time_now_nanoseconds_since_unix_epoch_or_zero() { struct timeval tp; if (gettimeofday(&tp, NULL) == -1) return caml_alloc_int63(0); else return caml_alloc_int63(NANOS_PER_SECOND * (uint64_t)tp.tv_sec + (uint64_t)tp.tv_usec * 1000); } #endif CAMLprim value time_now_nanosecond_counter_for_timing() { return time_now_nanoseconds_since_unix_epoch_or_zero(); } time_now-0.17.0/time_now.opam000066400000000000000000000017031461647336100161330ustar00rootroot00000000000000opam-version: "2.0" version: "v0.17.0" maintainer: "Jane Street developers" authors: ["Jane Street Group, LLC"] homepage: "https://github.com/janestreet/time_now" bug-reports: "https://github.com/janestreet/time_now/issues" dev-repo: "git+https://github.com/janestreet/time_now.git" doc: "https://ocaml.janestreet.com/ocaml-core/latest/doc/time_now/index.html" license: "MIT" build: [ ["dune" "build" "-p" name "-j" jobs] ] depends: [ "ocaml" {>= "5.1.0"} "base" {>= "v0.17" & < "v0.18"} "jane-street-headers" {>= "v0.17" & < "v0.18"} "jst-config" {>= "v0.17" & < "v0.18"} "ppx_base" {>= "v0.17" & < "v0.18"} "ppx_optcomp" {>= "v0.17" & < "v0.18"} "dune" {>= "3.11.0"} ] available: arch != "arm32" & arch != "x86_32" synopsis: "Reports the current time" description: " Provides a single function to report the current time in nanoseconds since the start of the Unix epoch. "