如何打印UTC UNIX时期/时间戳?
当我打印unix时期时,使用 time.time()
之类的东西,它似乎在我的本地时区打印了时间戳。特别是从转换器的样子中: https://www.epochconverter.com 告诉我。
有谁知道我如何打印UNIX时期/时间戳,但实际上是在UTC时?太感谢了。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
没有“ UTC Unix Epoch Time”之类的东西。有Unix时期,时期。全世界都是一样的。它是独立的时区。只要正确设置了计算机时钟,
time.time()
就会为您提供正确的Unix Epoch时间。dateTime.dateTime.now().timestamp()
将为您提供相同的Unix Epoch时间。您无需转换任何东西,没有什么可转换的。现在,要解决 deepak 的评论我认为很重要:
这是
dateTime.utcnow
的已知陷阱,实际上是在手册中解决了:要稍微打包一点:
当您调用
dateTime.now()
时,它将返回您a naïvedatetime
object object(意味着它没有tzinfo
随附的时区)对于您当前的本地时间:时间代表我当前的壁时钟时间,但它不知道它在哪个时区。取决于您将其视为什么时区。
使用此对象的
timestamp()
方法将其转换为 absolute unix timestamp, 一些时区必须假定。 Python将假设您的计算机的本地时区进行转换。这对于dateTime.now()
来说是可以的,因为无论如何是在我的本地时间创建此时间戳:这是编写时正确的UNIX时间戳。不是“对于我的本地时区” ,而是全球。在撰写本文时,它是世界各地的1618559403 。
现在,如果您使用
dateTime.utcnow()
,它将为您提供当前UTC时间的naïve timestamp:请注意,从上一个
dateTime.now(),这很好,因为这是UTC时区中当前的本地时间。但是,由于该时间戳仍然是幼稚的,在将其转换为UNIX时间戳时,Python必须假设一些时区,并且会假设我的本地时区。因此,这给了我7:50:03的Unix时间戳我当地的时间:
这不是当前的Unix时间。它已经停止了两个小时。这是两个小时前的Unix时间。
如果我们要正确执行此操作,通过创建 Aware Timestamp并花费时间,我们会得到正确的UNIX时间:
请注意,人类可读时间与我们为
dateTime.utcnow(),但是Unix Timestamp是正确我们为
dateTime.now().timestamp()
提供了一个。这也与我们要获得的time.time()
:将其粘贴到您参考:
是正确的。在UTC/GMT中,此UNIX时间戳代表上午7:50,在我的本地时区是上午9:50。
如果我们采用不正确的天真
dateTime.utcnow()
timestamp1618552203.571235
,则结果是:不正确。那是2小时前,现在不是。
There's no such thing as "UTC UNIX Epoch time". There's UNIX Epoch time, period. It's the same all over the world. It is timezone independent. As long as your computer's clock is set correctly,
time.time()
will give you the correct UNIX epoch time.datetime.datetime.now().timestamp()
will give you the same UNIX Epoch time. You don't need to convert anything and there's nothing to convert.Now, to address the comment by Deepak, which I think is important:
This is a known pitfall with
datetime.utcnow
and is in fact addressed in the manual:To unpack this a bit:
When you call
datetime.now()
, it returns you a naïvedatetime
object (meaning it has notzinfo
timezone attached) for your current local time:The time represents my current wall clock time, but it does not know what timezone it's in. So this timestamp could really represent about two dozen different absolute timestamps on this planet, depending on what timezone you take it as.
When using this object's
timestamp()
method to convert it to an absolute UNIX timestamp, some timezone must be assumed. Python will assume your computer's local timezone for the conversion. Which is fine fordatetime.now()
, because this timestamp was created with my local time anyway:This is the correct UNIX timestamp at the time of writing. Not "for my local timezone", but globally. At the time of writing, it's
1618559403
everywhere in the world.Now, if you use
datetime.utcnow()
, it will give you a naïve timestamp for the current UTC time:Note how it's off by 2 hours from the previous
datetime.now()
, which is fine, because that's the current local time in the UTC timezone. But since this timestamp is still naïve, when converting it to a UNIX timestamp, Python must assume some timezone, and will assume my local timezone. So it's giving me the UNIX timestamp for 7:50:03 of my local time:Which is not the current UNIX time. It's off by two hours. It's the UNIX time of two hours ago.
If we'd do this properly, by creating an aware timestamp and taking its UNIX time, we'd get the correct UNIX time:
Note how the human readable time is the same as we got for
datetime.utcnow()
, but the UNIX timestamp is correctly the one we got fordatetime.now().timestamp()
. Which is also the same as we'd get fortime.time()
:When pasting this into https://www.epochconverter.com, which you reference:
Which is correct. In UTC/GMT, this UNIX timestamp represents 7:50am, and in my local timezone it's 9:50am.
If we take the incorrect naïve
datetime.utcnow()
timestamp1618552203.571235
, the result is:Which is incorrect. That was 2 hours ago, not now.