Get Timezone from City in Python/Django

后端 未结 6 1582
独厮守ぢ
独厮守ぢ 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:27

    @robertklep's approach probably works.

    But here's another possible approach using astral - https://pypi.python.org/pypi/astral/0.5

    >>> import datetime
    >>> from astral import Astral
    
    >>> city_name = 'London'  # assuming we retrieve this from user input
    
    >>> a = Astral()
    >>> a.solar_depression = 'civil'
    
    >>> city = a[city_name]   # creates the city object given the city name above
    
    >>> print('Information for %s/%s\n' % (city_name, city.country))
    Information for London/England
    
    >>> timezone = city.timezone
    >>> print('Timezone: %s' % timezone)
    Timezone: Europe/London
    

提交回复
热议问题