C preprocessor __TIMESTAMP__ in ISO 8601: 2004

How can I replace __TIMESTAMP__ in ISO 8601: 2004?

__TIMESTAMP__

 Sat Jul 6 02:50:06 2013 

against

__TIMESTAMP_ISO__

 2013-07-06T00:50:06Z 
+8
c gcc c-preprocessor clang icc
source share
3 answers

Oh optimist! You would not expect one standard to pay attention to another, would you? The definition of __TIMESTAMP__ does not conform to the C standard, as you know. It would be great to have a format like your proposed __TIMESTAMP_ISO__ (would you always like Zulu time, or would it be better to have a local time zone offset?), But frankly, the easiest way to add it could be a patch for GCC and Clang, etc. .d.

You can try the monkey with asctime() , as suggested by user1034749 , d rather don't try this.

In the GCC 4.8.1 manual , there is an interesting warning:

-Wno-builtin-macro-redefined
Do not warn if certain built-in macros are overridden. This suppresses warnings for overriding __TIMESTAMP__ , __TIME__ , __DATE__ , __FILE__ and __BASE_FILE__ .

This allows you to try:

 gcc ... -Wno-builtin-macro-redefined -D__TIMESTAMP__=$(date +'"%Y-%m-%dT%H:%M:%S"') ... 

(Note the hieroglyphs needed to get a string from date , surrounded by double quotes.) However, some earlier versions of GCC do not support this option; I do not remember that it was before. You can override __TIMESTAMP__ :

 $ gcc -std=c99 -Wall -Wextra -O xx.c -o xx $ ./xx Fri Jul 5 19:56:25 2013 $ gcc -std=c99 -Wall -Wextra -D__TIMESTAMP__=$(date +'"%Y-%m-%dT%H:%M:%S"') -O xx.c -o xx <command-line>: warning: "__TIMESTAMP__" redefined $ ./xx 2013-07-05T20:10:28 $ 

Not very pretty, but it works ... Oh, and for the record only, the source code was (trivial):

 #include <stdio.h> int main(void) { printf("%s\n", __TIMESTAMP__); return 0; } 
+17
source share

If you need a truly cross-platform way to copy a time stamp string in ISO 8601 format or any other format defined at compile time, you can use CMake instead (which is always useful to consider).

What you want is easy to accomplish with CMake .

0
source share

clang and gcc use the C function asctime for this purpose, I suppose icc also uses it. On Linux, you can use LD_PRELOAD to invoke the asctime call and replace with any string you want.

-one
source share

All Articles