RTClib Assignment Operator

For RTC breakouts, etc., use the Other Products from Adafruit forum

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
dannyv2
 
Posts: 26
Joined: Sun Sep 22, 2013 2:42 pm

RTClib Assignment Operator

Post by dannyv2 »

Hello AdaFruit,

Here’s some suggested feedback for your software folks (It's NOT a request for help thanks).
You might be interested in these additions to the (Chronodot) RTClib library as it stands in gitweb,

1:
It’s an assignment operator so that we can update an existing DateTime variable by assigning it another DateTime variable (which we assume is more recent).


RTClib.h:

DateTime operator =(const DateTime& other) ;


RTClib.cpp:

Code: Select all

DateTime DateTime::operator=(const  DateTime& other)
{
    yOff    = other.yOff;
    m         = other.m;
    d           = other.d;
    hh         = other.hh;
    mm      = other.mm;
    ss          = other.ss;
    return *this;
}

//Usage (part of my 'getting NTP time’ function):
...
uint32_t myeTime = (uint32_t) epoch + 34200 + daylight_saving;
//Serial.println("\n Got NTP time ....");
#ifdef USE_RTC_DS3231
DateTime NewDate(myeTime);

NTPTime = NewDate; //using assignment operator (Updating my global copy of NTPTime)

RTC.adjust(NTPTime); //Correct the RTC
#endif //USE_RTC_DS3231

(For info:The global copy of the NTP time takes over if the RTC fails (which I had happen with another brand RTC), and gives a slightly less accurate time.)


I’m happy to send the rest of the NTP example as well if you like, it works well to keep the rtc spot on time.


2:

I’ve also changed my version of so that the date prints out the day, but I changed the order of the month and the date to make this work (see example).
18/November Edit: Corrected the order of the Days, 'dayOfWeek()' returns 0 for Sunday.

Code: Select all

const char *days[] = { "Sun, ", "Mon, ", "Tue, ", "Wed, ", "Thu, ", "Fri, ", "Sat, " };
const char* months[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

// as a string
char* DateTime::toString(char* buf, int maxlen) const
{
    snprintf(buf,maxlen,"%s %02u %s,   %04u - %02u : %02u : %02u",
             days[dayOfWeek()],
             d,
             months[m-1],
             2000 + yOff,
             hh,
             mm,
             ss
            );
    return buf;
}
Sat, 16 Nov, 2013 - 16 : 11 : 41



Danny Vagg
Last edited by dannyv2 on Sat Nov 16, 2013 1:18 pm, edited 2 times in total.

User avatar
adafruit_support_bill
 
Posts: 88089
Joined: Sat Feb 07, 2009 10:11 am

Re: RTClib Assignment Operator

Post by adafruit_support_bill »

Good stuff! Thanks for posting. :)

Locked
Please be positive and constructive with your questions and comments.

Return to “Clock Kits (discontinued)”