在尝试编写比给定时间少24小时的代码时,mktime()
显示输出不一致。我计算它类似于:current_time(GMT) - 86400
,它应该返回正确的值。我们需要做的就是根据输入时间计算;我们使用mktime()
来更改时间并获得GMT时间,然后进行常规计算。我在下面提供了我的代码。
#include <stdio.h>
#include <time.h>
int main()
{
time_t currentTime, tempTime;
struct tm *localTime;
time(¤tTime);
//localTime = localtime(¤tTime);
localTime = gmtime(¤tTime); //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()
存在这种不一致的行为,尽管手册页没有提到对时区的这种调整。
在进行此类转换时,我们是否还缺少某些内容?
提前致谢。
答案 0 :(得分:2)
我认为问题在于:
注意单词local time
。 C标准在mktime()
:
The mktime function converts the broken-down time, expressed as local time, in the
structure pointed to by timeptr into a calendar time value with the same encoding as
that of the values returned by the time function.
gmtime()
以GMT / UTC而不是您所在的时区生成时间:
编辑:如果您只想要上一个GMT / UTC日的19:00,您可以这样做:
#include <stdio.h>
#include <time.h>
int main(void)
{
time_t currentTime;
struct tm *brokenDownTime;
time(¤tTime);
// get the time in GMT as we are in PDT
brokenDownTime = gmtime(¤tTime);
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(¤tTime);
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(¤tTime);
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(¤tTime);
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
答案 1 :(得分:1)
mktime(3)
使用当前时区执行转换。这在the standard:
本地时区信息应设置为
mktime()
tzset()
。
答案 2 :(得分:1)
mktime specification表示其参数代表“分解时间,表示为本地时间”。所以你传递给mktime的任何东西都必须在当地时间,而不是GMT。您的变量localTime
名称中包含local
,但实际上是从gmtime
获得的,因此其结果描述了GMT 中的当前时间。将其传递给mktime
是不一致的。
答案 3 :(得分:1)
您没有说明您是在使用POSIX(类似)目标还是某些古怪的系统,但在前一种情况下,您可以使用POSIX公式将故障时间转换为time_t
:
tm_sec + tm_min*60 + tm_hour*3600 + tm_yday*86400 +
(tm_year-70)*31536000 + ((tm_year-69)/4)*86400 -
((tm_year-1)/100)*86400 + ((tm_year+299)/400)*86400
来源:http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_15
答案 4 :(得分:0)
在某些系统上还有timegm功能应该做正确的事 我刚刚检查过Linux并且它产生了正确的值(例如timegm(gmtime(time))== time)。
Here's如何在Windows中使用它 这是similar answer。