fbiego / ESP32Time

An Arduino library for setting and retrieving internal RTC time on ESP32 boards
MIT License
222 stars 37 forks source link

setTime(getTime()) decrements month by 1 #34

Closed ciaranmoore closed 1 year ago

ciaranmoore commented 1 year ago

In ESP32Time.cpp, setTime(sc, mn, hr, dy, mt, yr, 0) sets the date to year=yr-1900, month=mt-1, day=dy. For yr, the 1900 offset is fine, as getYear() returns timeinfo.tm_year+1900. But for mt, getMonth returns timeinfo.tm_mon, not timeinfo.tm_mon+1. This creates problems when you change the time.

For example,

int sc = rtc.getSecond(); int mn = rtc.getMinute(); int hr = rtc.getHour(); int dy = rtc.getDay(); int mt = rtc.getMonth(); int yr = rtc.getYear();

rtc.setTime(sc, mn, hr, dy, mt, yr + 1, 0); // Increments year correctly rtc.setTime(sc, mn, hr, dy, mt + 1, yr, 0); // Fails to increment month rtc.setTime(sc, mn, hr, dy + 1, mt, yr, 0); // Increments day correctly

fbiego commented 1 year ago

NB: getMonth() is (0-11) from timeinfo.tm_mon https://www.cplusplus.com/reference/ctime/tm/ So your code should be; int mt = rtc.getMonth() + 1;

https://github.com/fbiego/ESP32Time/blob/b731be64a32fafac2ddeebf57b2a715313223902/ESP32Time.cpp#L360-L367

13 #19