pytz:return 奥尔森时区名称仅来自 GMT 偏移量
pytz: return Olson Timezone name from only a GMT Offset
我有一个遗留应用程序需要用它来补充一些数据。目前,我们有一个数据库 table 存储美国(及其领土)的邮政编码,以及一个 GMT 偏移量,以及一个显示该邮政编码是否使用夏令时的标志。这是从一些免费提供商那里下载的,我现在找不到来源。
我现在需要用每个邮政编码的完整 Olson 名称(例如 America/New York
)补充此 table,因为这似乎是转换给定 date/time 的唯一好方法存储在数据库中作为该购买者本地的 UTC aware datetime
对象。
下面是table:
zip state city lat lon gmt dst
00605 PR AGUADILLA 18.4372 -67.1593 -4 f
02830 RI HARRISVILLE 41.9782 -71.7679 -5 t
99503 AK ANCHORAGE 61.1895 -149.874 -9 t
在另一个相关的 table Purchases
中,我有一个 postres
timestamp without tz
列,它当前包含类似 2014-05-27T15:54:26
的内容,它表示本地某个时间购买是在该邮政编码进行的。 (忽略将这些本地化时间戳保存到数据库时剥离时区信息的愚蠢行为)
最大的问题是:
如何从 timestamp
字符串为 zipcode
table 中的每个邮政编码创建规范化 UTC time
?这将假设时间戳作为 zipcode
table.
中每个示例行的本地时间戳写入数据库
例如,手动查找示例 table 中每个项目的 Olson 时区名称,我得出以下结果:
>>> timestring = '2014-05-27T15:54:26'
>>> dt_naive = datetime.strptime(timestring, '%Y-%m-%dT%H:%M:%S')
>>> # First example - Puerto Rico (no DST since 1945)
>>> print pytz.utc.normalize(pytz.timezone('America/Puerto_Rico').localize(dt_naive))
2014-05-27 19:54:26+00:00
# Second example - Road Island (At that timestamp, UTC Offset was same as PR because of DST)
>>> print pytz.utc.normalize(pytz.timezone('US/Eastern').localize(dt_naive))
>>> 2014-05-27 19:54:26+00:00
# Third Example - Anchorage, AK (AKDT at timestamp)
>>> print pytz.utc.normalize(pytz.timezone('America/Anchorage').localize(dt_naive))
2014-05-27 23:54:26+00:00
我见过几个销售邮政编码数据库的商业产品,它可以给我一个邮政编码 -> 时区查找。但是,对于给定的时区,他们似乎只给我 "EST" 。所以,我想我可以将美国时区(包括领土)的可能时区列表映射到每个时区的奥尔森名称。这可能看起来像这样:
zipcode_olson_lookup = {
('PR', 'f', 'AST'): 'America/Puerto_Rico',
('AK', 'f', 'AKDT',): 'America/Anchorage',
('AK', 't', 'AKT',): 'America/Anchorage',
...
}
非常欢迎任何建议!
UTC本身的偏移量可能不明确(它可能对应几个时区,在某个时间段可能有不同的规则):
#!/usr/bin/env python
from datetime import datetime, timedelta
import pytz # $ pip install pytz
input_utc_offset = timedelta(hours=-4)
timezone_ids = set()
now = datetime.now(pytz.utc) #XXX: use date that corresponds to input_utc_offset instead!
for tz in map(pytz.timezone, pytz.all_timezones_set):
dt = now.astimezone(tz)
tzinfos = getattr(tz, '_tzinfos',
[(dt.tzname(), dt.dst(), dt.utcoffset())])
if any(utc_offset == input_utc_offset for utc_offset, _, _ in tzinfos):
# match timezones that have/had/will have the same utc offset
timezone_ids.add(tz.zone)
print(timezone_ids)
输出
{'America/Anguilla',
'America/Antigua',
'America/Argentina/Buenos_Aires',
...,
'Cuba',
'EST5EDT',
'Jamaica',
'US/East-Indiana',
'US/Eastern',
'US/Michigan'}
您甚至不能使用 pytz.country_timezones['us']
限制列表,因为它会排除您的示例之一:'America/Puerto_Rico'
.
如果知道坐标(经纬度);您可以从 the shape file: you could use a local database or a web-service:
获取时区 ID
#!/usr/bin/env python
from geopy import geocoders # pip install "geopy[timezone]"
g = geocoders.GoogleV3()
for coords in [(18.4372, -67.159), (41.9782, -71.7679), (61.1895, -149.874)]:
print(g.timezone(coords).zone)
输出
America/Puerto_Rico
America/New_York
America/Anchorage
注意:某些本地时间可能不明确,例如,当时间在 DST 转换结束期间倒退时。在这种情况下,您可以将 is_dst=None
传递给 .localize()
方法以引发异常。
the tz database 的不同版本在某些日期的某些时区可能具有不同的 utc 偏移量,即,仅存储 UTC 时间和时区 ID 是不够的(使用哪个版本取决于您的应用程序)。
我有一个遗留应用程序需要用它来补充一些数据。目前,我们有一个数据库 table 存储美国(及其领土)的邮政编码,以及一个 GMT 偏移量,以及一个显示该邮政编码是否使用夏令时的标志。这是从一些免费提供商那里下载的,我现在找不到来源。
我现在需要用每个邮政编码的完整 Olson 名称(例如 America/New York
)补充此 table,因为这似乎是转换给定 date/time 的唯一好方法存储在数据库中作为该购买者本地的 UTC aware datetime
对象。
下面是table:
zip state city lat lon gmt dst
00605 PR AGUADILLA 18.4372 -67.1593 -4 f
02830 RI HARRISVILLE 41.9782 -71.7679 -5 t
99503 AK ANCHORAGE 61.1895 -149.874 -9 t
在另一个相关的 table Purchases
中,我有一个 postres
timestamp without tz
列,它当前包含类似 2014-05-27T15:54:26
的内容,它表示本地某个时间购买是在该邮政编码进行的。 (忽略将这些本地化时间戳保存到数据库时剥离时区信息的愚蠢行为)
最大的问题是:
如何从 timestamp
字符串为 zipcode
table 中的每个邮政编码创建规范化 UTC time
?这将假设时间戳作为 zipcode
table.
例如,手动查找示例 table 中每个项目的 Olson 时区名称,我得出以下结果:
>>> timestring = '2014-05-27T15:54:26'
>>> dt_naive = datetime.strptime(timestring, '%Y-%m-%dT%H:%M:%S')
>>> # First example - Puerto Rico (no DST since 1945)
>>> print pytz.utc.normalize(pytz.timezone('America/Puerto_Rico').localize(dt_naive))
2014-05-27 19:54:26+00:00
# Second example - Road Island (At that timestamp, UTC Offset was same as PR because of DST)
>>> print pytz.utc.normalize(pytz.timezone('US/Eastern').localize(dt_naive))
>>> 2014-05-27 19:54:26+00:00
# Third Example - Anchorage, AK (AKDT at timestamp)
>>> print pytz.utc.normalize(pytz.timezone('America/Anchorage').localize(dt_naive))
2014-05-27 23:54:26+00:00
我见过几个销售邮政编码数据库的商业产品,它可以给我一个邮政编码 -> 时区查找。但是,对于给定的时区,他们似乎只给我 "EST" 。所以,我想我可以将美国时区(包括领土)的可能时区列表映射到每个时区的奥尔森名称。这可能看起来像这样:
zipcode_olson_lookup = {
('PR', 'f', 'AST'): 'America/Puerto_Rico',
('AK', 'f', 'AKDT',): 'America/Anchorage',
('AK', 't', 'AKT',): 'America/Anchorage',
...
}
非常欢迎任何建议!
UTC本身的偏移量可能不明确(它可能对应几个时区,在某个时间段可能有不同的规则):
#!/usr/bin/env python
from datetime import datetime, timedelta
import pytz # $ pip install pytz
input_utc_offset = timedelta(hours=-4)
timezone_ids = set()
now = datetime.now(pytz.utc) #XXX: use date that corresponds to input_utc_offset instead!
for tz in map(pytz.timezone, pytz.all_timezones_set):
dt = now.astimezone(tz)
tzinfos = getattr(tz, '_tzinfos',
[(dt.tzname(), dt.dst(), dt.utcoffset())])
if any(utc_offset == input_utc_offset for utc_offset, _, _ in tzinfos):
# match timezones that have/had/will have the same utc offset
timezone_ids.add(tz.zone)
print(timezone_ids)
输出
{'America/Anguilla',
'America/Antigua',
'America/Argentina/Buenos_Aires',
...,
'Cuba',
'EST5EDT',
'Jamaica',
'US/East-Indiana',
'US/Eastern',
'US/Michigan'}
您甚至不能使用 pytz.country_timezones['us']
限制列表,因为它会排除您的示例之一:'America/Puerto_Rico'
.
如果知道坐标(经纬度);您可以从 the shape file: you could use a local database or a web-service:
获取时区 ID#!/usr/bin/env python
from geopy import geocoders # pip install "geopy[timezone]"
g = geocoders.GoogleV3()
for coords in [(18.4372, -67.159), (41.9782, -71.7679), (61.1895, -149.874)]:
print(g.timezone(coords).zone)
输出
America/Puerto_Rico
America/New_York
America/Anchorage
注意:某些本地时间可能不明确,例如,当时间在 DST 转换结束期间倒退时。在这种情况下,您可以将 is_dst=None
传递给 .localize()
方法以引发异常。
the tz database 的不同版本在某些日期的某些时区可能具有不同的 utc 偏移量,即,仅存储 UTC 时间和时区 ID 是不够的(使用哪个版本取决于您的应用程序)。