fromtimestamp返回不同的结果 [英] fromtimestamp returns different results

查看:125
本文介绍了fromtimestamp返回不同的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

import datetime

dt = 1546955400
print(datetime.datetime.fromtimestamp(dt))

当我在本地计算机上运行此代码时,我得到了正确的(预期)时间

When I run this code on my local machine, I get the correct (expected) time which is

2019-01-08 15:50:00

但是我尝试在VM上运行此完全相同的代码,结果是
2019-01-08 13:50:00 (两个小时之前)。为什么会发生这种情况,以及如何解决它,以便无论代码在哪里运行,我总是得到第一个?

However I tried running this exact same code on a VM and the result was 2019-01-08 13:50:00 (two hours earlier). Why is this is happening and how can I fix it so that I always get the first one regardless of where the code is running?

推荐答案

datetime.datetime.fromtimestamp()返回本地时间。从文档


返回与POSIX时间戳相对应的本地日期和时间,例如由 time.time()。如果可选参数 tz None 或未指定,则时间戳将转换为平台的本地日期和时间,并且返回的日期时间对象是天真的。

Return the local date and time corresponding to the POSIX timestamp, such as is returned by time.time(). If optional argument tz is None or not specified, the timestamp is converted to the platform’s local date and time, and the returned datetime object is naive.

时间戳记值与 UNIX的 epoch 值,UTC时区,1970年1月1日午夜。本地时间是相对于UTC(本地时区)在系统范围内配置的偏移量。

The timestamp value is an offset in seconds from the UNIX epoch value, midnight 1 January 1970, in the UTC timezone. The local time is a system-wide configured offset from UTC, the local timezone.

如果VM产生意外结果,则需要配置OS的时区。

If your VM is producing unexpected results, you need to configure the timezone of the OS.

或者,忽略时区,仅处理UTC时区中的时间。对于时间戳,这意味着使用 datetime .datetime.utcfromtimestamp()函数

Alternatively, ignore timezones and only deal with time in the UTC timezone. For timestamps, that means using the datetime.datetime.utcfromtimestamp() function.

您的特定时间戳是13:50 UTC:

Your specific timestamp is 13:50 UTC:

>>> dt = 1546955400
>>> from datetime import datetime
>>> datetime.utcfromtimestamp(dt)
datetime.datetime(2019, 1, 8, 13, 50)
>>> print(_)
2019-01-08 13:50:00

所以您的VM设置为UTC或GMT时区(后者当前为UTC + 0,直到切换到英国夏令时时区BST)。给定您指定的位置,您的本地系统位于 UTC + 2时区中您的个人资料将是欧洲东部时间复活节(EEE)

so your VM is either set to the UTC or the GMT timezone (the latter is currently at UTC+0, until the switch to the UK daylight saving timezone BST). Your local system is in a UTC+2 timezone, given your stated location from your profile that'd be EEE, Easter European Time.

另一种选择是通过传入 tz 参数来创建时区感知时间戳。如果您有特定的UTC偏移量,只需创建 datetime.timezone()实例的偏移量:

Another option is to create a timezone-aware timestamp by passing in a tz argument. If you have a specific UTC offset, just create a datetime.timezone() instance for that offset:

utcplus2 = datetime.timezone(datetime.timedelta(hours=2))
datetime.datetime.fromtimestamp(dt, utcplus2)

但是,通常最好在世界各地的UTC datetime 实例上进行存储和操作,并且仅在向用户显示信息时才转换为特定的时区。这样可以简化日期时间的处理,因为它可以避免许多时区的极端情况和问题,例如将来自不同时区和时区的日期时间信息与夏令时和冬令时区分开来。

However, it is usually better to store and operate on UTC datetime instances everywhere, and only convert to specific timezones when displaying information to users. This simplifies datetime handling as it lets you avoid a number of timezone corner cases and problems, such as mixing datetime information from different timezones and timezones with a summer and winter time distinction.

这篇关于fromtimestamp返回不同的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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