I am testing the proposed impementation for time adjustment, there is some code below you can run if you want to test it on your board
Edit the variable adjSeconds to a value appropriate for your board, it is the number of seconds to add or subtract each day.
int adjSecs = 120; // set this to the number of seconds to adjust per day, positive values speed up clock, negative values slow clock down
Compile and run the sketch (no library necessary).
You can see adjusted and unadjusted time updated every half second in the serial monitor (baud rate is 9600) and compare this to an accurate clock to verify that the adjusted value tracks the correct time.
The technique used here is to add the number of seconds set by adjSecs divided equally over a day. It does this by adding .001 times the value of adjSecs every 86 seconds (there are approx 86 thousand seconds per day)
I will update the DateTime library with an adjTime property (minus debug code) after I have completed testing, please let me know you find any issues.
Note that there is no handling of millis rollover in this code. I am testing using the proposed 0012 code as posted here: http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1210889307/#3
Feel free to implement your favorite technique for handling millis rollover if testing for longer than 9 hours.
// sketch to test time adjustment
int adjSeconds = 120; // set this to the number of seconds to adjust per day, positive values speed up clock, negative values slow clock down
// system variables
unsigned long prevMillis = 0;
unsigned long sysTime = 0; // increments once per second
unsigned long adjustedSysTime = 0; // increments once per second plus/minus adjsutment
unsigned long adjustedPrevMillis = 0;
void adjTime(unsigned long now){
// this function adds milliseconds times adjSecs every 86 seconds
if( now - adjustedPrevMillis >= 1000){
adjustedPrevMillis += 1000;
if( (++adjustedSysTime) % 86 == 0) {
adjustedPrevMillis += adjSeconds;
Serial.print("At adjustedSysTime= ");
Serial.print(adjustedSysTime,DEC);
Serial.print(" now= ");
Serial.print(now,DEC);
Serial.print(" now - adj=");
Serial.print(now - adjustedPrevMillis, DEC);
Serial.print(" adding ");
Serial.print(adjSeconds,DEC);
Serial.print(" msecs, adjPrevMs= ");
Serial.println(adjustedPrevMillis,DEC);
}
}
}
void printTime(unsigned long time){
int second = time % 60;
int minute = (time / 60) % 60;
int hours = (time / 3600) % 24;
Serial.print(hours,DEC);
Serial.print( ":");
Serial.print(minute,DEC);
Serial.print(":");
Serial.print(second,DEC);
}
void setup(){
Serial.begin(9600);
}
void loop() {
unsigned long now = millis();
while( now - prevMillis >= 1000){
sysTime++;
prevMillis += 1000;
}
adjTime(now);
Serial.print("Sys time= ");
printTime(sysTime);
Serial.print(", Adjusted time= ");
printTime(adjustedSysTime);
Serial.print(", delta= ");
Serial.print(sysTime - adjustedSysTime, DEC);
Serial.println(" seconds");
delay(500);
}