Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/babel/localtime/_helpers.py: 31%
26 statements
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:39 +0000
« prev ^ index » next coverage.py v7.2.7, created at 2023-06-07 06:39 +0000
1try:
2 import pytz
3except ModuleNotFoundError:
4 pytz = None
5 import zoneinfo
8def _get_tzinfo(tzenv: str):
9 """Get the tzinfo from `zoneinfo` or `pytz`
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
25 return None
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
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)