Skip to content

Commit

Permalink
Merge pull request #250 from edgar-bonet/more-const
Browse files Browse the repository at this point in the history
Improve const correctness
  • Loading branch information
drak7 authored Dec 7, 2021
2 parents 6be8310 + bc6139e commit b4791eb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/RTC_DS1307.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void RTC_DS1307::readnvram(uint8_t *buf, uint8_t size, uint8_t address) {
@param size Number of bytes in buf to write to NVRAM
*/
/**************************************************************************/
void RTC_DS1307::writenvram(uint8_t address, uint8_t *buf, uint8_t size) {
void RTC_DS1307::writenvram(uint8_t address, const uint8_t *buf, uint8_t size) {
uint8_t addrByte = DS1307_NVRAM + address;
i2c_dev->write(buf, size, true, &addrByte, 1);
}
Expand Down
14 changes: 7 additions & 7 deletions src/RTClib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ bool DateTime::isValid() const {
*/
/**************************************************************************/

char *DateTime::toString(char *buffer) {
char *DateTime::toString(char *buffer) const {
uint8_t apTag =
(strstr(buffer, "ap") != nullptr) || (strstr(buffer, "AP") != nullptr);
uint8_t hourReformatted = 0, isPM = false;
Expand Down Expand Up @@ -601,7 +601,7 @@ uint32_t DateTime::secondstime(void) const {
@return New DateTime object with span added to it.
*/
/**************************************************************************/
DateTime DateTime::operator+(const TimeSpan &span) {
DateTime DateTime::operator+(const TimeSpan &span) const {
return DateTime(unixtime() + span.totalseconds());
}

Expand All @@ -612,7 +612,7 @@ DateTime DateTime::operator+(const TimeSpan &span) {
@return New DateTime object with span subtracted from it.
*/
/**************************************************************************/
DateTime DateTime::operator-(const TimeSpan &span) {
DateTime DateTime::operator-(const TimeSpan &span) const {
return DateTime(unixtime() - span.totalseconds());
}

Expand All @@ -628,7 +628,7 @@ DateTime DateTime::operator-(const TimeSpan &span) {
@return TimeSpan of the difference between DateTimes.
*/
/**************************************************************************/
TimeSpan DateTime::operator-(const DateTime &right) {
TimeSpan DateTime::operator-(const DateTime &right) const {
return TimeSpan(unixtime() - right.unixtime());
}

Expand Down Expand Up @@ -689,7 +689,7 @@ bool DateTime::operator==(const DateTime &right) const {
@return Timestamp string, e.g. "2020-04-16T18:34:56".
*/
/**************************************************************************/
String DateTime::timestamp(timestampOpt opt) {
String DateTime::timestamp(timestampOpt opt) const {
char buffer[25]; // large enough for any DateTime, including invalid ones

// Generate timestamp according to opt
Expand Down Expand Up @@ -748,7 +748,7 @@ TimeSpan::TimeSpan(const TimeSpan &copy) : _seconds(copy._seconds) {}
@return New TimeSpan object, sum of left and right
*/
/**************************************************************************/
TimeSpan TimeSpan::operator+(const TimeSpan &right) {
TimeSpan TimeSpan::operator+(const TimeSpan &right) const {
return TimeSpan(_seconds + right._seconds);
}

Expand All @@ -759,6 +759,6 @@ TimeSpan TimeSpan::operator+(const TimeSpan &right) {
@return New TimeSpan object, right subtracted from left
*/
/**************************************************************************/
TimeSpan TimeSpan::operator-(const TimeSpan &right) {
TimeSpan TimeSpan::operator-(const TimeSpan &right) const {
return TimeSpan(_seconds - right._seconds);
}
16 changes: 8 additions & 8 deletions src/RTClib.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ class DateTime {
DateTime(const __FlashStringHelper *date, const __FlashStringHelper *time);
DateTime(const char *iso8601date);
bool isValid() const;
char *toString(char *buffer);
char *toString(char *buffer) const;

/*!
@brief Return the year.
Expand Down Expand Up @@ -207,11 +207,11 @@ class DateTime {
TIMESTAMP_TIME, //!< `hh:mm:ss`
TIMESTAMP_DATE //!< `YYYY-MM-DD`
};
String timestamp(timestampOpt opt = TIMESTAMP_FULL);
String timestamp(timestampOpt opt = TIMESTAMP_FULL) const;

DateTime operator+(const TimeSpan &span);
DateTime operator-(const TimeSpan &span);
TimeSpan operator-(const DateTime &right);
DateTime operator+(const TimeSpan &span) const;
DateTime operator-(const TimeSpan &span) const;
TimeSpan operator-(const DateTime &right) const;
bool operator<(const DateTime &right) const;

/*!
Expand Down Expand Up @@ -311,8 +311,8 @@ class TimeSpan {
*/
int32_t totalseconds() const { return _seconds; }

TimeSpan operator+(const TimeSpan &right);
TimeSpan operator-(const TimeSpan &right);
TimeSpan operator+(const TimeSpan &right) const;
TimeSpan operator-(const TimeSpan &right) const;

protected:
int32_t _seconds; ///< Actual TimeSpan value is stored as seconds
Expand Down Expand Up @@ -359,7 +359,7 @@ class RTC_DS1307 : RTC_I2C {
uint8_t readnvram(uint8_t address);
void readnvram(uint8_t *buf, uint8_t size, uint8_t address);
void writenvram(uint8_t address, uint8_t data);
void writenvram(uint8_t address, uint8_t *buf, uint8_t size);
void writenvram(uint8_t address, const uint8_t *buf, uint8_t size);
};

/**************************************************************************/
Expand Down

0 comments on commit b4791eb

Please sign in to comment.