Python datetime.utcnow() 返回不正确的日期时间 [英] Python datetime.utcnow() returning incorrect datetime

查看:69
本文介绍了Python datetime.utcnow() 返回不正确的日期时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

datetime.utcnow()

此调用返回了错误的日期时间,从 UTC/GMT 延迟了 1 小时(签入:http://www.worldtimeserver.com/current_time_in_UTC.asp).

This call is returning an incorrect datetime, delayed from UTC/GMT by 1 hour (check in: http://www.worldtimeserver.com/current_time_in_UTC.asp).

它是否像应有的那样工作?

Is it working like it should be?

例如,它现在正在返回:

For example, it's returning, right now:

2015-02-17 23:58:44.761000.

当前 UTC 时间是:00:58,而不是 23:58

Current UTC time is: 00:58, not 23:58

推荐答案

我知道我迟到了五年,但今晚我遇到了同样的问题.根据我的经验,问题的解决方案是使用有意识的 UTC 日期时间:

I know I'm five years late, but I had the same problem tonight. In my experience, the solution to the problem was to use the aware UTC datetime:

utc_dt_aware = datetime.datetime.now(datetime.timezone.utc)

如果你用谷歌搜索utcnow() 错误",这是你得到的第一个结果,所以我认为无论如何回答都会很好.

If you google "utcnow() wrong" this is the first result you get, so I thought it would be good to answer anyway.

这篇关于Python datetime.utcnow() 返回不正确的日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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