Source code for superdjango.contrib.i18n.timezones.utils

# Imports

from pytz import BaseTzInfo, UnknownTimeZoneError
from .choices import ALL_TIMEZONE_CHOICES
from .constants import ALL_TIMEZONES

# Exports

__all__ = (
    "get_timezone_label",
    "is_pytz_instance",
    "is_valid_timezone",
)

# Functions


[docs]def get_timezone_label(code): """Get the label for the timezone. :param code: The pytz timezone code. :type code: str :rtype: str :raise: UnknownTimeZoneError """ for value, label in ALL_TIMEZONE_CHOICES: if code == value: return label raise UnknownTimeZoneError("Invalid or unrecognized timezone: %s" % code)
[docs]def is_pytz_instance(value): """Determines whether the given value is a BaseTzInfo instance. :rtype: bool """ return isinstance(value, BaseTzInfo)
[docs]def is_valid_timezone(code): """Determines whether the given code is a valid pytz timezone code. :param code: The pytz timezone code. :type code: str :rtype: bool """ return code in ALL_TIMEZONES