From d16a17247683a724884663ce15a0774886891a38 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 https://listman.redhat.com/archives/anaconda-devel-list/2021-February/msg00020.html --- anaconda.spec.in | 1 - pyanaconda/isys/__init__.py | 9 ++++----- pyanaconda/timezone.py | 25 ++++++++++++++++++------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/anaconda.spec.in b/anaconda.spec.in index 2e9fdac68a96..cb156271d73c 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..2c4281321cca 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 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,17 @@ def get_preferred_timezone(territory): return timezones[0] +@cache +def available_timezones(): + """ + Get all timezones, but cached. + + :rtype: set + + """ + return zoneinfo.available_timezones() + + def get_all_regions_and_timezones(): """ Get a dictionary mapping the regions to the list of their timezones. @@ -136,7 +147,7 @@ def get_all_regions_and_timezones(): result = OrderedDict() - for tz in pytz.common_timezones: + for tz in sorted(available_timezones()): parts = tz.split("/", 1) if len(parts) > 1: @@ -157,9 +168,9 @@ def is_valid_timezone(timezone): """ - etc_zones = ["Etc/" + zone for zone in ETC_ZONES] + etc_zones = {"Etc/" + zone for zone in ETC_ZONES} - return timezone in pytz.common_timezones + etc_zones + return timezone in available_timezones() + etc_zones def get_timezone(timezone): @@ -170,4 +181,4 @@ def get_timezone(timezone): :rtype: datetime.tzinfo """ - return pytz.timezone(timezone) + return zoneinfo.ZoneInfo(timezone)