Fixed datetime integer overflow on 8-bit compiler (#1162)

This commit is contained in:
Steve Karg
2025-11-25 21:02:04 -06:00
committed by GitHub
parent ba2e2ac24f
commit 21626d1ac5
2 changed files with 10 additions and 9 deletions
+1
View File
@@ -34,6 +34,7 @@ The git repositories are hosted at the following sites:
### Fixed ### Fixed
* Fixed datetime integer overflow on 8-bit AVR compiler (#1162)
* Fixed the ports/linux BACnet/IP cache netmask for accurate subnet * Fixed the ports/linux BACnet/IP cache netmask for accurate subnet
prefix calculation implementation which had always returned 0. (#1155) prefix calculation implementation which had always returned 0. (#1155)
* Fixed the loop object empty reference property by initializing to self. * Fixed the loop object empty reference property by initializing to self.
+9 -9
View File
@@ -725,29 +725,29 @@ void datetime_add_seconds(BACNET_DATE_TIME *bdatetime, int32_t seconds)
bdatetime->time.hour, bdatetime->time.min, bdatetime->time.sec); bdatetime->time.hour, bdatetime->time.min, bdatetime->time.sec);
bdatetime_days = datetime_days_since_epoch(&bdatetime->date); bdatetime_days = datetime_days_since_epoch(&bdatetime->date);
/* more minutes than in a day? */ /* more minutes than in a day? */
days = seconds / (24 * 60 * 60); days = seconds / (24L * 60L * 60L);
bdatetime_days += days; bdatetime_days += days;
seconds -= (days * 24 * 60 * 60); seconds -= (days * 24L * 60L * 60L);
/* less seconds - previous day? */ /* less seconds - previous day? */
if (seconds < 0) { if (seconds < 0) {
/* convert to positive for easier math */ /* convert to positive for easier math */
seconds *= -1; seconds *= -1L;
if ((uint32_t)seconds > bdatetime_seconds) { if ((uint32_t)seconds > bdatetime_seconds) {
/* previous day */ /* previous day */
bdatetime_days -= 1; bdatetime_days -= 1L;
bdatetime_seconds += ((24 * 60 * 60) - seconds); bdatetime_seconds += ((24L * 60L * 60L) - seconds);
} else { } else {
bdatetime_seconds -= seconds; bdatetime_seconds -= seconds;
days = bdatetime_seconds / (24 * 60 * 60); days = bdatetime_seconds / (24L * 60L * 60L);
bdatetime_days += days; bdatetime_days += days;
bdatetime_seconds -= (days * 24 * 60 * 60); bdatetime_seconds -= (days * 24L * 60L * 60L);
} }
} else { } else {
/* more days than current datetime? */ /* more days than current datetime? */
bdatetime_seconds += seconds; bdatetime_seconds += seconds;
days = bdatetime_seconds / (24 * 60 * 60); days = bdatetime_seconds / (24L * 60L * 60L);
bdatetime_days += days; bdatetime_days += days;
bdatetime_seconds -= (days * 24 * 60 * 60); bdatetime_seconds -= (days * 24L * 60L * 60L);
} }
/* convert bdatetime from seconds and days */ /* convert bdatetime from seconds and days */
datetime_hms_from_seconds_since_midnight( datetime_hms_from_seconds_since_midnight(