Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version.h
18 changes: 14 additions & 4 deletions Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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;
}
}
}
Expand All @@ -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)
Expand Down Expand Up @@ -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;
}
3 changes: 2 additions & 1 deletion TimeLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#ifndef __AVR__
#include <sys/types.h> // 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;
Expand Down Expand Up @@ -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
Expand Down