为什么python时间有61秒

sto*_*orm 12 python python-2.7

有没有人注意到Python datetime中的秒的间隔是[00,61],请参见本页底部的表格. http://docs.python.org/library/datetime.html#strftime-strptime-behavior

为什么?

Eri*_*got 19

答案在页面中稍微向下:

范围真的是0到61; 根据Posix标准,这可以说明闰秒和(非常罕见的)双闰秒.时间模块可产生并因为它是基于POSIX标准并接受闰秒,但datetime模块不接受闰秒在strptime()输入,也不会产生它们的strftime()输出.

事实上,这是一种有趣的行为.

  • 我刚刚意识到Python没有闰秒数据库(类似于时区的tzinfo),因此没有办法验证某个闰秒是否合法...这意味着转换到Unix时间是有损的:`t1 = time.strptime("1997年6月30日23:59:60","%d%b%Y%H:%M:%S"); t2 = time.strptime("01 Jul 1997 00:00:00","%d%b%Y%H:%M:%S")`,然后`t1 == t2`按预期产生`False`,但是`calendar.timegm(t1)== calendar.timegm(t2)`产生'真' (3认同)
  • tz数据库(可通过`pytz` Python包获得)有一个闰秒列表.[您可以使用它来检查给定时间是否为闰秒.](http://stackoverflow.com/q/19332902/4279) (3认同)