【发布时间】:2018-11-01 06:50:05
【问题描述】:
我正在尝试将一些代码从 python2 移植到 python3。 使用日期/时间操作转换某些代码时遇到问题。
Python2.7
Python 2.7.13 (default, Apr 19 2017, 02:44:33)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> import os
>>> os.environ['TZ'] = 'UTC'
>>> datetime.datetime.fromtimestamp(1461085831)
datetime.datetime(2016, 4, 19, 17, 10, 31)
Python3.6
Python 3.6.1 (default, Apr 19 2017, 21:58:41)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import datetime
>>> import os
>>> os.environ['TZ'] = 'UTC'
>>> datetime.datetime.fromtimestamp(1461085831)
datetime.datetime(2016, 4, 19, 22, 40, 31)
python2 = (2016, 4, 19, 17, 10, 31) 的结果,而python3 = (2016, 4, 19, 22, 40, 31) 的结果。
为什么会有这种差异,我应该如何克服这个差异?
【问题讨论】:
-
我在英国的 3.6 中得到
datetime.datetime(2016, 4, 19, 18, 10, 31)。我认为os.environ['TZ'] = 'UTC'没有任何作用 -
很奇怪,它显示 python2.7 和 python3.6.2 的日期时间相同
-
这些是基于云的 Linux 实例吗?
-
@Arpit 你的 python3.6 和你的 python2.7 是从同一个系统运行的吗? @roganjosh 可能是正确的,
UTC没有效果。 -
@roganjosh 我在本地机器上使用 python 2 和 3 得到了不同的结果。也许他们使用时区的方式有所不同
标签: python python-3.x datetime