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 

3from pytz import BaseTzInfo, UnknownTimeZoneError 

4from .choices import ALL_TIMEZONE_CHOICES 

5from .constants import ALL_TIMEZONES 

6 

7# Exports 

8 

9__all__ = ( 

10 "get_timezone_label", 

11 "is_pytz_instance", 

12 "is_valid_timezone", 

13) 

14 

15# Functions 

16 

17 

18def get_timezone_label(code): 

19 """Get the label for the timezone. 

20 

21 :param code: The pytz timezone code. 

22 :type code: str 

23 

24 :rtype: str 

25 :raise: UnknownTimeZoneError 

26 

27 """ 

28 for value, label in ALL_TIMEZONE_CHOICES: 

29 if code == value: 

30 return label 

31 

32 raise UnknownTimeZoneError("Invalid or unrecognized timezone: %s" % code) 

33 

34 

35def is_pytz_instance(value): 

36 """Determines whether the given value is a BaseTzInfo instance. 

37 

38 :rtype: bool 

39 

40 """ 

41 return isinstance(value, BaseTzInfo) 

42 

43 

44def is_valid_timezone(code): 

45 """Determines whether the given code is a valid pytz timezone code. 

46 

47 :param code: The pytz timezone code. 

48 :type code: str 

49 

50 :rtype: bool 

51 

52 """ 

53 return code in ALL_TIMEZONES