如何获取 UTC 的当前时间(现在)?
我有一个 python 日期时间对象(代表从现在开始的五分钟),我想将其转换为 UTC。我计划以 RFC 2822 格式输出它以放入 HTTP 标头,但我不确定这对于这个问题是否重要。我在这个网站上找到了一些关于转换时间对象的信息,这样看起来更简单,但是这次我真的想使用日期时间对象,因为我正在使用时间增量来调整它们:
我尝试了这样的事情:
from datetime import datetime, timedelta
now = datetime.now()
fiveMinutesLater = datetime.now() + timedelta(minutes=5)
fiveMinutesLaterUtc = ???
没有时间或日期时间模块看起来会对我有帮助。看来我可以通过 3 或 4 个函数传递 datetime 对象来做到这一点,但我想知道是否有更简单的方法。
我不想使用第三方模块,但如果这是唯一合理的选择,我可能会这样做。
I have a python datetime object (representing five minutes from now) which I would like to convert to UTC. I am planning to output it in RFC 2822 format to put in an HTTP header, but I am not sure if that matters for this question. I found some information on this site about converting time objects, and it looks simpler that way, but this time I really want to use datetime objects, because I am using timedeltas to adjust them:
I tried something like this:
from datetime import datetime, timedelta
now = datetime.now()
fiveMinutesLater = datetime.now() + timedelta(minutes=5)
fiveMinutesLaterUtc = ???
Nothing in the time or datetime module looks like it would help me. It seems like I may be able to do it by passing the datetime object through 3 or 4 functions, but I am wondering if there is a simpler way.
I would prefer not to use third-party modules, but I may if it is the only reasonable choice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
运行此命令以获取 UTC 格式的原始日期时间(并为其添加五分钟):
如果您更喜欢时区感知的日期时间对象,请在 Python 3.2 或更高版本中运行此命令:
Run this to obtain a naive datetime in UTC (and to add five minutes to it):
If you would prefer a timezone-aware datetime object, run this in Python 3.2 or higher:
首先,您需要通过设置其
tzinfo
成员来确保日期时间是一个时区感知对象:http://docs.python.org/library/datetime.html#datetime.tzinfo
然后您可以使用
.astimezone()
函数进行转换它:http://docs.python.org/library/datetime.html #datetime.datetime.astimezone
First you need to make sure the datetime is a timezone-aware object by setting its
tzinfo
member:http://docs.python.org/library/datetime.html#datetime.tzinfo
You can then use the
.astimezone()
function to convert it:http://docs.python.org/library/datetime.html#datetime.datetime.astimezone
对于那些最终在这里寻找将日期时间对象转换为自 UNIX 纪元以来的 UTC 秒数的方法的人:
For those who ended up here looking for a way to convert a datetime object to UTC seconds since UNIX epoch:
使用以下内容:
假设假定有一个本地日期时间的原始日期时间对象,您可以使用以下方法调用序列将其转换为表示相同 UTC 时间的原始 UTC 日期时间(使用 Python 3.6.3 测试)。
示例:
注意:第一个 astimezone() 并不是真正需要的,因为 Python 文档中的这个注释:
Use the following:
Given a naive datetime object assumed to have a local datetime, you can use the following sequence of method calls to convert it to a naive UTC datetime representing the same UTC time (tested with Python 3.6.3).
Example:
Note: The first astimezone() is not really needed because of this note in the Python docs:
我找到了一种方法来获取当前时间,向其中添加一个 timedelta 对象,将结果转换为 UTC,然后在 RFC 2822 中输出:
这并没有完全回答我的问题,但我将其放在这里,因为它可能会帮助其他人在我的情况下。
编辑:我想补充一点,这个技巧仅在时间增量小于一天时才有效。如果您想要使用任何类型的 timedelta 值,可以使用 timedelta.total_seconds()(对于 Python 2.7 及更高版本),或使用 86400*timedelta.days + timedelta.seconds 。我还没有实际尝试过,所以我不能 100% 确定它是否有效。
I found a way to take the current time, add a timedelta object to it, convert the result to UTC, and output it in RFC 2822:
This did not exactly answer my question, but I am putting it here because it may help someone else in my situation.
EDIT: I would like to add that this trick only works if the timedelta is less than one day. If you want something that works with any sort of timedelta value, you can use
timedelta.total_seconds()
(for Python 2.7 and later), or with86400*timedelta.days + timedelta.seconds
. I haven't actually tried this so I'm not 100% sure if it will work.这是一个适用于 Python 2 和 3 的 stdlib 解决方案(无第 3 方模块)。
要获取 RFC 2822 格式的当前 UTC 时间:
要获取未来 5 分钟后的 UTC 时间:
Here's a stdlib solution (no 3rd-party modules) that works on both Python 2 and 3.
To get the current UTC time in RFC 2822 format:
To get the UTC time 5 minutes into the future:
您可以在 email.Utils 模块中使用 formatdate 方法,如下所示
上面是 RFC 2822 格式的日期时间。默认情况下,它返回 UTC 时间,但如果您需要本地时间,可以调用 formatdate(localtime=True)。
有关更多信息,请检查 http://docs.python.org /library/email.mime.html#module-email.util
You can use the formatdate method in the email.Utils module like follows
The above is date time in RFC 2822 format. By default it returns UTC time but in-case you need local time you can call formatdate(localtime=True).
For more information do check http://docs.python.org/library/email.mime.html#module-email.util
要将日期字符串转换为 UTC:
To convert to UTC from a date string:
解决方案非常棒 Delorean lib:
它允许轻松地在不同时区之间转移日期时间
solution with fantastic Delorean lib:
it allows to shift datetime between different timezones easily