C preprocessor __TIMESTAMP__ in ISO 8601:2004

Cetin Sert picture Cetin Sert · Jul 6, 2013 · Viewed 10.7k times · Source

How can I have a __TIMESTAMP__ replacement in ISO 8601:2004?

__TIMESTAMP__

Sat Jul  6 02:50:06 2013

vs

__TIMESTAMP_ISO__

2013-07-06T00:50:06Z

Answer

Jonathan Leffler picture Jonathan Leffler · Jul 6, 2013

Oh ye optimist! You wouldn't really expect one standard to pay attention to another, would you? The __TIMESTAMP__ define is not in standard C, just so as you are aware. It would be great to have a format like your proposed __TIMESTAMP_ISO__ (would you always want Zulu time, or would it be better to have the local time zone offset?), but frankly, the easiest way to get it added might be a patch to GCC and Clang and so on.

You can try monkeying with asctime() as suggested by user1034749's answer, but I'd rather not try that.

In the GCC 4.8.1 manual, there's an interesting warning suppression:

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

This suggests you could try:

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

(Note the hieroglyphics necessary to get the string from date surrounded by double quotes.) However, some earlier versions of GCC do not support the option; I don't recall seeing it before. You can still redefine __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 just for the record, the source code was (trivial):

#include <stdio.h>

int main(void)
{
    printf("%s\n", __TIMESTAMP__);
    return 0;
}