c++ - 哪个宏更准确? __TIME__ 还是 __TIMESTAMP__?

标签 c++ visual-c++ macros c-preprocessor

我有以下小程序:

#include <iostream>
int main() {
   std::cout << "Time: " << __TIME__ << std::endl;
   std::cout << "Timestamp: " << __TIMESTAMP__ << std::endl;
   return 0;
}

产生以下输出:

Time and Timestamp output

如你所见,__TIME____TIMESTAMP__有相当大的区别,提前了4秒 .

为什么会这样?这两个时间怎么会有这么大的差异?

即使程序的构建时间不到 4 秒,那么这种差异从何而来?

最佳答案

这两个宏做完全不同的事情:


海合会

__TIME__

This macro expands to a string constant that describes the time at which the preprocessor is being run. The string constant contains eight characters and looks like "23:59:01". If GCC cannot determine the current time, it will emit a warning message (once per compilation) and __TIME__ will expand to "??:??:??".

( source )

__TIMESTAMP__

This macro expands to a string constant that describes the date and time of the last modification of the current source file. The string constant contains abbreviated day of the week, month, day of the month, time in hh:mm:ss form, year and looks like "Sun Sep 16 01:03:52 1973". If the day of the month is less than 10, it is padded with a space on the left.

If GCC cannot determine the current date, it will emit a warning message (once per compilation) and __TIMESTAMP__ will expand to "??? ??? ?? ??:??:?? ????".

( source )


Visual Studio

__TIME__: The most recent compilation time of the current source file. The time is a string literal of the form hh:mm:ss.
__TIMESTAMP__: The date and time of the last modification of the current source file, expressed as a string literal in the form Ddd Mmm Date hh:mm:ss yyyy, where Ddd is the abbreviated day of the week and Date is an integer from 1 to 31.

( source )

关于c++ - 哪个宏更准确? __TIME__ 还是 __TIMESTAMP__?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27691101/

相关文章:

c++ - 使用自定义扩展名保存文件

c++ - 头文件包含/转发声明

c++ - decltype 的行为

c++ - ADSI(ADsOpenObject ) 始终绑定(bind)到特定域和特定用户

c++ - 声明数组时 VS2008 错误预期常量表达式,但 GCC 中此代码没有错误

vba - 在 excel 和 word 之间共享宏模块

c++ - 如何将 __DATE__ 和 __TIME__ 宏拆分为变量声明的单独组件?

c++ - 运行时不一致 DXVA 硬件视频解码

c++ - 如何仅测试字母的 u32string(使用语言环境)

c - GCC 对宏中使用的 __builtin 函数的优化