From f459bc0ae4ed1f6797ae46c9426cf18f33d26018 Mon Sep 17 00:00:00 2001 From: "Aaron Tulino (Aaronjamt)" Date: Thu, 13 Nov 2025 22:57:56 -0700 Subject: [PATCH 1/2] Potential fix for `gettimeofday` for Arduino --- src/utils.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/utils.c b/src/utils.c index a4459b5..b20efcb 100644 --- a/src/utils.c +++ b/src/utils.c @@ -184,6 +184,22 @@ int add_iso8601_utc_datetime(char* buf, size_t size) { #endif +#ifdef ARDUINO + +int64_t usec_now() +{ + return micros(); +} + +void get_time(uint32_t *seconds, uint32_t *micro_seconds) +{ + *micro_seconds = micros(); + *seconds = *micro_seconds / 1000000; + *micro_seconds %= 1000000; +} + +#else + int64_t usec_now() { int64_t usec; @@ -204,6 +220,8 @@ void get_time(uint32_t *seconds, uint32_t *micro_seconds) *micro_seconds = tv.tv_usec; } +#endif + int64_t usec_since(int64_t last) { return usec_now() - last; From 1a7b7a3ebd949a12c57d145b7b49a13b3f1d540a Mon Sep 17 00:00:00 2001 From: Aaron Tulino Date: Sat, 6 Dec 2025 23:25:55 -0700 Subject: [PATCH 2/2] Follow pattern of other platforms --- src/utils.c | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) diff --git a/src/utils.c b/src/utils.c index b20efcb..f536fbd 100644 --- a/src/utils.c +++ b/src/utils.c @@ -148,7 +148,7 @@ int add_iso8601_utc_datetime(char *buf, size_t size) return strftime(buf, size, "%Y-%m-%dT%H:%M:%SZ", &timeinfo); } -#elif defined(__BARE_METAL__) +#elif defined(ARDUINO) #ifndef _TIMEVAL_DEFINED struct timeval { @@ -167,8 +167,8 @@ struct timezone { int gettimeofday(struct timeval * tp, struct timezone * tzp) { ARG_UNUSED(tzp); - tp->tv_sec = 0; - tp->tv_usec = 0; + tp->tv_sec = micros() / 1000000; + tp->tv_usec = micros() % 1000000; return 0; } @@ -178,28 +178,42 @@ int add_iso8601_utc_datetime(char* buf, size_t size) { return 0; } -#else - -#error Platform test failed +#elif defined(__BARE_METAL__) +#ifndef _TIMEVAL_DEFINED +struct timeval { + long tv_sec; // seconds since epoch + long tv_usec; // microseconds +}; #endif -#ifdef ARDUINO +#ifndef _TIMEZONE_DEFINED +struct timezone { + int tz_minuteswest; // minutes west of UTC + int tz_dsttime; // daylight saving time flag +}; +#endif -int64_t usec_now() +int gettimeofday(struct timeval * tp, struct timezone * tzp) { - return micros(); + ARG_UNUSED(tzp); + tp->tv_sec = 0; + tp->tv_usec = 0; + return 0; } -void get_time(uint32_t *seconds, uint32_t *micro_seconds) -{ - *micro_seconds = micros(); - *seconds = *micro_seconds / 1000000; - *micro_seconds %= 1000000; +int add_iso8601_utc_datetime(char* buf, size_t size) { + ARG_UNUSED(buf); + ARG_UNUSED(size); + return 0; } #else +#error Platform test failed + +#endif + int64_t usec_now() { int64_t usec; @@ -220,8 +234,6 @@ void get_time(uint32_t *seconds, uint32_t *micro_seconds) *micro_seconds = tv.tv_usec; } -#endif - int64_t usec_since(int64_t last) { return usec_now() - last;