这是一个使用 Python 库 Pytz 的解决方案,它解决了夏令时结束时时间不明确的问题。
from pytz import timezone
import pandas as pd
def tz_diff(date, tz1, tz2):
'''
Returns the difference in hours between timezone1 and timezone2
for a given date.
'''
date = pd.to_datetime(date)
return (tz1.localize(date) -
tz2.localize(date).astimezone(tz1))\
.seconds/3600
Run Code Online (Sandbox Code Playgroud)
下面的示例分别计算 UTC 和澳大利亚时间在 1 月 1 日和 6 月 1 日之间的时差。请注意如何考虑夏令时。
utc = timezone('UTC')
aus = timezone('Australia/Sydney')
tz_diff('2017-01-01', utc, aus)
# 11.0
tz_diff('2017-06-01', utc, aus)
# 10.0
Run Code Online (Sandbox Code Playgroud)
谢谢
您必须要知道的第一件事是两个时区之间的偏移不仅取决于所讨论的时区,而且取决于您询问的日期.例如,2007年美国夏令时开始和结束的日期发生了变化.虽然基本时区物流在任何一个地点都很少发生变化,但全球变化的速度是不可忽视的.因此,您必须将相关日期合并到您的函数中.
完成必要的前言后,如果您利用钟摆库,实际功能并不难写.它应该看起来像这样:
import pendulum
def tz_diff(home, away, on=None):
"""
Return the difference in hours between the away time zone and home.
`home` and `away` may be any values which pendulum parses as timezones.
However, recommended use is to specify the full formal name.
See https://gist.github.com/pamelafox/986163
As not all time zones are separated by an integer number of hours, this
function returns a float.
As time zones are political entities, their definitions can change over time.
This is complicated by the fact that daylight savings time does not start
and end on the same days uniformly across the globe. This means that there are
certain days of the year when the returned value between `Europe/Berlin` and
`America/New_York` is _not_ `6.0`.
By default, this function always assumes that you want the current
definition. If you prefer to specify, set `on` to the date of your choice.
It should be a `Pendulum` object.
This function returns the number of hours which must be added to the home time
in order to get the away time. For example,
```python
>>> tz_diff('Europe/Berlin', 'America/New_York')
-6.0
>>> tz_diff('Europe/Berlin', 'Asia/Kabul')
2.5
```
"""
if on is None:
on = pendulum.today()
diff = (on.timezone_(home) - on.timezone_(away)).total_hours()
# what about the diff from Tokyo to Honolulu? Right now the result is -19.0
# it should be 5.0; Honolulu is naturally east of Tokyo, just not so around
# the date line
if abs(diff) > 12.0:
if diff < 0.0:
diff += 24.0
else:
diff -= 24.0
return diff
Run Code Online (Sandbox Code Playgroud)
如文档中所述,当您横扫一年中的任何一天时,您可能无法在任何两个给定位置之间获得稳定的结果.然而,实施一种在当年的日子里选择中位数结果的变体是留给读者的练习.
这是另一个解决方案:
from datetime import datetime
from pytz import timezone
from dateutil.relativedelta import relativedelta
utcnow = timezone('utc').localize(datetime.utcnow()) # generic time
here = utcnow.astimezone(timezone('US/Eastern')).replace(tzinfo=None)
there = utcnow.astimezone(timezone('Asia/Ho_Chi_Minh')).replace(tzinfo=None)
offset = relativedelta(here, there)
offset.hours
Run Code Online (Sandbox Code Playgroud)
这里我们要做的是将时间转换为两个不同的时区。然后,我们删除时区信息,以便当您使用relativedelta计算两者之间的差异时,我们会欺骗它认为这是两个不同的时刻,而不是不同时区的同一时刻。
上述结果将返回 -11,但是由于美国/东部地区遵守 DST 而亚洲/胡志明市不遵守,因此该金额全年可能会发生变化。
| 归档时间: |
|
| 查看次数: |
2270 次 |
| 最近记录: |