I was working with a DS3107 Real Time Clock module and the Time library. I've been using the tmElements_t structure to pass the data. This is defined as follows:
typedef struct { uint8_t Second; uint8_t Minute; uint8_t Hour; uint8_t Wday; // day of week, sunday is day 1 uint8_t Day; uint8_t Month; uint8_t Year; // offset from 1970; } tmElements_t, TimeElements, *tmElementsPtr_t;
Each of the elements are defined as 8bit unsigned integers which is fine for the hours and minutes etc but when you come to the years there's a problem waiting to snare you.
tmElements_t tm; tm.Year = 2014;
This code nicely compiles but will cause you a problem when it runs as 2014 will overflow the 8 bits available. It took me a few hours of experimenting and delving into the library to work out what was going on.
Is there a way I can trap for this overflow at compile time?
If not is there an alternative way of coding the time elements in C or C++ that can trap this?