C preprocessor __TIMESTAMP__ in ISO 8601:2004

2019-06-15 19:42发布

问题:

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

回答1:

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;
}


回答2:

@Jonathan Leffler's answer provides an excellent solution but there might be one thing was ignored:
The original question was asking alternative format of __TIMESTAMP__ while __TIMESTAMP__ is to be expanded to string of last modified date-time of current source file. However, the original answer was in fact assigning __TIMESTAMP__ with date-time of gcc being run (i.e. building time), which is working as __DATE__ and __TIME__.

How to improve:
With @mmond's answer, replaces the date +'"%Y-%m-%dT%H:%M:%S"' command in -D__TIMESTAMP__=... with date +'"%Y-%m-%dT%H:%M:%S"' -r xx.c, i.e. to add option -r xx.c to reference xx.c when running date command. The xx.c is the source code being built.

Reference:
According to GCC's [manual][2]

__TIMESTAMP__
This macro expands to a string constant that describes the date and time of the last modification of the current source file.



回答3:

If you need a really cross-platform way to have compile time stamp string formatted in ISO 8601 or any other format defined at compile-time, you can instead consider using CMake (which is always good to consider using).

What you want can be easily accomplished with CMake.



回答4:

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