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
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;
}
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With