ISO 8601:2004中的C预处理器__TIMESTAMP__ [英] C preprocessor __TIMESTAMP__ in ISO 8601:2004
问题描述
如何在ISO 8601:2004中进行 __ TIMESTAMP __
更换?
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
推荐答案
哦,你们乐观!你真的不希望一个标准注意另一个,你会吗? __ TIMESTAMP __
define不在标准C中,只是你知道。这将是非常好的有一个格式,如你的建议 __ TIMESTAMP_ISO __
(你会永远想要祖鲁时间,还是最好有本地时区偏移?),但坦率地说
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.
您可以尝试使用 asctime() / code>,由 user1034749 的
You can try monkeying with asctime()
as suggested by user1034749's answer, but I'd rather not try that.
在GCC 4.8.1中 answer ,但我不想尝试。 /gcc.gnu.org/onlinedocs/\">手动,有一个有趣的警告抑制:
In the GCC 4.8.1 manual, there's an interesting warning suppression:
-Wno-builtin-macro-redefined
如果重新定义某些内置宏,则不要发出警告。这抑制了重新定义__ TIMESTAMP __
,__ TIME __
,__ DATE __
,__ FILE __
和
__ BASE_FILE __
。
这表明你可以尝试:
gcc ... -Wno-builtin-macro-redefined -D__TIMESTAMP__=$(date +'"%Y-%m-%dT%H:%M:%S"') ...
(请注意,从 date
获取字符串所需的象形文字必须用双引号括起。)然而,一些早期版本的GCC支持选项;我不记得以前看到它。您仍然可以重新定义 __ TIMESTAMP __
:
(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
$
$ b b
不是很漂亮,但它工作...哦,只是为了记录,源代码是(琐碎):
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;
}
这篇关于ISO 8601:2004中的C预处理器__TIMESTAMP__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!