Skip to content

Commit

Permalink
Merge pull request #196 from hlovdal/unsigned-fix
Browse files Browse the repository at this point in the history
Force unsigned integer aritmetic for all year 2000 calculations
  • Loading branch information
drak7 authored Oct 29, 2020
2 parents a8e2d31 + 9995dc4 commit 52dc576
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
30 changes: 15 additions & 15 deletions RTClib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,8 +129,8 @@ const uint8_t daysInMonth[] PROGMEM = {31, 28, 31, 30, 31, 30,
*/
/**************************************************************************/
static uint16_t date2days(uint16_t y, uint8_t m, uint8_t d) {
if (y >= 2000)
y -= 2000;
if (y >= 2000U)
y -= 2000U;
uint16_t days = d;
for (uint8_t i = 1; i < m; ++i)
days += pgm_read_byte(daysInMonth + i - 1);
Expand Down Expand Up @@ -225,8 +225,8 @@ DateTime::DateTime(uint32_t t) {
/**************************************************************************/
DateTime::DateTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour,
uint8_t min, uint8_t sec) {
if (year >= 2000)
year -= 2000;
if (year >= 2000U)
year -= 2000U;
yOff = year;
m = month;
d = day;
Expand Down Expand Up @@ -669,8 +669,8 @@ TimeSpan DateTime::operator-(const DateTime &right) {
*/
/**************************************************************************/
bool DateTime::operator<(const DateTime &right) const {
return (yOff + 2000 < right.year() ||
(yOff + 2000 == right.year() &&
return (yOff + 2000U < right.year() ||
(yOff + 2000U == right.year() &&
(m < right.month() ||
(m == right.month() &&
(d < right.day() ||
Expand All @@ -693,7 +693,7 @@ bool DateTime::operator<(const DateTime &right) const {
*/
/**************************************************************************/
bool DateTime::operator==(const DateTime &right) const {
return (right.year() == yOff + 2000 && right.month() == m &&
return (right.year() == yOff + 2000U && right.month() == m &&
right.day() == d && right.hour() == hh && right.minute() == mm &&
right.second() == ss);
}
Expand Down Expand Up @@ -724,11 +724,11 @@ String DateTime::timestamp(timestampOpt opt) {
break;
case TIMESTAMP_DATE:
// Only date
sprintf(buffer, "%d-%02d-%02d", 2000 + yOff, m, d);
sprintf(buffer, "%u-%02d-%02d", 2000U + yOff, m, d);
break;
default:
// Full
sprintf(buffer, "%d-%02d-%02dT%02d:%02d:%02d", 2000 + yOff, m, d, hh, mm,
sprintf(buffer, "%u-%02d-%02dT%02d:%02d:%02d", 2000U + yOff, m, d, hh, mm,
ss);
}
return String(buffer);
Expand Down Expand Up @@ -851,7 +851,7 @@ void RTC_DS1307::adjust(const DateTime &dt) {
Wire._I2C_WRITE(bin2bcd(0));
Wire._I2C_WRITE(bin2bcd(dt.day()));
Wire._I2C_WRITE(bin2bcd(dt.month()));
Wire._I2C_WRITE(bin2bcd(dt.year() - 2000));
Wire._I2C_WRITE(bin2bcd(dt.year() - 2000U));
Wire.endTransmission();
}

Expand All @@ -873,7 +873,7 @@ DateTime RTC_DS1307::now() {
Wire._I2C_READ();
uint8_t d = bcd2bin(Wire._I2C_READ());
uint8_t m = bcd2bin(Wire._I2C_READ());
uint16_t y = bcd2bin(Wire._I2C_READ()) + 2000;
uint16_t y = bcd2bin(Wire._I2C_READ()) + 2000U;

return DateTime(y, m, d, hh, mm, ss);
}
Expand Down Expand Up @@ -1110,7 +1110,7 @@ void RTC_PCF8523::adjust(const DateTime &dt) {
Wire._I2C_WRITE(bin2bcd(dt.day()));
Wire._I2C_WRITE(bin2bcd(0)); // skip weekdays
Wire._I2C_WRITE(bin2bcd(dt.month()));
Wire._I2C_WRITE(bin2bcd(dt.year() - 2000));
Wire._I2C_WRITE(bin2bcd(dt.year() - 2000U));
Wire.endTransmission();

// set to battery switchover mode
Expand Down Expand Up @@ -1138,7 +1138,7 @@ DateTime RTC_PCF8523::now() {
uint8_t d = bcd2bin(Wire._I2C_READ());
Wire._I2C_READ(); // skip 'weekdays'
uint8_t m = bcd2bin(Wire._I2C_READ());
uint16_t y = bcd2bin(Wire._I2C_READ()) + 2000;
uint16_t y = bcd2bin(Wire._I2C_READ()) + 2000U;

return DateTime(y, m, d, hh, mm, ss);
}
Expand Down Expand Up @@ -1573,7 +1573,7 @@ void RTC_DS3231::adjust(const DateTime &dt) {
Wire._I2C_WRITE(bin2bcd(dowToDS3231(dt.dayOfTheWeek())));
Wire._I2C_WRITE(bin2bcd(dt.day()));
Wire._I2C_WRITE(bin2bcd(dt.month()));
Wire._I2C_WRITE(bin2bcd(dt.year() - 2000));
Wire._I2C_WRITE(bin2bcd(dt.year() - 2000U));
Wire.endTransmission();

uint8_t statreg = read_i2c_register(DS3231_ADDRESS, DS3231_STATUSREG);
Expand All @@ -1599,7 +1599,7 @@ DateTime RTC_DS3231::now() {
Wire._I2C_READ();
uint8_t d = bcd2bin(Wire._I2C_READ());
uint8_t m = bcd2bin(Wire._I2C_READ());
uint16_t y = bcd2bin(Wire._I2C_READ()) + 2000;
uint16_t y = bcd2bin(Wire._I2C_READ()) + 2000U;

return DateTime(y, m, d, hh, mm, ss);
}
Expand Down
2 changes: 1 addition & 1 deletion RTClib.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class DateTime {
@brief Return the year.
@return Year (range: 2000--2099).
*/
uint16_t year() const { return 2000 + yOff; }
uint16_t year() const { return 2000U + yOff; }
/*!
@brief Return the month.
@return Month number (1--12).
Expand Down

0 comments on commit 52dc576

Please sign in to comment.