This one is just for fun, though I suppose I might not be the only one on the planet that ever needed to do this…
At my day job, I have a board that had a realtime clock, but no battery backup to retain the time. During startup, the system sends the current PC date and time (actually, it sends it in GMT, I believe, so looking at logs captured in different parts of the world will be easier — GMT is GMT anywhere on the planet ;-)
On startup, the board wants to log some things, but does not yet know the time. It had been using a hard-coded default time (like 1/1/2000). I wondered if the C compiler build date and time could be used to at least set the time based on when the firmware-in-use was compiled.
A quick chat with Bing’s AI (ChatGPT) and some experiments to make what it gave me far less bulky provided me with this:
int main()
{
// Initialize time to when this firmware was built.
const char *c_months = "JanFebMarAprMayJunJulAugSepOctNovDec";
char monthStr[4];
int year = 0;
int month = 0;
int day = 0;
int hour = 0;
int minute = 0;
int second = 0;
// “Mmm dd yyyy”
strncpy(monthStr, __DATE__, 3);
monthStr[3] = '\0';
month = (strstr(c_months, monthStr) - c_months) / 3 + 1;
day = atoi (&__DATE__[4]);
year = atoi (&__DATE__[7]);
printf ("%04d-%02d-%02d\n", year, month, day);
// “hh:mm:ss”
hour = atoi (&__TIME__[0]);
minute = atoi (&__TIME__[3]);
second = atoi (&__TIME__[6]);
printf ("%02d:%02d:%02d\n", hour, minute, second);
return 0;
}
This works by taking the compiler-generated macros of “__DATE__” and “__TIME__” and parsing out the values we want so they can be passed to a realtime clock routine or whatever.
In my case, this is not the code I am using since our embedded compiler handled __DATE__ in a different format. (It uses “dd-Mmm-yy” for some reason, while the standard C formatting appears to be “Mmm dd yyyy”.) But, the concept is similar.
Of course, as soon as I tested this, I found another issue. My board would power up and set to the build date (which is central standard time) and then when the system is connected, a new date/time is sent in GMT, which is currently 5 (or is it 6?) hours different, setting the clock back in time.
This makes log entries a bit odd ;-) but that’s a problem for another day.
Until then…
Where is this log saved/emitted to?
If it’s saved locally then maybe adjust the time when the real time clock is set during boot? I.E. first set the realtime clock to “zero”, and when it’s set to the correct time/date go through the log and adjust the times to be correct.
If it’s emitted in human readable form over a serial port or similar, maybe just use “T+4 seconds” or similar, where “T” is the boot time, for log entries that happen before knowing the actual time.
It goes to a flash part with write once (else erase entire sector) sectors. Adjusting the data is not possible :( For unfortunate reasons, I now have to scan every entry on startup looking for lowest and highest sequence numbers. This also would let me find the earliest time and start from there. I’m pretty sure some 80s computer – maybe the Amiga? – may have done something with floppy disks. Files always had a later date on a disk even if the date wasn’t correct.