Python 在正确的时区获取当前时间 [英] Python get current time in right timezone

查看:91
本文介绍了Python 在正确的时区获取当前时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我用

导入日期时间打印(日期时间.日期时间.现在().strftime(%X"))

将当前时间显示为字符串.
问题是,我的电脑运行在 Europe/Berlin 时区,+2 到 UTC 的偏移量这里没有考虑.它应该显示 21:22:26 而不是 19:22:26也不同于我在这里找到的其他答案,我不通过调用来存储它

datetime.datetime(2014, 7, 10, 18, 44, 59, 193982, tzinfo=)

但是

datetime.datetime.now()

所以我尝试了(但失败了)以下操作:

<预><代码>>>>从 pytz 导入时区>>>datetime.datetime.now().astimezone(timezone('欧洲/柏林'))ValueError: astimezone() 不能应用于简单的日期时间


答案

无法作为答案发布,因为此问题已标记为关闭

我遇到此问题的服务器不再存在.不管怎样,这里还有一些值得检查的事情:

  • 您的服务器/系统的时区设置是否正确?
    • VM 或 docker 容器可能与主机不同步,这值得检查.
  • 那台计算机上的时间是否正确?更改时区后您没有以 +2 小时结束?

解决方案

获取本地时区的当前时间作为一个简单的日期时间对象:

from datetime import datetimenaive_dt = datetime.now()

如果它没有返回预期的时间,则表示您的计算机配置错误.您应该先修复它(它与 Python 无关).

要获取 UTC 中的当前时间作为一个简单的日期时间对象:

naive_utc_dt = datetime.utcnow()

要在 Python 3.3+ 中将当前时间作为感知日期时间对象获取:

from datetime 导入日期时间、时区utc_dt = datetime.now(timezone.utc) # UTC 时间dt = utc_dt.astimezone() # 本地时间

从 tz 数据库中获取给定时区的当前时间:

导入pytztz = pytz.timezone('欧洲/柏林')berlin_now = datetime.now(tz)

它在 DST 转换期间有效.如果时区在过去具有不同的 UTC 偏移量,则它有效,即,即使时区对应于不同时间的多个 tzinfo 对象,它也有效.

Right now I use

import datetime
print(datetime.datetime.now().strftime("%X"))

to display the current time as a string.
Problem is, my computer is running in Europe/Berlin time zone, and the offset of +2 to UTC is not accounted here. Instead of 19:22:26 it should display 21:22:26 Also different to the other answers I found here, I do not store it by calling

datetime.datetime(2014, 7, 10, 18, 44, 59, 193982, tzinfo=<UTC>)

but

datetime.datetime.now()

so I tried (and failed) the following:

>>> from pytz import timezone
>>> datetime.datetime.now().astimezone(timezone('Europe/Berlin'))
 ValueError: astimezone() cannot be applied to a naive datetime


Edit:

Answer

Can't post as answer, as this question is marked closed

The server I had this issue with doesn't exists any longer. Anyway, here are some other things worth checking:

  • Is the timezone of your server/system set up correctly?
    • VMs or docker containers might be out of sync with the host, that's worth checking.
  • Is the time on that computer correct? You don't ended up with +2 hours after changing the timezone?

解决方案

To get the current time in the local timezone as a naive datetime object:

from datetime import datetime
naive_dt = datetime.now()

If it doesn't return the expected time then it means that your computer is misconfigured. You should fix it first (it is unrelated to Python).

To get the current time in UTC as a naive datetime object:

naive_utc_dt = datetime.utcnow()

To get the current time as an aware datetime object in Python 3.3+:

from datetime import datetime, timezone

utc_dt = datetime.now(timezone.utc) # UTC time
dt = utc_dt.astimezone() # local time

To get the current time in the given time zone from the tz database:

import pytz

tz = pytz.timezone('Europe/Berlin')
berlin_now = datetime.now(tz)

It works during DST transitions. It works if the timezone had different UTC offset in the past i.e., it works even if the timezone corresponds to multiple tzinfo objects at different times.

这篇关于Python 在正确的时区获取当前时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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