diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6702033 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +version.h diff --git a/Time.cpp b/Time.cpp index 5ca1327..7a7770e 100644 --- a/Time.cpp +++ b/Time.cpp @@ -237,6 +237,7 @@ time_t makeTime(tmElements_t &tm){ static uint32_t sysTime = 0; static uint32_t prevMillis = 0; static uint32_t nextSyncTime = 0; +static uint32_t lastSyncTime = 0; static timeStatus_t Status = timeNotSet; getExternalTime getTimePtr; // pointer to external sync function @@ -248,9 +249,9 @@ time_t sysUnsyncedTime = 0; // the time sysTime unadjusted by sync time_t now() { - // calculate number of seconds passed since last call to now() + // calculate number of seconds passed since last call to now() while (millis() - prevMillis >= 1000) { - // millis() and prevMillis are both unsigned ints thus the subtraction will always be the absolute value of the difference + // millis() and prevMillis are both unsigned ints thus the subtraction will always be the absolute value of the difference sysTime++; prevMillis += 1000; #ifdef TIME_DRIFT_INFO @@ -262,9 +263,13 @@ time_t now() { time_t t = getTimePtr(); if (t != 0) { setTime(t); + } else if(timeNotSet == Status) { + // getTimePtr returned 0 and time has not yet been set, i.e. this was not + // a temporarily failure, we have not been able to sync at all. + // Do nothing so a new attempt to sync will be made next time now() is called. } else { nextSyncTime = sysTime + syncInterval; - Status = (Status == timeNotSet) ? timeNotSet : timeNeedsSync; + Status = timeNeedsSync; } } } @@ -277,7 +282,8 @@ void setTime(time_t t) { sysUnsyncedTime = t; // store the time of the first call to set a valid Time #endif - sysTime = (uint32_t)t; + sysTime = (uint32_t)t; + lastSyncTime = (uint32_t)t; nextSyncTime = (uint32_t)t + syncInterval; Status = timeSet; prevMillis = millis(); // restart counting from now (thanks to Korman for this fix) @@ -319,3 +325,7 @@ void setSyncInterval(time_t interval){ // set the number of seconds between re-s syncInterval = (uint32_t)interval; nextSyncTime = sysTime + syncInterval; } + +time_t getLastSyncTime(){ + return (time_t)lastSyncTime; +} \ No newline at end of file diff --git a/TimeLib.h b/TimeLib.h index ddb1668..e74832e 100644 --- a/TimeLib.h +++ b/TimeLib.h @@ -15,7 +15,7 @@ #ifndef __AVR__ #include // for __time_t_defined, but avr libc lacks sys/types.h #endif - +#include "version.h" #if !defined(__time_t_defined) // avoid conflict with newlib or other posix libc typedef unsigned long time_t; @@ -133,6 +133,7 @@ char* dayShortStr(uint8_t day); timeStatus_t timeStatus(); // indicates if time has been set and recently synchronized void setSyncProvider( getExternalTime getTimeFunction); // identify the external time provider void setSyncInterval(time_t interval); // set the number of seconds between re-sync +time_t getLastSyncTime(); // get time when last successful sync was made /* low level functions to convert to and from system time */ void breakTime(time_t time, tmElements_t &tm); // break time_t into elements