From 98296d2f74403b8b46fee27d289def2f60395606 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Hron=C4=8Dok?= Date: Fri, 12 Feb 2021 17:21:18 +0100 Subject: [PATCH] Migrate from pytz to zoneinfo, list (close to) all time zones https://listman.redhat.com/archives/anaconda-devel-list/2021-February/msg00020.html Python's zoneinfo does not have any concept of "common timezones". Hence, we show all zones with slash in them, except some weird Etc ones. This effectively adds about 17% of zones, today, that means: Africa/Asmera Africa/Timbuktu America/Argentina/ComodRivadavia America/Atka America/Buenos_Aires America/Catamarca America/Coral_Harbour America/Cordoba America/Ensenada America/Fort_Wayne America/Godthab America/Indianapolis America/Jujuy America/Knox_IN America/Louisville America/Mendoza America/Montreal America/Porto_Acre America/Rosario America/Santa_Isabel America/Shiprock America/Virgin Antarctica/South_Pole Asia/Ashkhabad Asia/Calcutta Asia/Chongqing Asia/Chungking Asia/Dacca Asia/Harbin Asia/Istanbul Asia/Kashgar Asia/Katmandu Asia/Macao Asia/Rangoon Asia/Saigon Asia/Tel_Aviv Asia/Thimbu Asia/Ujung_Pandang Asia/Ulan_Bator Atlantic/Faeroe Atlantic/Jan_Mayen Australia/ACT Australia/Canberra Australia/Currie Australia/LHI Australia/NSW Australia/North Australia/Queensland Australia/South Australia/Tasmania Australia/Victoria Australia/West Australia/Yancowinna Brazil/Acre Brazil/DeNoronha Brazil/East Brazil/West Canada/Saskatchewan Canada/Yukon Chile/Continental Chile/EasterIsland Europe/Belfast Europe/Nicosia Europe/Tiraspol Mexico/BajaNorte Mexico/BajaSur Mexico/General Pacific/Johnston Pacific/Ponape Pacific/Samoa Pacific/Truk Pacific/Yap US/Aleutian US/East-Indiana US/Indiana-Starke US/Michigan US/Samoa See also https://github.com/rhinstaller/anaconda/pull/3167#issuecomment-778329679 for why is that OK. --- anaconda.spec.in | 1 - pyanaconda/isys/__init__.py | 9 ++++----- pyanaconda/timezone.py | 27 ++++++++++++++++++--------- 3 files changed, 22 insertions(+), 15 deletions(-) diff --git a/anaconda.spec.in b/anaconda.spec.in index cf38803a47a0..0b1e836c1365 100644 --- a/anaconda.spec.in +++ b/anaconda.spec.in @@ -122,7 +122,6 @@ Requires: subscription-manager >= %{subscriptionmanagerver} # which is apparently great for containers but unhelpful for the rest of us Requires: cracklib-dicts -Requires: python3-pytz Requires: teamd %ifarch s390 s390x Requires: openssh diff --git a/pyanaconda/isys/__init__.py b/pyanaconda/isys/__init__.py index e1e9db55bed9..39e498a612d6 100644 --- a/pyanaconda/isys/__init__.py +++ b/pyanaconda/isys/__init__.py @@ -28,7 +28,7 @@ import blivet.arch import time import datetime -import pytz +import zoneinfo from pyanaconda.anaconda_loggers import get_module_logger log = get_module_logger(__name__) @@ -88,7 +88,7 @@ def set_system_date_time(year=None, month=None, day=None, hour=None, minute=None """ - utc = pytz.UTC + utc = zoneinfo.ZoneInfo(key='UTC') # If no timezone is set, use UTC if not tz: tz = utc @@ -104,13 +104,12 @@ def set_system_date_time(year=None, month=None, day=None, hour=None, minute=None minute = minute if minute is not None else now.minute second = second if second is not None else now.second - set_date = tz.localize(datetime.datetime(year, month, day, hour, minute, second)) + set_date = datetime.datetime(year, month, day, hour, minute, second, tzinfo=tz) # Calculate the number of seconds between this time and timestamp 0 - # see pytz docs, search for "Converting between timezones" # pylint bug here: https://github.com/PyCQA/pylint/issues/1104 # pylint: disable=no-value-for-parameter - epoch = utc.localize(datetime.datetime.utcfromtimestamp(0)).astimezone(tz) + epoch = datetime.datetime.fromtimestamp(0, tz=utc).astimezone(tz) timestamp = (set_date - epoch).total_seconds() set_system_time(int(timestamp)) diff --git a/pyanaconda/timezone.py b/pyanaconda/timezone.py index e9aa826b1432..de36d9acd38c 100644 --- a/pyanaconda/timezone.py +++ b/pyanaconda/timezone.py @@ -22,9 +22,10 @@ """ -import pytz import langtable +import zoneinfo from collections import OrderedDict +from functools import cache from pyanaconda.core import util from pyanaconda.core.constants import THREAD_STORAGE @@ -38,8 +39,7 @@ from pyanaconda.anaconda_loggers import get_module_logger log = get_module_logger(__name__) -# The following zones are not in pytz.common_timezones and -# Etc category in pytz.all_timezones includes some more, +# The Etc category in zoneinfo.available_timezones() includes some more, # however confusing ones (like UCT, GMT+0, GMT-0,...) ETC_ZONES = ['GMT+1', 'GMT+2', 'GMT+3', 'GMT+4', 'GMT+5', 'GMT+6', 'GMT+7', 'GMT+8', 'GMT+9', 'GMT+10', 'GMT+11', 'GMT+12', @@ -126,6 +126,18 @@ def get_preferred_timezone(territory): return timezones[0] +@cache +def all_timezones(): + """ + Get all timezones, but with the Etc zones reduced. Cached. + + :rtype: set + + """ + etc_zones = {"Etc/" + zone for zone in ETC_ZONES} + return zoneinfo.available_timezones() | etc_zones + + def get_all_regions_and_timezones(): """ Get a dictionary mapping the regions to the list of their timezones. @@ -136,7 +148,7 @@ def get_all_regions_and_timezones(): result = OrderedDict() - for tz in pytz.common_timezones: + for tz in sorted(all_timezones()): parts = tz.split("/", 1) if len(parts) > 1: @@ -144,7 +156,6 @@ def get_all_regions_and_timezones(): result[parts[0]] = set() result[parts[0]].add(parts[1]) - result["Etc"] = set(ETC_ZONES) return result @@ -157,9 +168,7 @@ def is_valid_timezone(timezone): """ - etc_zones = ["Etc/" + zone for zone in ETC_ZONES] - - return timezone in pytz.common_timezones + etc_zones + return timezone in all_timezones() def get_timezone(timezone): @@ -170,4 +179,4 @@ def get_timezone(timezone): :rtype: datetime.tzinfo """ - return pytz.timezone(timezone) + return zoneinfo.ZoneInfo(timezone)