Skip to content

Commit

Permalink
Merge pull request #113 from gutierrezps/RTClib
Browse files Browse the repository at this point in the history
Added comparison operators to DateTime class
  • Loading branch information
drak7 committed Jul 1, 2019
2 parents 3f7d2e2 + 040a6eb commit c592af1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
39 changes: 23 additions & 16 deletions RTClib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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];
Expand All @@ -234,7 +242,6 @@ String DateTime::timestamp(timestampOpt opt){
return String(buffer);
}


////////////////////////////////////////////////////////////////////////////////
// TimeSpan implementation

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand All @@ -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();

Expand All @@ -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();
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand All @@ -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();

Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

Expand All @@ -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();

Expand All @@ -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);
Expand All @@ -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=");
Expand Down
6 changes: 6 additions & 0 deletions RTClib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c592af1

Please sign in to comment.