mktime显示不一致的输出 [英] mktime shows inconsistent output

查看:229
本文介绍了mktime显示不一致的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

While trying to write code which returns 24 hours less than a given time, mktime()shows inconsistent output. I calculate it similar to this: current_time(GMT) - 86400 which should return the right value. All we need to do is calculate based on the input time; we used mktime() to change the time and get the GMT time and then do the regular calculation. I included my code below.

#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()之后,返回的结果不正确.下面是上面程序的输出.

But when we check the output it is coming back incorrect after the call to call mktime(). Below is the output of above program.

$ ./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中显示),则输出将如预期的那样.下面是上面程序中的时区值

Now if we un-comment the line which subtracts the timezone (present in time.h), then the output is as expected. Below is the value for timezone in above program

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

所以,尽管手册页没有提到对时区的这种调整,但为什么mktime()却有这种不一致的行为. 进行此类转换时我们会缺少什么东西吗?

So why is there such inconsistent behavior for mktime() although the man pages do not mention such adjustment of the timezone. Is there something we are missing while doing such conversions?

谢谢.

推荐答案

我认为问题出在这里:

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()的描述中也有它们:

Note the words local time. The C standard has them too in the description of 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.

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而不是您所在的时区产生时间:

gmtime(), on the other hand, produces time in GMT/UTC and not in your time zone:

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

编辑:如果您只想在格林尼治标准时间/UTC前一天的19:00,可以执行以下操作:

EDIT: If you just want 19:00 of the previous GMT/UTC day, you can do this:

#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

这篇关于mktime显示不一致的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆