如何打印UTC UNIX时期/时间戳?

发布于 2025-02-01 00:17:26 字数 249 浏览 2 评论 0 原文

当我打印unix时期时,使用 time.time()之类的东西,它似乎在我的本地时区打印了时间戳。特别是从转换器的样子中: https://www.epochconverter.com 告诉我。

有谁知道我如何打印UNIX时期/时间戳,但实际上是在UTC时?太感谢了。

When I print the unix epoch, with something like time.time() it seems to print the timestamp in my local timezone. Especially from what converters like: https://www.epochconverter.com tell me.

Does anyone know how I can print the UNIX Epoch/timestamp but actually in UTC time? Thank you so much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

刘备忘录 2025-02-08 00:17:26

没有“ UTC Unix Epoch Time”之类的东西。有Unix时期,时期。全世界都是一样的。它是独立的时区。只要正确设置了计算机时钟, time.time()就会为您提供正确的Unix Epoch时间。 dateTime.dateTime.now().timestamp()将为您提供相同的Unix Epoch时间。您无需转换任何东西,没有什么可转换的。

现在,要解决 deepak 的评论我认为很重要:

我确实会获得 dateTime.now() vs dateTime.utcnow()对应于我的本地时间与UTC的不同响应。因此,经过的秒数与我的19800年代相差为19800年代(我的时区+5:30)。

这是 dateTime.utcnow 的已知陷阱,实际上是在手册中解决了

警告:因为幼稚 dateTime 对象由许多 datetime 方法作为当地时间处理,因此,最好使用Aware DateTimes来表示时间在UTC中。因此,创建代表UTC当前时间的对象的推荐方法是调用 dateTime.now(timezone.utc)

要稍微打包一点:

当您调用 dateTime.now()时,它将返回您a naïve datetime object object(意味着它没有 tzinfo 随附的时区)对于您当前的本地时间:

>>> datetime.now()
datetime.datetime(2021, 4, 16, 9, 50, 3, 571235)

时间代表我当前的壁时钟时间,但它不知道它在哪个时区。取决于您将其视为什么时区。

使用此对象的 timestamp()方法将其转换为 absolute unix timestamp, 一些时区必须假定。 Python将假设您的计算机的本地时区进行转换。这对于 dateTime.now()来说是可以的,因为无论如何是在我的本地时间创建此时间戳:

>>> datetime.now().timestamp()
1618559403.571235

这是编写时正确的UNIX时间戳。不是“对于我的本地时区” ,而是全球。在撰写本文时,它是世界各地的1618559403 。

现在,如果您使用 dateTime.utcnow(),它将为您提供当前UTC时间的naïve timestamp:

>>> datetime.utcnow()
datetime.datetime(2021, 4, 16, 7, 50, 3, 571235)

请注意,从上一个 dateTime.now(),这很好,因为这是UTC时区中当前的本地时间。但是,由于该时间戳仍然是幼稚的,在将其转换为UNIX时间戳时,Python必须假设一些时区,并且会假设我的本地时区。因此,这给了我7:50:03的Unix时间戳我当地的时间

>>> datetime.utcnow().timestamp()
1618552203.571235

这不是当前的Unix时间。它已经停止了两个小时。这是两个小时前的Unix时间。

如果我们要正确执行此操作,通过创建 Aware Timestamp并花费时间,我们会得到正确的UNIX时间:

>>> datetime.now(timezone.utc)
datetime.datetime(2021, 4, 16, 7, 50, 3, 571235, tzinfo=datetime.timezone.utc)
>>> datetime.now(timezone.utc).timestamp()
1618559403.571235

请注意,人类可读时间与我们为 dateTime.utcnow(),但是Unix Timestamp是正确我们为 dateTime.now().timestamp()提供了一个。这也与我们要获得的 time.time()

>>> from time import time
>>> time()
1618559403.571235

将其粘贴到您参考:

GMT:2021年4月16日,星期五07:50:03.571
您的时区:2021年4月16日,星期五09:50:03.571 GMT+02:00 DST

是正确的。在UTC/GMT中,此UNIX时间戳代表上午7:50,在我的本地时区是上午9:50。

如果我们采用不正确的天真 dateTime.utcnow() timestamp 1618552203.571235 ,则结果是:

GMT:2021年4月16日,星期五05:50:03.571
您的时区:2021年4月16日,星期五07:50:03.571 GMT+02:00 DST

不正确。那是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:

I do get different responses for datetime.now() vs datetime.utcnow() corresponding to my local time vs UTC. Accordingly, the seconds elapsed has a difference of 19800s (+5:30 for my time zone) for me.

This is a known pitfall with datetime.utcnow and is in fact addressed in the manual:

Warning: Because naive datetime objects are treated by many datetime methods as local times, it is preferred to use aware datetimes to represent times in UTC. As such, the recommended way to create an object representing the current time in UTC is by calling datetime.now(timezone.utc).

To unpack this a bit:

When you call datetime.now(), it returns you a naïve datetime object (meaning it has no tzinfo timezone attached) for your current local time:

>>> datetime.now()
datetime.datetime(2021, 4, 16, 9, 50, 3, 571235)

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 for datetime.now(), because this timestamp was created with my local time anyway:

>>> datetime.now().timestamp()
1618559403.571235

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:

>>> datetime.utcnow()
datetime.datetime(2021, 4, 16, 7, 50, 3, 571235)

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:

>>> datetime.utcnow().timestamp()
1618552203.571235

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:

>>> datetime.now(timezone.utc)
datetime.datetime(2021, 4, 16, 7, 50, 3, 571235, tzinfo=datetime.timezone.utc)
>>> datetime.now(timezone.utc).timestamp()
1618559403.571235

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 for datetime.now().timestamp(). Which is also the same as we'd get for time.time():

>>> from time import time
>>> time()
1618559403.571235

When pasting this into https://www.epochconverter.com, which you reference:

GMT: Friday, 16 April 2021 07:50:03.571
Your time zone: Friday, 16 April 2021 09:50:03.571 GMT+02:00 DST

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() timestamp 1618552203.571235, the result is:

GMT: Friday, 16 April 2021 05:50:03.571
Your time zone: Friday, 16 April 2021 07:50:03.571 GMT+02:00 DST

Which is incorrect. That was 2 hours ago, not now.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文