如何获得UTC时间“午夜”对于给定的时区? [英] How do I get the UTC time of "midnight" for a given timezone?

查看:158
本文介绍了如何获得UTC时间“午夜”对于给定的时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在最好的办法就是这个怪物:

 >>> datetime.utcnow()\ 
... .replace(tzinfo = pytz.UTC)\
... .astimezone(pytz.timezone(澳大利亚/墨尔本))\
... .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(),因为这将使用服务器的时区,而不是用户的时区。



我不禁感到我'我想,如果你这样做,我可以去掉几个方法调用:

解决方案

/ p>

 >>> 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))
>>>现在打印
2012-04-01 05:00:00 + 10:00
>>>打印now.replace(小时= 0)
2012-04-01 00:00:00 + 10:00#错!午夜在2012-04-01 00:00:00 + 11:00
>>>打印日期时间(2012,3,1,0,0,0,0,tzinfo = tz)
2012-03-01 00:00:00 + 10:00#再次错误!

但是, tz.localize()的文档状态:


此方法应用于构建本地时间,而不是将tzinfo参数传递给datetime构造函数。


因此,您的问题如此解决:

 >>> import pytz 
>>>> from datetime import datetime,date,time

>>>> tz = pytz.timezone(澳大利亚/墨尔本)
>>> the_date = date(2012,4,1)#use date.today()here

>>>> midnight_without_tzinfo = datetime.combine(the_date,time())
>>>打印midnight_without_tzinfo
2012-04-01 00:00:00

>>> midnight_with_tzinfo = tz.localize(midnight_without_tzinfo)
>>>>打印midnight_with_tzinfo
2012-04-01 00:00:00 + 11:00

>>>打印midnight_with_tzinfo.astimezone(pytz.utc)
2012-03-31 13:00:00 + 00:00

不保证1582之前的日期。


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)

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.

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.

The reason for this is that neither the datetime constructors nor replace() take DST changes into account.

For example:

>>> 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!

However, the documentation for tz.localize() states:

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

No guarantees for dates before 1582, though.

这篇关于如何获得UTC时间“午夜”对于给定的时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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