I am using sscanf_P to parse the system generated compile date/time. This is simply loaded into the real time clock. It is only done if DEVELOPMENT is defined.
TIME defined format is hh:mm:ss
DATE defined format is mmm dd yyyy
sscanf_P(sd, PSTR("%s %u %u"), month, &(tm.Day), &yy); works just fine and returns the month, day, and year as expected.
sscanf_P(st, PSTR("%u:%u:%u"), &tm.Hour, &tm.Minute, &tm.Second) does not work at all. It returns only the last value, ie. tm.Second in this example and the other values are zero. If I use a format string with just two specifiers then it returns tm.Minute, and other values of zero. A format string with one specifier returns just tm.Hour.
I have tried dozens of different variations on this and none of them work. Eventually I gave up and resorted to laboriously tokenizing the string. That worked, but leaves the code less readable.
Here is the routine where the problem occurs. If anyone has any idea what is the problem I would appreciate an answer.
#ifdef _DEVELOPMENT_
#define _TFF_ 10 // Delay in seconds between compiling and running.
// Set the DS3231 date/time to compile date/time.
void setDS3231time() {
time_t t;
tmElements_t tm;
char st[] = {__TIME__}; // System defined format hh:mm:ss
char sd[] = {__DATE__}; // System defined format mmm dd yyyy
char sm[][4] = {"Jan", "Feb", "Mar", "Apr",
"May", "Jun", "Jul", "Aug",
"Sep", "Oct", "Nov", "Dec"};
// Parse the time.
// This bizare mess is because sscanf_P(st, PSTR("%u:%u:%u"), &(tm.Hour), &(tm.Minute), &(tm.Second))
// refuses to work!!! Why???
char *pp;
uint8_t nn[3], n = 0;
pp = strtok(st, ":");
while(pp != NULL) {
nn[n++] = atoi(pp);
pp = strtok(NULL, ":");
}
tm.Hour = nn[0];
tm.Minute = nn[1];
tm.Second = nn[2];
// Parse the date. Month is returned as a string.
uint16_t yy;
char month[4];
sscanf_P(sd, PSTR("%s %u %u"), month, &(tm.Day), &yy);
tm.Year = CalendarYrToTm(yy);
// Search for the month and return its ordinal value (12 on failure).
tm.Month = 0;
while(strcmp(sm[tm.Month], month) && (tm.Month < 12)) tm.Month++;
// Set the RTC and adjust for the delay between compiling and running.
t = makeTime(tm);
RTC.set(t);
setTime(t);
adjustTime(_TFF_);
}
#endif // _DEVELOPMENT_
/* EOF FRIDGETEMPLOG.INO */