I am attempting to update the RTC time to allow for Daylight Saving Time. I get NPT devicetime and update the RTC using
rtc.adjust(DateTime(devicetime)); without any problems. When I attempt to update the hour with the command
DateTime TempFix = (DateTime(now.year(),now.month(), now.day(),now.hour() + 1,now.minute(),now.second()); The hour does get incremented by one BUT the year gets changed from 2020 to 2050. But if I follow the month change with a year change of -30 to get back to 2020 nothing happens and the year stays at 2050. Any Ideas what I could be doing wrong?
I have done a bit more on this subject and have discovered that the problem is caused when initially setting up the DateTime structure prior to using the rtc.adjust() routine.
If after the call DateTime TempFix = (DateTime(now.year(),now.month(), now.day(), now.hour() + 1,now.minute(),now.second()) I the issue a print command Serial.print(TempFix.year(); then 2050 is displayed. so the problem is in the DateTime routine
DateTime does not directly calculate a new time. It just stores the elements in the object. There is no checking of values so you can get an invalid time.
You can use something like this:
DateTime alarm = DateTime ( now + 60); //Alarm 60 seconds in the future
DateTime future = DateTime ( now + TimeSpan(days, hours, minutes,seconds));
e.g. DateTime future = DateTime ( now + TimeSpan(1, 2, 3,4));
Gives these timestamps.
|21:25:12.243 -> Time is:|1602365100|2020-10-10T21:25:00|
|21:25:12.243 -> Future is:|1602458884|2020-10-11T23:28:04|
|21:25:13.214 -> Time is:|1602365101|2020-10-10T21:25:01|
|21:25:13.214 -> Future is:|1602458885|2020-10-11T23:28:05|
To handle TimeZones it is usually best to keep your system time as UTC then use a TimeZone library to display times in the chosen timezone.
I hope this helps.
Ted
Thank you for taking the time to reply to my post. I agree that the DateTime() module purpose is to fill in the DateTime structure. When it was just passed the number of seconds from the NTP server it worked fine. It was only when I used the method that accepted the year month day hour minutes and seconds for DST that I had the problem. I am pleased to say that this has now been fixed in the latest version of the Seeed_Arduino_RTC that was released on Friday.