Skip to content

Commit ae63f45

Browse files
committed
time: timespec_now for windows and android
1 parent 6ad89b7 commit ae63f45

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/time/time.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
#include <time.h>
1414
#include "time/timex.h"
1515

16-
// Some older systems do not support timespec_get() from time.h
17-
#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L || \
16+
// Some platforms do not support timespec_get() from time.h.
17+
#if defined(_WIN32)
18+
#include <sys/timeb.h>
19+
#elif !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L || \
1820
(!defined(TIME_UTC) && (!defined(_POSIX_TIMERS) || _POSIX_TIMERS <= 0))
1921
#include <sys/time.h>
2022
#endif
@@ -255,11 +257,21 @@ static Duration time_div(Time t, Duration d) {
255257
// timespec_now returns the current time with nanosecond precision.
256258
static struct timespec timespec_now(void) {
257259
struct timespec ts;
258-
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && defined(TIME_UTC)
259-
timespec_get(&ts, TIME_UTC);
260+
#if defined(_WIN32)
261+
// Windows.
262+
struct __timeb64 tb;
263+
_ftime64(&tb);
264+
ts.tv_sec = (time_t)tb.time;
265+
ts.tv_nsec = tb.millitm * 1000000;
260266
#elif defined(_POSIX_TIMERS) && (_POSIX_TIMERS > 0)
267+
// POSIX.
261268
clock_gettime(CLOCK_REALTIME, &ts);
269+
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && defined(TIME_UTC) && \
270+
!defined(__ANDROID__)
271+
// C11.
272+
timespec_get(&ts, TIME_UTC);
262273
#else
274+
// Fallback for older systems.
263275
struct timeval tv;
264276
gettimeofday(&tv, NULL);
265277
ts.tv_sec = tv.tv_sec;

0 commit comments

Comments
 (0)