Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/babel/localtime/_helpers.py: 31%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

26 statements  

1try: 

2 import pytz 

3except ModuleNotFoundError: 

4 pytz = None 

5 import zoneinfo 

6 

7 

8def _get_tzinfo(tzenv: str): 

9 """Get the tzinfo from `zoneinfo` or `pytz` 

10 

11 :param tzenv: timezone in the form of Continent/City 

12 :return: tzinfo object or None if not found 

13 """ 

14 if pytz: 

15 try: 

16 return pytz.timezone(tzenv) 

17 except pytz.UnknownTimeZoneError: 

18 pass 

19 else: 

20 try: 

21 return zoneinfo.ZoneInfo(tzenv) 

22 except zoneinfo.ZoneInfoNotFoundError: 

23 pass 

24 

25 return None 

26 

27 

28def _get_tzinfo_or_raise(tzenv: str): 

29 tzinfo = _get_tzinfo(tzenv) 

30 if tzinfo is None: 

31 raise LookupError( 

32 f"Can not find timezone {tzenv}. \n" 

33 "Timezone names are generally in the form `Continent/City`.", 

34 ) 

35 return tzinfo 

36 

37 

38def _get_tzinfo_from_file(tzfilename: str): 

39 with open(tzfilename, 'rb') as tzfile: 

40 if pytz: 

41 return pytz.tzfile.build_tzinfo('local', tzfile) 

42 else: 

43 return zoneinfo.ZoneInfo.from_file(tzfile)