Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pendulum/tz/__init__.py: 62%

32 statements  

« prev     ^ index     » next       coverage.py v7.0.1, created at 2022-12-25 06:11 +0000

1from typing import Tuple 

2from typing import Union 

3 

4import pytzdata 

5 

6from .local_timezone import get_local_timezone 

7from .local_timezone import set_local_timezone 

8from .local_timezone import test_local_timezone 

9from .timezone import UTC 

10from .timezone import FixedTimezone as _FixedTimezone 

11from .timezone import Timezone as _Timezone 

12 

13 

14PRE_TRANSITION = "pre" 

15POST_TRANSITION = "post" 

16TRANSITION_ERROR = "error" 

17 

18timezones = pytzdata.timezones # type: Tuple[str, ...] 

19 

20 

21_tz_cache = {} 

22 

23 

24def timezone(name, extended=True): # type: (Union[str, int], bool) -> _Timezone 

25 """ 

26 Return a Timezone instance given its name. 

27 """ 

28 if isinstance(name, int): 

29 return fixed_timezone(name) 

30 

31 if name.lower() == "utc": 

32 return UTC 

33 

34 if name in _tz_cache: 

35 return _tz_cache[name] 

36 

37 tz = _Timezone(name, extended=extended) 

38 _tz_cache[name] = tz 

39 

40 return tz 

41 

42 

43def fixed_timezone(offset): # type: (int) -> _FixedTimezone 

44 """ 

45 Return a Timezone instance given its offset in seconds. 

46 """ 

47 if offset in _tz_cache: 

48 return _tz_cache[offset] # type: ignore 

49 

50 tz = _FixedTimezone(offset) 

51 _tz_cache[offset] = tz 

52 

53 return tz 

54 

55 

56def local_timezone(): # type: () -> _Timezone 

57 """ 

58 Return the local timezone. 

59 """ 

60 return get_local_timezone()