gpt4 book ai didi

c++ - 为什么 gmtime 和 localtime 给我相同的结果?

转载 作者:行者123 更新时间:2023-11-30 16:26:07 25 4
gpt4 key购买 nike

我有以下测试代码来查看 gmtime 和 localtime 之间的差异。但他们给了我相同的结果: UTC:2013-05-02T13:59:58 Local:2013-05-02T13:59:58

time_t now;
time(&now);
tm *pTimeStruct = gmtime(&now);
tm *plocalTimeStruct = localtime(&now);

string timeStr = "";
char timeBuf[64] = {'\0'};

sprintf(timeBuf,"UTC:%-4.4d-%-2.2d-%-2.2dT%-2.2d:%-2.2d:%-2.2d "
"Local:%-4.4d-%-2.2d-%-2.2dT%-2.2d:%-2.2d:%-2.2d",
(pTimeStruct->tm_year + 1900),
(pTimeStruct->tm_mon + 1),
pTimeStruct->tm_mday,
pTimeStruct->tm_hour,
pTimeStruct->tm_min,
pTimeStruct->tm_sec,
(plocalTimeStruct->tm_year + 1900),
(plocalTimeStruct->tm_mon + 1),
plocalTimeStruct->tm_mday,
plocalTimeStruct->tm_hour,
plocalTimeStruct->tm_min,
plocalTimeStruct->tm_sec);

timeStr += timeBuf;
cout << timeStr << endl;

编辑:

我在东部时区。

编辑2:

更新的代码使用 diff 结构,但得到相同的结果:

        time_t now;
time(&now);
time_t now2;
time(&now2);
tm *pTimeStruct = gmtime(&now);
tm *plocalTimeStruct = localtime(&now2);

最佳答案

您需要复制对 gmtimelocaltime 的调用之间的值:

The return value points to a statically allocated struct which might be overwritten by subsequent calls to any of the date and time functions.

说我系统上的手册页。至少在 Linux 上这是常见行为。

关于c++ - 为什么 gmtime 和 localtime 给我相同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53149399/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com