summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDamien George <damien@micropython.org>2020-09-24 14:37:01 +1000
committerDamien George <damien@micropython.org>2020-10-01 14:20:42 +1000
commit905a18aafefbe04aa2beceb84885c29aa156b975 (patch)
treedc58f48c5d80334accf2f1ccbf0fa2f3ff4015bc
parent98182a97c5a9229938406beb722966eacceeb823 (diff)
unix,windows: Implement mp_hal_time_ns using gettimeofday.
This provides microsecond accuracy. Signed-off-by: Damien George <damien@micropython.org>
-rw-r--r--ports/unix/unix_mphal.c5
-rw-r--r--ports/windows/windows_mphal.c6
2 files changed, 9 insertions, 2 deletions
diff --git a/ports/unix/unix_mphal.c b/ports/unix/unix_mphal.c
index f43067f64..3fe2fa9fe 100644
--- a/ports/unix/unix_mphal.c
+++ b/ports/unix/unix_mphal.c
@@ -216,6 +216,7 @@ mp_uint_t mp_hal_ticks_us(void) {
}
uint64_t mp_hal_time_ns(void) {
- time_t now = time(NULL);
- return (uint64_t)now * 1000000000ULL;
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL;
}
diff --git a/ports/windows/windows_mphal.c b/ports/windows/windows_mphal.c
index 8ed087358..b442b5914 100644
--- a/ports/windows/windows_mphal.c
+++ b/ports/windows/windows_mphal.c
@@ -255,3 +255,9 @@ mp_uint_t mp_hal_ticks_cpu(void) {
return value.LowPart;
#endif
}
+
+uint64_t mp_hal_time_ns(void) {
+ struct timeval tv;
+ gettimeofday(&tv, NULL);
+ return (uint64_t)tv.tv_sec * 1000000000ULL + (uint64_t)tv.tv_usec * 1000ULL;
+}