Get Timezone from City in Python/Django

后端 未结 6 1594
独厮守ぢ
独厮守ぢ 2020-11-27 06:00

Using pytz, I am able to get a list of timezones like so:

>>> from pytz import country_timezones
>>> print(\' \'.join(country_         


        
6条回答
  •  北荒
    北荒 (楼主)
    2020-11-27 06:23

    there have been a lot of possible solutions proposed here and they're all a bit tedious to set up.

    To make things quicker for the next person with this problem, I took the one from Will Charlton and made a quick python library out of it: https://pypi.python.org/pypi/whenareyou

    from whenareyou import whenareyou
    tz = whenareyou('Hamburg')
    tz.localize(datetime(2002, 10, 27, 6, 0, 0))
    

    Gets you datetime.datetime(2002, 10, 27, 6, 0, tzinfo=) .

    This gets you a pytz object (tz in the example) so you can use it pythonicly.

    • It uses the google API
    • Leaves daylight savings calculation to pytz, only one call per city, rest happens offline
    • LRU caches the requests so you shouldn't hit the API limit easily
    • Should also work with any address or anything google maps understands

提交回复
热议问题