【问题标题】:std::localtime one second leads to 30 mins gapstd::localtime 一秒导致 30 分钟的差距
【发布时间】:2017-10-26 20:29:54
【问题描述】:
#include <iostream>
int main(){
    std::time_t t = 893665799;
    std::tm * tm = std::localtime(&t);
    printf("local time duration =>year:%d, month:%d, day:%d, hour:%d, min:%d, sec:%d\n",tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);

    t = 893665800;
    tm = std::localtime(&t);
    printf("local time duration =>year:%d, month:%d, day:%d, hour:%d, min:%d, sec:%d\n",tm->tm_year, tm->tm_mon, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
}

893665799 是一个神奇的 UTC。

当您将时区更改为 皮特凯恩群岛

日志如下所示

本地时长 =>年:98,月:3,日:26,小时:23,分钟:59,秒:59

本地时长 =>年:98,月:3,日:27,小时:0,分钟:30,秒:0

发生了什么事?为什么1秒导致30分钟的差距?

【问题讨论】:

  • 您是否将时区更改为皮特凯恩群岛?

标签: c++ localtime


【解决方案1】:

鉴于给出的time_t 正好是进入新小时的前一秒,这几乎肯定与夏令时或其他一些调整有关。

而且,有点 Google-fu,here 它是:

1998 年 4 月 27 日 - 时区更改(PNT → PST)

当当地标准时间快要到 1998 年 4 月 27 日星期一 12:00:00 时,午夜时钟被调快为 1998 年 4 月 27 日星期一上午 12:30:00 时的午夜时钟。

这实际上经常发生,世界各地的各个国家/地区都在随意更改当地时间:-)

【讨论】:

    【解决方案2】:

    正如另一个答案所提到的,当地时间一直在变化,夏令时等等。

    “可移植”表示一个时刻几乎是不可能的,因此发明了 UTC,这是一个神奇的时区*,它是表示时间的标准方式,只考虑闰秒。

    正确处理所有这些情况非常乏味,但幸运的是 Howard Hinnant 有 this amazing library 为您做所有事情 :)

    * UTC 并不是真正的时区

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-28
      • 1970-01-01
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-09
      相关资源
      最近更新 更多