Allow month and year to wrap in timezone calculation

This commit is contained in:
Lewis Jackson 2023-05-29 14:45:06 +03:00
parent efcb292998
commit 82bfd0471f

View file

@ -32,6 +32,8 @@ void WatchyRTC::read(tmElements_t & tm, int offsetInSeconds) {
tm.Minute = rtc_pcf.getMinute(); tm.Minute = rtc_pcf.getMinute();
tm.Second = rtc_pcf.getSecond(); tm.Second = rtc_pcf.getSecond();
int year = tm.Year;
int month = tm.Month;
int day = tm.Day; int day = tm.Day;
int hour = tm.Hour; int hour = tm.Hour;
int minute = tm.Minute; int minute = tm.Minute;
@ -54,6 +56,38 @@ void WatchyRTC::read(tmElements_t & tm, int offsetInSeconds) {
hour = hour % 24; hour = hour % 24;
} }
bool leapYear = year % 4 == 0;
if (leapYear) {
if (month == 2 && day > 29) {
month++;
day = day % 29;
}
} else {
if (month == 2 && day > 28) {
month++;
day = day % 28;
}
}
if (month == 4 || month == 6 || month == 9 || month == 11) {
if (day > 30) {
month++;
day = day % 30;
}
} else {
if (day > 31) {
month++;
day = day % 31;
}
}
if (month > 12 || month < 0) {
year++;
month = month % 12;
}
tm.Year = year;
tm.Month = month;
tm.Day = day; tm.Day = day;
tm.Hour = hour; tm.Hour = hour;
tm.Minute = minute; tm.Minute = minute;