diff --git a/RTClib.cpp b/RTClib.cpp index 46caf4aa..393e1fb4 100644 --- a/RTClib.cpp +++ b/RTClib.cpp @@ -137,7 +137,7 @@ static uint8_t conv2d(const char* p) { DateTime::DateTime (const char* date, const char* time) { // sample input: date = "Dec 26 2009", time = "12:34:56" yOff = conv2d(date + 9); - // Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec + // Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec switch (date[0]) { case 'J': m = (date[1] == 'a') ? 1 : ((date[2] == 'n') ? 6 : 7); break; case 'F': m = 2; break; @@ -180,7 +180,7 @@ DateTime::DateTime (const __FlashStringHelper* date, const __FlashStringHelper* ss = conv2d(buff + 6); } -uint8_t DateTime::dayOfTheWeek() const { +uint8_t DateTime::dayOfTheWeek() const { uint16_t day = date2days(yOff, m, d); return (day + 6) % 7; // Jan 1, 2000 is a Saturday, i.e. returns 6 } @@ -213,6 +213,14 @@ TimeSpan DateTime::operator-(const DateTime& right) { return TimeSpan(unixtime()-right.unixtime()); } +bool DateTime::operator<(const DateTime& right) const { + return unixtime() < right.unixtime(); +} + +bool DateTime::operator==(const DateTime& right) const { + return unixtime() == right.unixtime(); +} + //ISO 8601 Timestamp String DateTime::timestamp(timestampOpt opt){ char buffer[20]; @@ -234,7 +242,6 @@ String DateTime::timestamp(timestampOpt opt){ return String(buffer); } - //////////////////////////////////////////////////////////////////////////////// // TimeSpan implementation @@ -294,7 +301,7 @@ void RTC_DS1307::adjust(const DateTime& dt) { DateTime RTC_DS1307::now() { Wire.beginTransmission(DS1307_ADDRESS); - Wire._I2C_WRITE((byte)0); + Wire._I2C_WRITE((byte)0); Wire.endTransmission(); Wire.requestFrom(DS1307_ADDRESS, 7); @@ -305,7 +312,7 @@ DateTime RTC_DS1307::now() { uint8_t d = bcd2bin(Wire._I2C_READ()); uint8_t m = bcd2bin(Wire._I2C_READ()); uint16_t y = bcd2bin(Wire._I2C_READ()) + 2000; - + return DateTime (y, m, d, hh, mm, ss); } @@ -315,7 +322,7 @@ Ds1307SqwPinMode RTC_DS1307::readSqwPinMode() { Wire.beginTransmission(DS1307_ADDRESS); Wire._I2C_WRITE(DS1307_CONTROL); Wire.endTransmission(); - + Wire.requestFrom((uint8_t)DS1307_ADDRESS, (uint8_t)1); mode = Wire._I2C_READ(); @@ -335,7 +342,7 @@ void RTC_DS1307::readnvram(uint8_t* buf, uint8_t size, uint8_t address) { Wire.beginTransmission(DS1307_ADDRESS); Wire._I2C_WRITE(addrByte); Wire.endTransmission(); - + Wire.requestFrom((uint8_t) DS1307_ADDRESS, size); for (uint8_t pos = 0; pos < size; ++pos) { buf[pos] = Wire._I2C_READ(); @@ -454,7 +461,7 @@ void RTC_PCF8523::adjust(const DateTime& dt) { DateTime RTC_PCF8523::now() { Wire.beginTransmission(PCF8523_ADDRESS); - Wire._I2C_WRITE((byte)3); + Wire._I2C_WRITE((byte)3); Wire.endTransmission(); Wire.requestFrom(PCF8523_ADDRESS, 7); @@ -465,7 +472,7 @@ DateTime RTC_PCF8523::now() { Wire._I2C_READ(); // skip 'weekdays' uint8_t m = bcd2bin(Wire._I2C_READ()); uint16_t y = bcd2bin(Wire._I2C_READ()) + 2000; - + return DateTime (y, m, d, hh, mm, ss); } @@ -475,7 +482,7 @@ Pcf8523SqwPinMode RTC_PCF8523::readSqwPinMode() { Wire.beginTransmission(PCF8523_ADDRESS); Wire._I2C_WRITE(PCF8523_CLKOUTCONTROL); Wire.endTransmission(); - + Wire.requestFrom((uint8_t)PCF8523_ADDRESS, (uint8_t)1); mode = Wire._I2C_READ(); @@ -535,7 +542,7 @@ void RTC_DS3231::adjust(const DateTime& dt) { DateTime RTC_DS3231::now() { Wire.beginTransmission(DS3231_ADDRESS); - Wire._I2C_WRITE((byte)0); + Wire._I2C_WRITE((byte)0); Wire.endTransmission(); Wire.requestFrom(DS3231_ADDRESS, 7); @@ -546,7 +553,7 @@ DateTime RTC_DS3231::now() { uint8_t d = bcd2bin(Wire._I2C_READ()); uint8_t m = bcd2bin(Wire._I2C_READ()); uint16_t y = bcd2bin(Wire._I2C_READ()) + 2000; - + return DateTime (y, m, d, hh, mm, ss); } @@ -556,7 +563,7 @@ Ds3231SqwPinMode RTC_DS3231::readSqwPinMode() { Wire.beginTransmission(DS3231_ADDRESS); Wire._I2C_WRITE(DS3231_CONTROL); Wire.endTransmission(); - + Wire.requestFrom((uint8_t)DS3231_ADDRESS, (uint8_t)1); mode = Wire._I2C_READ(); @@ -575,7 +582,7 @@ void RTC_DS3231::writeSqwPinMode(Ds3231SqwPinMode mode) { ctrl |= 0x04; // turn on INTCN } else { ctrl |= mode; - } + } write_i2c_register(DS3231_ADDRESS, DS3231_CONTROL, ctrl); //Serial.println( read_i2c_register(DS3231_ADDRESS, DS3231_CONTROL), HEX); @@ -587,11 +594,11 @@ float RTC_DS3231::getTemperature() Wire.beginTransmission(DS3231_ADDRESS); Wire._I2C_WRITE(DS3231_TEMPERATUREREG); Wire.endTransmission(); - + Wire.requestFrom(DS3231_ADDRESS, 2); msb = Wire._I2C_READ(); lsb = Wire._I2C_READ(); - + // Serial.print("msb="); // Serial.print(msb,HEX); // Serial.print(", lsb="); diff --git a/RTClib.h b/RTClib.h index e59f3d1b..efad38bc 100644 --- a/RTClib.h +++ b/RTClib.h @@ -59,6 +59,12 @@ class DateTime { DateTime operator+(const TimeSpan& span); DateTime operator-(const TimeSpan& span); TimeSpan operator-(const DateTime& right); + bool operator<(const DateTime& right) const; + bool operator>(const DateTime& right) const { return right < *this; } + bool operator<=(const DateTime& right) const { return !(*this > right); } + bool operator>=(const DateTime& right) const { return !(*this < right); } + bool operator==(const DateTime& right) const; + bool operator!=(const DateTime& right) const { return !(*this == right); } protected: uint8_t yOff, m, d, hh, mm, ss;