How do I get the UTC time of quot;midnightquot; for a given timezone?(如何获得“午夜的 UTC 时间?对于给定的时区?)
问题描述
我现在能想到的最好的就是这个怪物:
The best I can come up with for now is this monstrosity:
>>> datetime.utcnow()
... .replace(tzinfo=pytz.UTC)
... .astimezone(pytz.timezone("Australia/Melbourne"))
... .replace(hour=0,minute=0,second=0,microsecond=0)
... .astimezone(pytz.UTC)
... .replace(tzinfo=None)
datetime.datetime(2008, 12, 16, 13, 0)
即,用英语,获取当前时间(UTC),将其转换为其他时区,将时间设置为午夜,然后转换回 UTC.
I.e., in English, get the current time (in UTC), convert it to some other timezone, set the time to midnight, then convert back to UTC.
我不只是使用 now() 或 localtime(),因为这将使用服务器的时区,而不是用户的时区.
I'm not just using now() or localtime() as that would use the server's timezone, not the user's timezone.
我不禁觉得我错过了什么,有什么想法吗?
I can't help feeling I'm missing something, any ideas?
推荐答案
如果你这样做,我认为你可以减少一些方法调用:
I think you can shave off a few method calls if you do it like this:
>>> from datetime import datetime
>>> datetime.now(pytz.timezone("Australia/Melbourne"))
.replace(hour=0, minute=0, second=0, microsecond=0)
.astimezone(pytz.utc)
但是......在您的代码中存在比美学更大的问题:它会在切换到夏令时的当天给出错误的结果.
BUT… there is a bigger problem than aesthetics in your code: it will give the wrong result on the day of the switch to or from Daylight Saving Time.
原因是 datetime 构造函数和 replace()
都没有考虑 DST 更改.
The reason for this is that neither the datetime constructors nor replace()
take DST changes into account.
例如:
>>> now = datetime(2012, 4, 1, 5, 0, 0, 0, tzinfo=pytz.timezone("Australia/Melbourne"))
>>> print now
2012-04-01 05:00:00+10:00
>>> print now.replace(hour=0)
2012-04-01 00:00:00+10:00 # wrong! midnight was at 2012-04-01 00:00:00+11:00
>>> print datetime(2012, 3, 1, 0, 0, 0, 0, tzinfo=tz)
2012-03-01 00:00:00+10:00 # wrong again!
但是,tz.localize()
的文档指出:
这个方法应该用于构建本地时间,而不是而不是将 tzinfo 参数传递给 datetime 构造函数.
This method should be used to construct localtimes, rather than passing a tzinfo argument to a datetime constructor.
这样,你的问题就这样解决了:
Thus, your problem is solved like so:
>>> import pytz
>>> from datetime import datetime, date, time
>>> tz = pytz.timezone("Australia/Melbourne")
>>> the_date = date(2012, 4, 1) # use date.today() here
>>> midnight_without_tzinfo = datetime.combine(the_date, time())
>>> print midnight_without_tzinfo
2012-04-01 00:00:00
>>> midnight_with_tzinfo = tz.localize(midnight_without_tzinfo)
>>> print midnight_with_tzinfo
2012-04-01 00:00:00+11:00
>>> print midnight_with_tzinfo.astimezone(pytz.utc)
2012-03-31 13:00:00+00:00
但不保证 1582 年之前的日期.
No guarantees for dates before 1582, though.
这篇关于如何获得“午夜"的 UTC 时间?对于给定的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何获得“午夜"的 UTC 时间?对于给定的时区?


基础教程推荐
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01