python:时间戳基于utcnow()时,utcfromtimestamp与fromtimestamp [英] python: utcfromtimestamp vs fromtimestamp, when the timestamp is based on utcnow()

查看:391
本文介绍了python:时间戳基于utcnow()时,utcfromtimestamp与fromtimestamp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以肯定这很容易,但是我不明白。

Pretty sure it's an easy one but I don't get it.

我的本​​地TZ当前是GMT + 3,并且当我从<$ c获取时间戳时$ c> datetime.utcnow()。timestamp()它确实比 datetime.now()。timestamp()少了3个小时

My local TZ is currently GMT+3, and when I take timestamp from datetime.utcnow().timestamp() it is indeed giving me 3 hours less than datetime.now().timestamp()

在流程中的另一个过程中,我采用了utc时间戳,需要将其转换为日期时间。

During another process in my flow, I take that utc timestamp and need to turn it into datetime.

在执行 fromtimestamp 时,我得到了正确的utc小时,但是当我使用 utcfromtimestamp 时,我又得到了3分。时间偏移量。

When I'm doing fromtimestamp I get the right utc hour, but when I'm using utcfromtimestamp I get another 3 hours offset.

不过,文档要求我将 fromtimestamp 用于本地时区,将 utcfromtimestamp 以获取utc用法。

The documentation, though, asks me to use fromtimestamp for local timezone, and utcfromtimestamp for utc usages.

我缺少什么?这两个函数的初始假设是时间戳是在本地时区给出的吗?

What am I missing ? is the initial assumption for both funcs is that the timestamp is given in local timezone ?

谢谢:)

推荐答案

同时使用日期时间对象及其POSIX时间戳时要注意的关键是Python假定朴素的日期时间对象(没有时区信息的对象)引用本地时间。 (操作系统设置)。相反,POSIX时间戳(应该)始终指的是自该时期以来的UTC秒。您可以通过 time.time明确获得它。 ) 。在您的示例中,发生了不太明显的事情:

The key thing to notice when working with datetime objects and their POSIX timestamps at the same time is that naive datetime objects (the ones without timezone information) are assumed by Python to refer to local time (OS setting). In contrast, a POSIX timestamp (should) always refer to UTC seconds since the epoch. You can unambiguously obtain it by time.time(). In your example, not-so-obvious things happen:

1) datetime.now()。timestamp()-now()为您提供一个类似于本地时间的朴素的datetime对象。如果您需要timestamp(),Python会将日期时间转换为UTC并为其计算时间戳。

1) datetime.now().timestamp() - now() gives you a naive datetime object that resembles local time. If you call for the timestamp(), Python converts the datetime to UTC and calculates the timestamp for that.

2) datetime.utcnow() .timestamp()-utcnow()给您一个类似于UTC的朴素的datetime对象。但是,如果您调用timestamp(),Python会(因为天真)假定datetime是本地时间-并在计算时间戳之前再次转换为UTC!因此,由此产生的时间戳与UTC的距离是本地时间UTC偏移的两倍...

2) datetime.utcnow().timestamp() - utcnow() gives you a naive datetime object that resembles UTC. However, if you call timestamp(), Python assumes (since naive) that the datetime is local time - and converts to UTC again before calculating the timestamp! The resulting timestamp is therefore off from UTC by twice your local time's UTC offset...

一个代码示例。让我们做一些时间戳记。请注意,我使用的是UTC + 2,所以偏移量是-7200秒。

A code example. Let's make some timestamps. Note that I'm on UTC+2, so offset is -7200 s.

import time
from datetime import datetime, timezone

ts_ref = time.time() # reference POSIX timestamp

ts_utcnow = datetime.utcnow().timestamp() # dt obj UTC but naive - so also assumed local

ts_now = datetime.now().timestamp() # dt obj naive, assumed local

ts_loc_utc = datetime.now(tz=timezone.utc).timestamp() # dt obj localized to UTC

print(int(ts_utcnow - ts_ref))
# -7200 # -> ts_utcnow doesn't refer to UTC!
print(int(ts_now - ts_ref))
# 0 # -> correct
print(int(ts_loc_utc - ts_ref))
# 0 # -> correct

我希望这可以澄清,如果您执行 datetime.utcfromtimestamp(ts_utcnow) ,您将获得本地时间UTC偏移量的两倍。 Python假定(我认为这很理智)时间戳记是指UTC的-实际上,它不是。

I hope this clarifies that if you do datetime.utcfromtimestamp(ts_utcnow), you get double the local time's UTC offset. Python assumes (which I think is pretty sane) that the timestamp refers to UTC - which in fact, it does not.

我的建议是使用可识别时区的datetime对象例如 datetime.now(tz = timezone.utc)。在处理日期时间和时区时, dateutil 库也可能非常有用。如果您想深入研究,请查看 datetime src代码。这也有助于澄清您遇到的问题。

My suggestion would be to use timezone-aware datetime objects; like datetime.now(tz=timezone.utc). The dateutil library can also be very helpful when working with datetime and timezones. And if you want to dig deep, have a look at the datetime src code. That could also help clarifying the issue you encountered.

这篇关于python:时间戳基于utcnow()时,utcfromtimestamp与fromtimestamp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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