首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >mktime显示不一致的输出

mktime显示不一致的输出
EN

Stack Overflow用户
提问于 2012-07-02 20:09:35
回答 5查看 2.9K关注 0票数 1

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

代码语言:javascript
运行
复制
#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()之后返回的结果是不正确的。下面是上述程序的输出。

代码语言:javascript
运行
复制
$ ./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中出现),那么输出就是预期的。下面是上述程序中时区的值

代码语言:javascript
运行
复制
$ ./a.out
. . .
Current timezone is 28800
. . .

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

提前谢谢。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2012-07-02 20:35:02

我认为问题出在这里:

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()的描述中也包含了它们

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()以格林尼治标准时间/协调世界时生成时间,而不是在您的时区:

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).

EDIT:如果您只想要前一天的19:00,您可以这样做:

代码语言:javascript
运行
复制
#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;
}

输出:

代码语言:javascript
运行
复制
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
票数 2
EN

Stack Overflow用户

发布于 2012-07-02 22:58:56

您没有说明是在POSIX(-like)目标上工作,还是在某个奇怪的系统上工作,但是在前一种情况下,您可以使用POSIX公式将故障时间转换为time_t

代码语言:javascript
运行
复制
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

票数 2
EN

Stack Overflow用户

发布于 2012-07-02 20:39:14

mktime(3)使用当前时区执行转换。这在某种程度上是用the standard编写的

应按照mktime()调用tzset()的方式设置

本地时区信息。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11293422

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档