From 8e0338c361868ffd1c708b9d76bded94e45a9840 Mon Sep 17 00:00:00 2001 From: NitrofMtl Date: Fri, 7 Aug 2026 22:25:46 -0400 Subject: [PATCH] Add POSIX support for platforms with native system clocks --- Time.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++-- TimeLib.h | 17 +++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/Time.cpp b/Time.cpp index 0dcb29f..da811df 100644 --- a/Time.cpp +++ b/Time.cpp @@ -152,6 +152,21 @@ void breakTime(time_t timeInput, tmElements_t &tm){ // break the given time_t into time components // this is a more compact version of the C library localtime function // note that year is offset from 1970 !!! +#ifdef USING_POSIX_TIME + struct tm *tm_info = gmtime(&timeInput); + if (!tm_info) + return; + + tm.Year = tm_info->tm_year -70; + tm.Month = tm_info->tm_mon + 1; + tm.Day = tm_info->tm_mday; + tm.Wday = tm_info->tm_wday + 1; + tm.Hour = tm_info->tm_hour; + tm.Minute = tm_info->tm_min; + tm.Second = tm_info->tm_sec; + return; + +#endif uint8_t year; uint8_t month, monthLength; @@ -206,6 +221,20 @@ time_t makeTime(const tmElements_t &tm){ // note year argument is offset from 1970 (see macros in time.h to convert to other formats) // previous version used full four digit year (or digits since 2000),i.e. 2009 was 2009 or 9 +#ifdef USING_POSIX_TIME + struct tm tm_info{}; + + tm_info.tm_year = tm.Year + 70; + tm_info.tm_mon = tm.Month - 1; + tm_info.tm_mday = tm.Day; + tm_info.tm_hour = tm.Hour; + tm_info.tm_min = tm.Minute; + tm_info.tm_sec = tm.Second; + + return mktime(&tm_info); + +#endif + int i; uint32_t seconds; @@ -267,11 +296,15 @@ time_t now() { Status = (Status == timeNotSet) ? timeNotSet : timeNeedsSync; } } - } + } +#ifdef USING_POSIX_TIME + return time(nullptr); +#endif return (time_t)sysTime; } -void setTime(time_t t) { +void setTime(time_t t) { + #ifdef TIME_DRIFT_INFO if(sysUnsyncedTime == 0) sysUnsyncedTime = t; // store the time of the first call to set a valid Time @@ -281,6 +314,9 @@ void setTime(time_t t) { nextSyncTime = (uint32_t)t + syncInterval; Status = timeSet; prevMillis = millis(); // restart counting from now (thanks to Korman for this fix) +#ifdef USING_POSIX_TIME + TimeLibPlatformSetTime(t); +#endif } void setTime(int hr,int min,int sec,int dy, int mnth, int yr){ @@ -300,6 +336,12 @@ void setTime(int hr,int min,int sec,int dy, int mnth, int yr){ } void adjustTime(long adjustment) { +#ifdef USING_POSIX_TIME + time_t t = time(nullptr); + t += adjustment; + set_time(t); + return; +#endif sysTime += adjustment; } diff --git a/TimeLib.h b/TimeLib.h index b587046..cce4c21 100644 --- a/TimeLib.h +++ b/TimeLib.h @@ -22,6 +22,23 @@ typedef unsigned long time_t; #endif +/* + * Use the platform system clock when POSIX time support is available. + * Platform-specific clock-setting functions are mapped here. + */ +#if defined(__NEWLIB__) +#define USING_POSIX_TIME + +#if defined(ARDUINO_ARCH_MBED) +#include +#define TimeLibPlatformSetTime(t) set_time(t) +#else +#warning "Using newlib time functions, find the right header to include set_time(t)" +#endif // ARDUINO_ARCH_MBED + +#endif //__NEWLIB__ + + // This ugly hack allows us to define C++ overloaded functions, when included // from within an extern "C", as newlib's sys/stat.h does. Actually it is // intended to include "time.h" from the C library (on ARM, but AVR does not