跳转到主要内容

帮助您根据地址或电话号码确定时区。

项目描述

时区鳟鱼

这个库试图解决确定特定地址或电话号码所在时区这一常见问题。它通过使用由pytz、Geonames.org和TimezoneFinder生成的几个映射来做到这一点。

当前版本对于美国、加拿大、澳大利亚以及处于单一时区内的国家来说相当准确。

该库中使用的词汇

  • PST - 时区名称
  • America/Los_Angeles - 时区标识符
  • UTC-07:00 或 -420 - UTC偏移量(后者以分钟为单位)
  • DST - 夏令时

美国zipcode数据由www.geonames.org提供,许可协议为Creative Commons Attribution 3.0。

v1.0.0版本开始,该库需要Python版本3.6或以上。

示例

>>> tztrout.tz_ids_for_phone('+16503334444')
[u'America/Los_Angeles']
>>> tztrout.tz_ids_for_phone('+49 (0)711 400 40990')
[u'Europe/Berlin', u'Europe/Busingen']
>>> tztrout.tz_ids_for_address('US', state='CA')
[u'America/Los_Angeles']
>>> tztrout.tz_ids_for_address('PL')
[u'Europe/Warsaw']
>>> tztrout.tz_ids_for_address('CN')
[
    u'Asia/Shanghai',
    u'Asia/Harbin',
    u'Asia/Chongqing',
    u'Asia/Urumqi',
    u'Asia/Kashgar'
]
>>> import tztrout
>>> tztrout.tz_ids_for_tz_name('PDT')  # ran during DST
[
    u'America/Dawson',
    u'America/Los_Angeles',
    u'America/Santa_Isabel',
    u'America/Tijuana',
    u'America/Vancouver',
    u'America/Whitehorse',
    u'Canada/Pacific',
    u'US/Pacific'
]
>>> tztrout.tz_ids_for_tz_name('PDT')  # ran outside of the DST period
[]
>>> tztrout.local_time_for_phone('+1 (650) 333-4444')
datetime.datetime(2013, 9, 16, 17, 45, 43, 0000000, tzinfo=<DstTzInfo 'America/Los_Angeles' PDT-1 day, 17:00:00 DST>)

>>> tztrout.local_time_for_phone('+48 601 941 311)
datetime.datetime(2013, 9, 17, 2, 45, 43, 0000000, tzinfo=<DstTzInfo 'Europe/Warsaw' CEST+2:00:00 DST>)
>>> tztrout.local_time_for_address('US', state='CA')
datetime.datetime(2013, 9, 16, 17, 45, 43, 0000000, tzinfo=<DstTzInfo 'America/Los_Angeles' PDT-1 day, 17:00:00 DST>)
>>> tztrout.local_time_for_address('PL')
datetime.datetime(2013, 9, 17, 2, 45, 43, 0000000, tzinfo=<DstTzInfo 'Europe/Warsaw' CEST+2:00:00 DST>)
>>> tztrout.tz_ids_for_offset(-7 * 60)  # during DST
[
    u'America/Creston',
    u'America/Dawson',
    u'America/Dawson_Creek',
    u'America/Hermosillo',
    u'America/Los_Angeles',
    u'America/Phoenix',
    u'America/Santa_Isabel',
    u'America/Tijuana',
    u'America/Vancouver',
    u'America/Whitehorse',
    u'Canada/Pacific',
    u'US/Arizona',
    u'US/Pacific'
]
>>> tztrout.tz_ids_for_offset(+2 * 60)  # during DST
[
    "Africa/Blantyre",
    "Africa/Bujumbura",
    "Africa/Cairo",
    "Africa/Ceuta",
    "Africa/Gaborone",
    "Africa/Harare",
    "Africa/Johannesburg",
    "Africa/Kigali",
    "Africa/Lubumbashi",
    "Africa/Lusaka",
    "Africa/Maputo",
    "Africa/Maseru",
    "Africa/Mbabane",
    "Africa/Tripoli",
    "Africa/Windhoek",
    "Arctic/Longyearbyen",
    "Europe/Amsterdam",
    "Europe/Andorra",
    "Europe/Belgrade",
    "Europe/Berlin",
    "Europe/Bratislava",
    "Europe/Brussels",
    "Europe/Budapest",
    "Europe/Busingen",
    "Europe/Copenhagen",
    "Europe/Gibraltar",
    "Europe/Ljubljana",
    "Europe/Luxembourg",
    "Europe/Madrid",
    "Europe/Malta",
    "Europe/Monaco",
    "Europe/Oslo",
    "Europe/Paris",
    "Europe/Podgorica",
    "Europe/Prague",
    "Europe/Rome",
    "Europe/San_Marino",
    "Europe/Sarajevo",
    "Europe/Skopje",
    "Europe/Stockholm",
    "Europe/Tirane",
    "Europe/Vaduz",
    "Europe/Vatican",
    "Europe/Vienna",
    "Europe/Warsaw",
    "Europe/Zagreb",
    "Europe/Zurich"
]

测试

只需运行pytest

重新生成数据

时区、地址和电话号码处于不断变化中,因此该库使用的数据需要定期重新生成。为此,升级pytztimezonefinder依赖项,并运行python regenerate_data.py。如果这不能解决问题,请考虑打开一个问题或向data_exceptions.py中添加异常。

已知问题

  • 澳大利亚中央西部标准时间(CWST)被视为澳大利亚中央标准时间(ACST)。有关更多详情,请参阅澳大利亚异常
  • 洛夫豪斯标准时间(LHST)被视为澳大利亚东部标准时间(AEST)。实际上,它们之间相差30分钟。
  • 加拿大不列颠哥伦比亚省的整个地区都被认为是太平洋时间,尽管其东南部的一小部分地区应被视为山区时间。
  • 加拿大安大略省的整个地区都被认为是东部时间,尽管其西部的一小部分地区应被视为中部时间。
  • 所有+1 867电话号码都被认为是山区时间,尽管这个前缀与加拿大北极远北部的三个地区的电话号码共享,跨越太平洋、山区和中部时间。

发布新版本

  1. 确保代码已在现实的生产环境中彻底审查和测试。
  2. 更新setup.pyCHANGELOG.md。确保您包含任何重大更改。
  3. 运行python setup.py sdisttwine upload dist/<要上传的包名>
  4. 推送一个指向已发布提交的新标签,例如:v0.13

项目详情


下载文件

下载您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源分发

tz-trout-1.0.4.tar.gz (759.6 kB 查看哈希值)

由以下机构支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误日志 StatusPage StatusPage 状态页面