我现在能想到的最好的就是这个怪物:
>>> 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。
我不只是使用 now() 或 localtime(),因为这将使用服务器的时区,而不是用户的时区。
我不禁觉得我错过了什么,有什么想法吗?
最佳答案
如果你这样做,我认为你可以减少一些方法调用:
>>> from datetime import datetime
>>> datetime.now(pytz.timezone("Australia/Melbourne")) \
.replace(hour=0, minute=0, second=0, microsecond=0) \
.astimezone(pytz.utc)
但是......在您的代码中存在比美学更大的问题:它会在切换到夏令时的当天给出错误的结果。
原因是 datetime 构造函数和 replace()
都没有考虑 DST 更改。
例如:
>>> 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()
的文档指出:
This method should be used to construct localtimes, rather than passing a tzinfo argument to a datetime constructor.
这样,你的问题就这样解决了:
>>> 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 年之前的日期。
关于python - 如何获得给定时区的 UTC 时间 "midnight"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/373370/