Electronic – Inserting compile date/time into the code in XC8

microchippicxc8

I'm in the process of writing code for a real time clock, and I want to compile the code with constants for the date/time of compilation to burn onto the clock immediately upon programming.

I believe I have seen this done before, but my searches have proved fruitless. Is there some kind of compile time macro that would do this? Something like:

rtc.second = __COMPILE_SECOND;
rtc.minute = __COMPILE_MINUTE;
rtc.hour   = __COMPILE_HOUR;
rtc.date   = __COMPILE_DATE;
rtc.month  = __COMPILE_MONTH;
rtc.year   = __COMPILE_YEAR;

Best Answer

From the XC8 C Compiler Users Guide, page 229:-

5.14.3 Predefined Macros

__DATE__ to indicate the current date eg. May 21, 2004 
__TIME__ to indicate the current time eg. 08:06:31

You can parse these strings to get numbers compatible with your rtc, but what's the point of a real time clock that doesn't have the real time?

Anyway, look at this Stack Overflow Question for a way to do it.