Python datetime.timestamp()问题 [英] Python datetime.timestamp() issue

查看:454
本文介绍了Python datetime.timestamp()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现datetime.timestamp()函数在Linux和Windows上返回不同的值。复制它的方法很简单:

I find the datetime.timestamp() function return different value on Linux and Windows. Here is a simple to to replicate it:

from datetime import date, time, datetime, timedelta

def main(): 
    dt = datetime(2000, 1, 1)
    edt = datetime(2006, 12, 31)

    fname = 't1.csv'
    f = open(fname, 'w')
    f.write('date,timestamp\n')
    while dt <= edt:
        f.write('{0:%Y-%m-%d},{1:.0f}\n'.format(dt, dt.timestamp()))
        dt += timedelta(days=1)
    f.close()

return 0

最后一个不同Windows的一部分:(Windows7 64 + Python3.4.3 64)

Here is the LAST different part from Windows: (Windows7 64 + Python3.4.3 64)

...
2006-10-30,1162180800
2006-10-31,1162267200
2006-11-01,1162353600
2006-11-02,1162440000
2006-11-03,1162526400
2006-11-04,1162612800
2006-11-05,1162699200
...

以下是相应的Linux输出:(RedHat6 64 + Python 3.4.3 64)

Here is the corresponding Linux output: (RedHat6 64 + Python 3.4.3 64)

...
2006-10-30,1162184400
2006-10-31,1162270800
2006-11-01,1162357200
2006-11-02,1162443600
2006-11-03,1162530000
2006-11-04,1162616400
2006-11-05,1162702800
...

系统全部使用带有自动DST调整功能的EST。一些观察结果:

Systems all using the EST with automatic DST adjustment. Some observations:


  • Linux输出似乎是正确的

  • 2006年之后没有发现差异(I没有在2015年12月31日之前进行测试)

  • 差异似乎是1小时/ 3600秒

  • 差异似乎仅在该年DST更改前后的天数(或欧盟/美国DST更改日期不同的时间。)

  • the linux output seems to be correct
  • no difference observed after 2006 (I did not test above 12/31/2015)
  • the difference seems to be 1 hour/3600 seconds
  • the difference seems to happen only in those days around the DST change in that year (or around the time EU/US has different DST change date.)

只是想知道为什么要加上时间戳()函数在Windows和Linux上的行为有所不同。

Just wondering why timestamp() function behaves differently on windows and linux.

推荐答案

datetime.timestamp()在朴素的datetime对象上调用 mktime()在内部,即,输入被解释为本地时间。不同系统之间的本地时间定义可能会有所不同。

datetime.timestamp() on a naive datetime object calls mktime() internally i.e., the input is interpreted as the local time. Local time definitions may differ between systems.

C mktime()如果本地时区具有过去的utc偏移量不同,并且未使用历史时区数据库。 python 无权访问 tz数据库在Windows上。

C mktime() may return a wrong result if the local timezone had different utc offset in the past and a historical timezone database is not used. python has no access to the tz database on Windows.

如果应用程序使用不同的tzdata版本,则可能会得到不同的结果。如果使用不同的 mktime()实现(所有其他条件都相同),则在模棱两可的时间(例如,在DST转换期间),您也可能会得到不同的结果。

You may get different results if applications use different tzdata versions. You may also get different results for ambiguous times (e.g., during DST transitions) if different mktime() implementations are used (all else being equal).

要在不同系统上获得相同的结果,使用 pytz 模块(在与Python包zoneinfo捆绑使用的不同系统上的相同版本):

To get the same result on different systems, use pytz module (the same version on different systems that uses bundled with the Python package zoneinfo):

#!/usr/bin/env python3
from datetime import datetime
import pytz  # $ pip install pytz

tz = pytz.timezone('America/New_York')
for tt in [(2006, 10, 30),
           (2006, 10, 31),
           (2006, 11, 1),
           (2006, 11, 2),
           (2006, 11, 3),
           (2006, 11, 4),
           (2006, 11, 5)]:
    dt = datetime(*tt)
    ts = tz.localize(dt, is_dst=None).timestamp()
    print("{dt:%Y-%m-%d},{ts:.0f}".format(**vars()))



输出( pytz。 __version__ == 2014.10



Output (pytz.__version__ == 2014.10)

2006-10-30,1162184400
2006-10-31,1162270800
2006-11-01,1162357200
2006-11-02,1162443600
2006-11-03,1162530000
2006-11-04,1162616400
2006-11-05,1162702800

这篇关于Python datetime.timestamp()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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