codekingpro/portable-devtools
114k
1try:2 import pytz3except ModuleNotFoundError:4 pytz = None5 import zoneinfo6 7 8def _get_tzinfo(tzenv: str):9 """Get the tzinfo from `zoneinfo` or `pytz`10 11 :param tzenv: timezone in the form of Continent/City12 :return: tzinfo object or None if not found13 """14 if pytz:15 try:16 return pytz.timezone(tzenv)17 except pytz.UnknownTimeZoneError:18 pass19 else:20 try:21 return zoneinfo.ZoneInfo(tzenv)22 except zoneinfo.ZoneInfoNotFoundError:23 pass24 25 return None26 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 tzinfo36 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)44 