python-timezones

A Python library that provides better selection of common timezones, can output HTML and auto select the best timezone based on user's IP.

View the Project on GitHub Doist/python-timezones

Making Python timezones suck less with timezones

timezones makes timezones more user-friendly for the users, especially because it picks the best timezone automatically based on the user's IP address.

$ sudo pip install timezones

What the library provides:

Requires

Screenshot of the library in action

timezones screenshot

Modules

tz_rendering

HTML helper to render timezones. The output will be a SELECT element.

Will auto guess the user's timezone based on IP. Will auto-select the current selected timezone.

Example usage (returns HTML based on current properties):

from timezones import tz_rendering
html_timezones = tz_rendering.html_render_timezones(select_name=timezone',
                                                    current_selected=current_timezone,
                                                    user_ip=get_current_ip(),
                                                    first_entry=_('Select your timezone'))

zones

Holds a collection of common timezones. Is much smaller and better formated than pytz.common_timezones. It also supports fixed timezones such as GMT +7:00.

Example usage (returns US based timezones):

from timezones import zones
for tz_offset, tz_name, tz_formated in zones.get_timezones(only_us=True):
    print tz_formated

=>

    "(GMT-1000) Hawaii')"
    "(GMT-0900) Alaska')"
    "(GMT-0800) Pacific Time (US & Canada)"
    ...

tz_utils

Includes timezone related utilities.

Example usage (get a fixed offset timezone):

print tz_utils.get_timezone('GMT +10:00')

Example usage (guess timezone by IP, required pygeoip!)::

tz_utils.GEOIP_DATA_LOCATION = '/usr/local/geo_ip/GeoLiteCity.dat'
assert tz_utils.guess_timezone_by_ip('201.246.115.62') == 'Chile/Continental'

Example usage (format timezone by name)::

print tz_utils.format_tz_by_name('Europe/Copenhagen')
=>
("+0100", "Europe/Copenhagen", '(GMT+0100) Copenhagen')

Example usage (is a timezone valid?)::

print tz_utils.is_valid_timezone('Europe/Copenhagen')
=>
True

Copyright

Copyright 2012-2020 by Doist. Licensed under MIT license.