Skip to content

Commit

Permalink
Migrate from pytz to zoneinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
hroncok committed Feb 12, 2021
1 parent 982a45f commit d16a172
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
1 change: 0 additions & 1 deletion anaconda.spec.in
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions pyanaconda/isys/__init__.py
Expand Up @@ -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__)
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down
25 changes: 18 additions & 7 deletions pyanaconda/timezone.py
Expand Up @@ -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
Expand All @@ -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',
Expand Down Expand Up @@ -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.
Expand All @@ -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:
Expand All @@ -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):
Expand All @@ -170,4 +181,4 @@ def get_timezone(timezone):
:rtype: datetime.tzinfo
"""

return pytz.timezone(timezone)
return zoneinfo.ZoneInfo(timezone)

0 comments on commit d16a172

Please sign in to comment.