gpt4 book ai didi

c - mktime 显示不一致的输出

转载 作者:太空宇宙 更新时间:2023-11-04 01:11:39 28 4
gpt4 key购买 nike

在尝试编写返回比给定时间少 24 小时的代码时,mktime() 显示不一致的输出。我的计算类似于:current_time(GMT) - 86400 应该返回正确的值。我们需要做的就是根据输入的时间进行计算;我们使用 mktime() 来改变时间并得到 GMT 时间,然后进行常规计算。我在下面包含了我的代码。

#include <stdio.h>
#include <time.h>

int main()
{
time_t currentTime, tempTime;
struct tm *localTime;

time(&currentTime);
//localTime = localtime(&currentTime);
localTime = gmtime(&currentTime); //get the time in GMT as we are in PDT

printf("Time %2d:%02d\n", (localTime->tm_hour)%24, localTime->tm_min);
localTime->tm_hour = 19; // Set the time to 19:00 GMT
localTime->tm_min = 0;
localTime->tm_sec = 0;
tempTime = mktime(localTime);
//tempTime = mktime(localTime) - timezone;

printf("Current time is %ld and day before time is %ld\n", currentTime, (currentTime - 86400));
printf("Current timezone is %ld \n", timezone);

printf("New time is %ld and day before time is %ld\n",tempTime, (tempTime - 86400));
}

但是当我们检查输出时,它在调用 mktime() 后返回不正确。以下是上述程序的输出。

$ ./a.out
Time 11:51
Current time is 1341229916 and day before time is 1341143516
New time is 1341284400 and day before time is 1341198000
$ ./print_gmt 1341229916
Mon Jul 2 11:51:56 2012
$ ./print_gmt 1341143516
Sun Jul 1 11:51:56 2012
$ ./print_gmt 1341284400
Tue Jul 3 03:00:00 2012
$ ./print_gmt 1341198000
Mon Jul 2 03:00:00 2012
$ date
Mon Jul 2 04:52:46 PDT 2012

现在,如果我们取消注释减去时区的行(存在于 time.h 中),则输出符合预期。以下是上述程序中时区的值

$ ./a.out
. . .
Current timezone is 28800
. . .

那么为什么 mktime() 会出现这种不一致的行为,尽管手册页没有提到时区的这种调整。在进行此类转换时,我们是否遗漏了什么?

提前致谢。

最佳答案

我觉得问题出在这里:

The mktime() function shall convert the broken-down time, expressed as local time, in the structure pointed to by timeptr, into a time since the Epoch value...

注意单词local time。 C 标准在 mktime() 的描述中也有它们:

mktime函数将以本地时间表示的故障时间转换为
timeptr 指向的结构为一个日历时间值,其编码与
时间函数返回的值。

另一方面,

gmtime() 生成 GMT/UTC 时间,而不是您所在时区的时间:

The gmtime() function shall convert the time in seconds since the Epoch pointed to by timer into a broken-down time, expressed as Coordinated Universal Time (UTC).

编辑:如果您只想要前一个 GMT/UTC 日的 19:00,您可以这样做:

#include <stdio.h>
#include <time.h>

int main(void)
{
time_t currentTime;
struct tm *brokenDownTime;

time(&currentTime);

// get the time in GMT as we are in PDT
brokenDownTime = gmtime(&currentTime);
printf("Current Time (GMT): %2d:%02d\n"
" seconds since Epoch: %ld\n",
brokenDownTime->tm_hour,
brokenDownTime->tm_min,
(long)currentTime);

// "Unwind" time to 0:00:00 (assuming time_t is an integer):
currentTime /= 24 * (time_t)3600;
currentTime *= 24 * (time_t)3600;

brokenDownTime = gmtime(&currentTime);
printf("Time at the beginning of the current GMT day: %2d:%02d\n"
" seconds since Epoch: %ld\n",
brokenDownTime->tm_hour,
brokenDownTime->tm_min,
(long)currentTime);

// Add 19 hours:
currentTime += 19 * (time_t)3600;

brokenDownTime = gmtime(&currentTime);
printf("Time at 19:00:00 of the current GMT day: %2d:%02d\n"
" seconds since Epoch: %ld\n",
brokenDownTime->tm_hour,
brokenDownTime->tm_min,
(long)currentTime);

// Subtract 1 day:
currentTime -= 24 * (time_t)3600;

brokenDownTime = gmtime(&currentTime);
printf("Time at 19:00:00 of the previous GMT day: %2d:%02d\n"
" seconds since Epoch: %ld\n",
brokenDownTime->tm_hour,
brokenDownTime->tm_min,
(long)currentTime);

return 0;
}

输出:

Current Time (GMT): 13:23
seconds since Epoch: 1341235429
Time at the beginning of the current GMT day: 0:00
seconds since Epoch: 1341187200
Time at 19:00:00 of the current GMT day: 19:00
seconds since Epoch: 1341255600
Time at 19:00:00 of the previous GMT day: 19:00
seconds since Epoch: 1341169200

关于c - mktime 显示不一致的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11293422/

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