Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# Imports
3import pytz
4from .utils import get_timezone_label, is_valid_timezone
6# Exports
8__all__ = (
9 "TimeZone",
10)
12# Classes
15class TimeZone(object):
16 """Represents a timezone."""
18 def __init__(self, code, label=None):
19 """Initialize a timezone instance.
21 :param code: The pytz code for the timezone.
22 :type code: str
24 :param label: The label for the timezone.
25 :type label: str
27 """
28 self.code = code
29 self._label = label or code
31 def as_pytz(self):
32 """Get the timezone from pytz.
34 :rtype: datetime.tzinfo
36 """
37 if not self.is_valid:
38 return None
40 return pytz.timezone(self.code)
42 @property
43 def is_valid(self):
44 """Indicates whether the timezone is valid.
46 :rtype: bool
48 """
49 return is_valid_timezone(self.code)
51 @property
52 def label(self):
53 """Get the timezone label.
55 :rtype: str
57 """
58 if self.is_valid:
59 return get_timezone_label(self.code)
61 return self._label