Hide keyboard shortcuts

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 

2 

3import pytz 

4from .utils import get_timezone_label, is_valid_timezone 

5 

6# Exports 

7 

8__all__ = ( 

9 "TimeZone", 

10) 

11 

12# Classes 

13 

14 

15class TimeZone(object): 

16 """Represents a timezone.""" 

17 

18 def __init__(self, code, label=None): 

19 """Initialize a timezone instance. 

20 

21 :param code: The pytz code for the timezone. 

22 :type code: str 

23 

24 :param label: The label for the timezone. 

25 :type label: str 

26 

27 """ 

28 self.code = code 

29 self._label = label or code 

30 

31 def as_pytz(self): 

32 """Get the timezone from pytz. 

33 

34 :rtype: datetime.tzinfo 

35 

36 """ 

37 if not self.is_valid: 

38 return None 

39 

40 return pytz.timezone(self.code) 

41 

42 @property 

43 def is_valid(self): 

44 """Indicates whether the timezone is valid. 

45 

46 :rtype: bool 

47 

48 """ 

49 return is_valid_timezone(self.code) 

50 

51 @property 

52 def label(self): 

53 """Get the timezone label. 

54 

55 :rtype: str 

56 

57 """ 

58 if self.is_valid: 

59 return get_timezone_label(self.code) 

60 

61 return self._label