Skip to content

Commit 905a18a

Browse files
committed
unix,windows: Implement mp_hal_time_ns using gettimeofday.
This provides microsecond accuracy. Signed-off-by: Damien George <[email protected]>
1 parent 98182a9 commit 905a18a

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

ports/unix/unix_mphal.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ mp_uint_t mp_hal_ticks_us(void) {
216216
}
217217

218218
uint64_t mp_hal_time_ns(void) {
219-
time_t now = time(NULL);
220-
return (uint64_t)now * 1000000000ULL;
219+
struct timeval tv;
220+
gettimeofday(&tv, NULL);
221+
return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL;
221222
}

ports/windows/windows_mphal.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,3 +255,9 @@ mp_uint_t mp_hal_ticks_cpu(void) {
255255
return value.LowPart;
256256
#endif
257257
}
258+
259+
uint64_t mp_hal_time_ns(void) {
260+
struct timeval tv;
261+
gettimeofday(&tv, NULL);
262+
return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL;
263+
}

0 commit comments

Comments
 (0)