...get the build date and time from within my program?
How do I go about getting the date and time my program was built from within my program?
You can use the standard __DATE__ and __TIME__ preprocessor definitions. For example:
const char *build_date = __DATE__;
const char *build_time = __TIME__;debug_printf("Build Date: %s\n", build_date);
debug_printf("Build Time: %s\n", build_time);
Will output:
Build Date: Feb 11 2010
Build Time: 09:43:54
Alternatively, if you want more control over how the date and time are formatted you can use CrossStudio's $(Date), $(DateYear), $(DateMonth), $(DateDay), $(Time), $(TimeHour), $(TimeMinute) and $(TimeSecond) macros. For example, adding the following preprocessor definitions to your project:
BUILD_DATE_NUMBER=0x$(DateYear)$(DateMonth)$(DateDay)
BUILD_TIME_NUMBER=0x$(TimeHour)$(TimeMinute)$(TimeSecond)
And then running the following program:
const unsigned long build_date_number = BUILD_DATE_NUMBER;
const unsigned long build_time_number = BUILD_TIME_NUMBER;
debug_printf("Build Date Number: 0x%08X\n", build_date_number);
debug_printf("Build Time Number: 0x%06X\n", build_time_number);
Will output:
Build Date Number: 0x20100211
Build Time Number: 0x101559
Note: this method will only work with CrossWorks V2.x and later.
-
Hello Jon,
I have a trouble to use your example in version 3.1.1. I have tried
const uint64_t build_date_number = 0x$(DateYear)$(DateMonth)$(DateDay);
but I am getting:
"invalid suffix "x$" on integer constant"
Do I have to include any specific include to note preprocessor how to use $(DateYear) etc.?
-
Hi Martin,
You've missed out a step - you need to use the CrossStudio macros to define a C preprocessor definition. So, for example, you might add the following to your Preprocessor Definitions project property:
BUILD_DATE_NUMBER=0x$(DateYear)$(DateMonth)$(DateDay)
Once you have done that, you can use the preprocessor definition from within your program as you would any other, for example:
const unsigned long build_date_number = BUILD_DATE_NUMBER;
You cannot use the CrossStudio macro directly in your C code, the compiler doesn't know anything about them.
Regards,
Jon
Please sign in to leave a comment.
Comments
5 comments