diff --git a/lib/tzlocal/unix.py b/lib/tzlocal/unix.py index 4999eb5b..8574965a 100644 --- a/lib/tzlocal/unix.py +++ b/lib/tzlocal/unix.py @@ -1,16 +1,10 @@ import os +import pytz import re -import sys import warnings -from datetime import timezone from tzlocal import utils -if sys.version_info >= (3, 9): - from zoneinfo import ZoneInfo, ZoneInfoNotFoundError -else: - from backports.zoneinfo import ZoneInfo, ZoneInfoNotFoundError - _cache_tz = None @@ -21,17 +15,17 @@ def _tz_from_env(tzenv): # TZ specifies a file if os.path.isabs(tzenv) and os.path.exists(tzenv): with open(tzenv, 'rb') as tzfile: - return ZoneInfo.from_file(tzfile, key='local') + return pytz.tzfile.build_tzinfo('local', tzfile) # TZ specifies a zoneinfo zone. try: - tz = ZoneInfo(tzenv) + tz = pytz.timezone(tzenv) # That worked, so we return this: return tz - except ZoneInfoNotFoundError: - raise ZoneInfoNotFoundError( + except pytz.UnknownTimeZoneError: + raise pytz.UnknownTimeZoneError( "tzlocal() does not support non-zoneinfo timezones like %s. \n" - "Please use a timezone in the form of Continent/City") from None + "Please use a timezone in the form of Continent/City") def _try_tz_from_env(): @@ -39,7 +33,7 @@ def _try_tz_from_env(): if tzenv: try: return _tz_from_env(tzenv) - except ZoneInfoNotFoundError: + except pytz.UnknownTimeZoneError: pass @@ -62,7 +56,7 @@ def _get_localzone(_root='/'): if os.path.exists('/system/bin/getprop'): import subprocess androidtz = subprocess.check_output(['getprop', 'persist.sys.timezone']).strip().decode() - return ZoneInfo(androidtz) + return pytz.timezone(androidtz) # Now look for distribution specific configuration files # that contain the timezone name. @@ -89,15 +83,15 @@ def _get_localzone(_root='/'): etctz, dummy = etctz.split('#', 1) if not etctz: continue - tz = ZoneInfo(etctz.replace(' ', '_')) + tz = pytz.timezone(etctz.replace(' ', '_')) if _root == '/': # We are using a file in etc to name the timezone. # Verify that the timezone specified there is actually used: utils.assert_tz_offset(tz) return tz - except (IOError, UnicodeDecodeError): - # File doesn't exist or is a directory, or it's a binary file. + except IOError: + # File doesn't exist or is a directory continue # CentOS has a ZONE setting in /etc/sysconfig/clock, @@ -127,15 +121,15 @@ def _get_localzone(_root='/'): etctz = line[:end_re.search(line).start()] # We found a timezone - tz = ZoneInfo(etctz.replace(' ', '_')) + tz = pytz.timezone(etctz.replace(' ', '_')) if _root == '/': # We are using a file in etc to name the timezone. # Verify that the timezone specified there is actually used: utils.assert_tz_offset(tz) return tz - except (IOError, UnicodeDecodeError) as e: - # UnicodeDecode handles when clock is symlink to /etc/localtime + except IOError: + # File doesn't exist or is a directory continue # systemd distributions use symlinks that include the zone name, @@ -147,8 +141,8 @@ def _get_localzone(_root='/'): while start != 0: tzpath = tzpath[start:] try: - return ZoneInfo(tzpath) - except ZoneInfoNotFoundError: + return pytz.timezone(tzpath) + except pytz.UnknownTimeZoneError: pass start = tzpath.find("/")+1 @@ -159,10 +153,10 @@ def _get_localzone(_root='/'): if not os.path.exists(tzpath): continue with open(tzpath, 'rb') as tzfile: - return ZoneInfo.from_file(tzfile, key='local') + return pytz.tzfile.build_tzinfo('local', tzfile) warnings.warn('Can not find any timezone configuration, defaulting to UTC.') - return timezone.utc + return pytz.utc def get_localzone(): """Get the computers configured local timezone, if any.""" diff --git a/lib/tzlocal/win32.py b/lib/tzlocal/win32.py index 0472a7c8..fcc42a23 100644 --- a/lib/tzlocal/win32.py +++ b/lib/tzlocal/win32.py @@ -1,18 +1,13 @@ -import sys - try: import _winreg as winreg except ImportError: import winreg +import pytz + from tzlocal.windows_tz import win_tz from tzlocal import utils -if sys.version_info >= (3, 9): - from zoneinfo import ZoneInfo, ZoneInfoNotFoundError -else: - from backports.zoneinfo import ZoneInfo, ZoneInfoNotFoundError - _cache_tz = None @@ -86,7 +81,7 @@ def get_localzone_name(): # Return what we have. if timezone is None: - raise ZoneInfoNotFoundError(tzkeyname) + raise pytz.UnknownTimeZoneError('Can not find timezone ' + tzkeyname) return timezone @@ -95,7 +90,7 @@ def get_localzone(): """Returns the zoneinfo-based tzinfo object that matches the Windows-configured timezone.""" global _cache_tz if _cache_tz is None: - _cache_tz = ZoneInfo(get_localzone_name()) + _cache_tz = pytz.timezone(get_localzone_name()) utils.assert_tz_offset(_cache_tz) return _cache_tz @@ -104,6 +99,6 @@ def get_localzone(): def reload_localzone(): """Reload the cached localzone. You need to call this if the timezone has changed.""" global _cache_tz - _cache_tz = ZoneInfo(get_localzone_name()) + _cache_tz = pytz.timezone(get_localzone_name()) utils.assert_tz_offset(_cache_tz) return _cache_tz diff --git a/lib/tzlocal/windows_tz.py b/lib/tzlocal/windows_tz.py index 0d285037..86ba807d 100644 --- a/lib/tzlocal/windows_tz.py +++ b/lib/tzlocal/windows_tz.py @@ -104,7 +104,6 @@ win_tz = {'AUS Central Standard Time': 'Australia/Darwin', 'Saratov Standard Time': 'Europe/Saratov', 'Singapore Standard Time': 'Asia/Singapore', 'South Africa Standard Time': 'Africa/Johannesburg', - 'South Sudan Standard Time': 'Africa/Juba', 'Sri Lanka Standard Time': 'Asia/Colombo', 'Sudan Standard Time': 'Africa/Khartoum', 'Syria Standard Time': 'Asia/Damascus', @@ -119,7 +118,7 @@ win_tz = {'AUS Central Standard Time': 'Australia/Darwin', 'Turks And Caicos Standard Time': 'America/Grand_Turk', 'US Eastern Standard Time': 'America/Indianapolis', 'US Mountain Standard Time': 'America/Phoenix', - 'UTC': 'Etc/UTC', + 'UTC': 'Etc/GMT', 'UTC+12': 'Etc/GMT-12', 'UTC+13': 'Etc/GMT-13', 'UTC-02': 'Etc/GMT+2', @@ -137,8 +136,7 @@ win_tz = {'AUS Central Standard Time': 'Australia/Darwin', 'West Asia Standard Time': 'Asia/Tashkent', 'West Bank Standard Time': 'Asia/Hebron', 'West Pacific Standard Time': 'Pacific/Port_Moresby', - 'Yakutsk Standard Time': 'Asia/Yakutsk', - 'Yukon Standard Time': 'America/Whitehorse'} + 'Yakutsk Standard Time': 'Asia/Yakutsk'} # Old name for the win_tz variable: tz_names = win_tz @@ -168,7 +166,7 @@ tz_win = {'Africa/Abidjan': 'Greenwich Standard Time', 'Africa/Gaborone': 'South Africa Standard Time', 'Africa/Harare': 'South Africa Standard Time', 'Africa/Johannesburg': 'South Africa Standard Time', - 'Africa/Juba': 'South Sudan Standard Time', + 'Africa/Juba': 'E. Africa Standard Time', 'Africa/Kampala': 'E. Africa Standard Time', 'Africa/Khartoum': 'Sudan Standard Time', 'Africa/Kigali': 'South Africa Standard Time', @@ -236,8 +234,8 @@ tz_win = {'Africa/Abidjan': 'Greenwich Standard Time', 'America/Creston': 'US Mountain Standard Time', 'America/Cuiaba': 'Central Brazilian Standard Time', 'America/Curacao': 'SA Western Standard Time', - 'America/Danmarkshavn': 'Greenwich Standard Time', - 'America/Dawson': 'Yukon Standard Time', + 'America/Danmarkshavn': 'UTC', + 'America/Dawson': 'Pacific Standard Time', 'America/Dawson_Creek': 'US Mountain Standard Time', 'America/Denver': 'Mountain Standard Time', 'America/Detroit': 'Eastern Standard Time', @@ -347,14 +345,14 @@ tz_win = {'Africa/Abidjan': 'Greenwich Standard Time', 'America/Tortola': 'SA Western Standard Time', 'America/Vancouver': 'Pacific Standard Time', 'America/Virgin': 'SA Western Standard Time', - 'America/Whitehorse': 'Yukon Standard Time', + 'America/Whitehorse': 'Pacific Standard Time', 'America/Winnipeg': 'Central Standard Time', 'America/Yakutat': 'Alaskan Standard Time', 'America/Yellowknife': 'Mountain Standard Time', - 'Antarctica/Casey': 'Central Pacific Standard Time', + 'Antarctica/Casey': 'Singapore Standard Time', 'Antarctica/Davis': 'SE Asia Standard Time', 'Antarctica/DumontDUrville': 'West Pacific Standard Time', - 'Antarctica/Macquarie': 'Tasmania Standard Time', + 'Antarctica/Macquarie': 'Central Pacific Standard Time', 'Antarctica/Mawson': 'West Asia Standard Time', 'Antarctica/McMurdo': 'New Zealand Standard Time', 'Antarctica/Palmer': 'SA Eastern Standard Time', @@ -503,7 +501,7 @@ tz_win = {'Africa/Abidjan': 'Greenwich Standard Time', 'Canada/Newfoundland': 'Newfoundland Standard Time', 'Canada/Pacific': 'Pacific Standard Time', 'Canada/Saskatchewan': 'Canada Central Standard Time', - 'Canada/Yukon': 'Yukon Standard Time', + 'Canada/Yukon': 'Pacific Standard Time', 'Chile/Continental': 'Pacific SA Standard Time', 'Chile/EasterIsland': 'Easter Island Standard Time', 'Cuba': 'Cuba Standard Time', diff --git a/requirements.txt b/requirements.txt index 5b80b8fe..a0473ff7 100644 --- a/requirements.txt +++ b/requirements.txt @@ -44,7 +44,7 @@ soupsieve==2.2.1 tempora==4.1.2 tokenize-rt==4.1.0 tzdata==2021.2.post0 -tzlocal==3.0 +tzlocal<3.0 urllib3==1.26.7 webencodings==0.5.1 websocket-client==1.2.1