【问题标题】:mktime shows inconsistent outputmktime 显示不一致的输出
【发布时间】:2012-07-02 20:27:40
【问题描述】:

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

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

提前致谢。

【问题讨论】:

  • timezone 来自哪里?
  • mktime() 假定输入是本地时间而不是 UTC(又名 GMT)。因此,如果不在格林威治,它会增加一个时移。
  • @alk, timezoneX/Open System Interfaces Extension 的一部分,在 time.h 中定义为 extern long timezone;
  • @HristoIliev 哎呀,是的.. - 谢谢你指点我这个。

标签: c time mktime


【解决方案1】:

我认为问题出在这里:

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() 会生成 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

【讨论】:

  • 实际上我使用的是使用 localtime() 的代码,但是由于我们需要在 GMT 时间而不是当前系统时间上进行计算,因为解决方案在不同的时区运行,所以我认为 mktime 可以采用 GMT时间作为输入,然后进行转换。
【解决方案2】:

您没有说明您是在使用 POSIX(-like) 目标还是一些奇怪的系统,但在前一种情况下,您可以使用 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

【讨论】:

    【解决方案3】:

    mktime(3) 使用 current 时区来执行转换。这有点神秘地写在the standard

    本地时区信息应设置为mktime() 称为tzset()

    【讨论】:

      【解决方案4】:

      mktime specification 声明其参数表示“分解时间,表示为当地时间”。因此,您传递给 mktime 的任何内容都必须是当地时间,而不是 GMT。您的变量localTime 的名称中有local,但实际上是从gmtime 获得的,因此它的结果描述了当前时间格林威治标准时间。将其传递给 mktime 是不一致的。

      【讨论】:

        【解决方案5】:

        在某些系统上,还有一个 timegm 函数应该做正确的事情。
        我刚刚在 Linux 上进行了检查,它产生了正确的值(例如 timegm (gmtime (time)) == time)。

        Here's如何在Windows中使用。
        这是similar answer

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-06-25
          • 2018-11-10
          • 2021-06-11
          • 1970-01-01
          • 2013-07-10
          • 2013-12-28
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多