Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/time_now_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ CAMLprim value time_now_nanoseconds_since_unix_epoch_or_zero()

#else

#ifndef _MSC_VER

#include <sys/types.h>
#include <sys/time.h>

Expand All @@ -33,4 +35,36 @@ CAMLprim value time_now_nanoseconds_since_unix_epoch_or_zero()
return caml_alloc_int63(NANOS_PER_SECOND * (uint64_t)tp.tv_sec + (uint64_t)tp.tv_usec * 1000);
}

#else /* compiling with cl */

#if _MSC_VER < 1900 /* timespec_get is supported starting VS 2015 */

/*
* Note that there are fallback implementations of gettimeofday for Windows.
* E.g., see https://git.postgresql.org/gitweb/?p=postgresql.git;a=blob;f=src/port/gettimeofday.c;h=75a91993b74414c0a1c13a2a09ce739cb8aa8a08;hb=HEAD
* I am not convinced there is much value in supporting Visual Studio versions
* older than 2015 in an OCaml library, so we just croak here instead of
* falling back.
*/

#error timespec_get is only available in Visual Studio 2015 (14.0) and later
#endif

#include <time.h>

/*
* https://en.cppreference.com/w/c/chrono/timespec_get
* https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/timespec-get-timespec32-get-timespec64-get1?view=msvc-160
*/

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);
}
#endif
#endif