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

# Imports

import pytz
from .utils import get_timezone_label, is_valid_timezone

# Exports

__all__ = (
    "TimeZone",
)

# Classes


[docs]class TimeZone(object): """Represents a timezone."""
[docs] def __init__(self, code, label=None): """Initialize a timezone instance. :param code: The pytz code for the timezone. :type code: str :param label: The label for the timezone. :type label: str """ self.code = code self._label = label or code
[docs] def as_pytz(self): """Get the timezone from pytz. :rtype: datetime.tzinfo """ if not self.is_valid: return None return pytz.timezone(self.code)
@property def is_valid(self): """Indicates whether the timezone is valid. :rtype: bool """ return is_valid_timezone(self.code) @property def label(self): """Get the timezone label. :rtype: str """ if self.is_valid: return get_timezone_label(self.code) return self._label