Python 获取 datetime.datetime.fromtimestamp() 使用的时区

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18812638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:56:08  来源:igfitidea点击:

Get timezone used by datetime.datetime.fromtimestamp()

pythontimezone

提问by Feuermurmel

Is it possible, and if yes, how, to get the time zone (i.e. the UTC offset or a datetime.timezoneinstance with that offset) that is used by datetime.datetime.fromtimestamp()to convert a POSIX timestamp(seconds since the epoch) to a datetimeobject?

是否有可能,如果是,如何获取datetime.timezone用于datetime.datetime.fromtimestamp()POSIX 时间戳(自纪元以来的秒数)转换为datetime对象的时区(即 UTC 偏移量或具有该偏移量的实例)?

datetime.datetime.fromtimestamp()converts a POSIX timestamp to a naive datetimeobject (i.e. without a tzinfo), but does so using the system's locale to adjust it to the local timezone and the UTC offset that was in effect at that time.

datetime.datetime.fromtimestamp()将 POSIX 时间戳转换为原始datetime对象(即没有tzinfo),但这样做是使用系统的语言环境将其调整为本地时区和当时有效的 UTC 偏移量。

For example, using the date 2008-12-27 midnight UTC (40 * 356 * 86400 seconds since the epoch):

例如,使用日期 2008-12-27 午夜 UTC(自纪元以来的 40 * 356 * 86400 秒):

>>> datetime.datetime.fromtimestamp(40 * 356 * 86400)
datetime.datetime(2008, 12, 27, 1, 0)

That timestamp is converted to a datetimeobject at 1 o'clock in the morning (which it was at that time, here in an CET/CEST timezone). 100 days later, this is the result:

该时间戳datetime在凌晨 1 点转换为对象(当时是 CET/CEST 时区)。100天后,结果如下:

>>> datetime.datetime.fromtimestamp((40 * 356 + 100) * 86400)
datetime.datetime(2009, 4, 6, 2, 0)

Which is 2 o'clock in the morning. This is because by then, DST was active.

这是凌晨2点。这是因为到那时,DST 是活跃的。

I'd expected that datetime.datetime.fromtimestamp()would set the tzinfoit uses in the returned datetimeinstance, but it doesn't.

我原以为datetime.datetime.fromtimestamp()tzinfo在返回的datetime实例中设置它使用的值,但事实并非如此。

采纳答案by jfs

datetime.fromtimestamp(ts)converts "seconds since the epoch" to a naive datetime object that represents local time. tzinfois always Nonein this case.

datetime.fromtimestamp(ts)将“自纪元以来的秒数”转换为表示本地时间的原始日期时间对象。tzinfo总是None在这种情况下。

Local timezone may have had a different UTC offset in the past. On some systems that provide access to a historical timezone database, fromtimestamp()may take it into account.

本地时区过去可能有不同的 UTC 偏移量。在某些提供对历史时区数据库的访问的系统上,fromtimestamp()可能会将其考虑在内。

To get the UTC offset used by fromtimestamp():

要获取 UTC 使用的偏移量fromtimestamp()

utc_offset = fromtimestamp(ts) - utcfromtimestamp(ts)

See also, Getting computer's utc offset in Python.

另请参阅在 Python 中获取计算机的 utc 偏移量

回答by AChampion

Using time.gmtimeyou can extract the timezone as described in this previous answer: Get TZ information of the system in Python?.

使用time.gmtime您可以提取时区,如上一个答案中所述:在 Python 中获取系统的 TZ 信息?.

>>> from __future__ import print_function
>>> from time import gmtime, strftime
>>> print(strftime("%z", gmtime()))
-0600

Prints -06:00 for my CST laptop in both python-2.7 and python-3.3 You can also use localtime() to get a local time struct.

在 python-2.7 和 python-3.3 中为我的 CST 笔记本电脑打印 -06:00 您还可以使用 localtime() 来获取本地时间结构。

>>> from __future__ import print_function
>>> from time import localtime
>>> lt = localtime()
>>> print(lt.tm_zone)
"CDT"
>>> print(lt.tm_gmtoff/(60*60))
-5.0
>>> print(lt.tm_gmtoff/(60*60) - (1 if lt.tm_isdst == 1 else 0)) # Adjusted for DST
-6.0

Hope this helps

希望这可以帮助

回答by Matt Johnson-Pint

From the Python documentation:

Python 文档

classmethoddatetime.fromtimestamp(timestamp, tz=None)

Return the local date and time corresponding to the POSIX timestamp, such as is returned by time.time(). If optional argument tzis Noneor not specified, the timestamp is converted to the platform's local date and time, and the returned datetimeobject is naive.

Else tzmust be an instance of a class tzinfosubclass, and the timestamp is converted to tz‘s time zone. In this case the result is equivalent to tz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz)).

类方法datetime.fromtimestamp( timestamp, tz=None)

返回 POSIX 时间戳对应的本地日期和时间,如由 返回time.time()。如果可选参数TZNone或没有指定,时间戳转换为平台的本地日期和时间,返回的datetime对象是幼稚的。

否则tz必须是类tzinfo子类的实例,时间戳转换为tz的时区。在这种情况下,结果等效于tz.fromutc(datetime.utcfromtimestamp(timestamp).replace(tzinfo=tz))

The key part of this description as it relates to your question is that when you don't specify a time zone, not only does it use the local time zone, but the result is naive. You seem to want it to be aware.

此描述与您的问题相关的关键部分是,当您不指定时区时,它不仅使用本地时区,而且结果是naive。你似乎想让它知道

This is a particular distinction made by Python, and is discussed right at the very top of the datetime documentation.

这是 Python 做出的一个特殊区别,并且在日期时间文档的最顶部进行讨论。

If what you want is a datetimethat is awareof the local time zone, try the tzlocallibrary. It is focused on that particular problem. See also this question.

如果您想要的datetime了解本地时区的,请尝试使用tzlocal库。它专注于那个特定的问题。另请参阅此问题

回答by Emil Stenstr?m

If you know the timezone of the timestamp you want to convert, you can simply send it in while calling fromtimestamp:

如果您知道要转换的时间戳的时区,则可以在调用时简单地将其发送fromtimestamp

>>> from datetime import datetime
>>> import pytz
>>>
>>> datetime.fromtimestamp(1562684265, pytz.timezone("Europe/Stockholm"))
datetime.datetime(2019, 7, 9, 16, 57, 45, tzinfo=<DstTzInfo 'Europe/Stockholm' CEST+2:00:00 DST>)
>>>
>>> datetime.fromtimestamp(1562684265, pytz.timezone("UTC"))
datetime.datetime(2019, 7, 9, 14, 57, 45, tzinfo=<UTC>)