Skip to content

Commit 9752fab

Browse files
test: fix timezone_not_near_midnight not to use pytz (ietf-tools#4678)
1 parent b6b2902 commit 9752fab

2 files changed

Lines changed: 13 additions & 10 deletions

File tree

ietf/utils/tests.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -491,13 +491,13 @@ class TimezoneTests(TestCase):
491491
def test_timezone_not_near_midnight(self, mock):
492492
# give it several choices that should be rejected and one that should be accepted
493493
with patch(
494-
'ietf.utils.timezone.pytz.common_timezones',
495-
[
494+
'ietf.utils.timezone.available_timezones',
495+
return_value=set([
496496
'America/Chicago', # time is 23:15, should be rejected
497497
'America/Lima', # time is 23:15, should be rejected
498498
'America/New_York', # time is 00:15, should be rejected
499499
'Europe/Riga', # time is 07:15, acceptable
500-
],
500+
]),
501501
):
502502
# check a few times (will pass by chance < 0.1% of the time)
503503
self.assertEqual(timezone_not_near_midnight(), 'Europe/Riga')
@@ -508,12 +508,12 @@ def test_timezone_not_near_midnight(self, mock):
508508

509509
# now give it no valid choice
510510
with patch(
511-
'ietf.utils.timezone.pytz.common_timezones',
512-
[
511+
'ietf.utils.timezone.available_timezones',
512+
return_value=set([
513513
'America/Chicago', # time is 23:15, should be rejected
514514
'America/Lima', # time is 23:15, should be rejected
515515
'America/New_York', # time is 00:15, should be rejected
516-
],
516+
]),
517517
):
518518
with self.assertRaises(RuntimeError):
519519
timezone_not_near_midnight()

ietf/utils/timezone.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import datetime
22
import random
3+
import zoneinfo
34

45
from typing import Union
5-
from zoneinfo import ZoneInfo
6+
from zoneinfo import available_timezones, ZoneInfo
67

78
from django.conf import settings
89
from django.utils import timezone
@@ -89,15 +90,17 @@ def timezone_not_near_midnight():
8990
Avoids midnight +/- 1 hour. Raises RuntimeError if it is unable to find
9091
a time zone satisfying this constraint.
9192
"""
92-
timezone_options = pytz.common_timezones
93+
timezone_options = list(
94+
available_timezones().difference(['Factory', 'localtime']) # these two are not known to pytz
95+
)
9396
tzname = random.choice(timezone_options)
94-
right_now = timezone.now().astimezone(pytz.timezone(tzname))
97+
right_now = timezone.now().astimezone(ZoneInfo(tzname))
9598
# Avoid the remote possibility of an infinite loop (might come up
9699
# if there is a problem with the time zone library)
97100
tries_left = 20
98101
while right_now.hour < 1 or right_now.hour >= 23:
99102
tzname = random.choice(timezone_options)
100-
right_now = right_now.astimezone(pytz.timezone(tzname))
103+
right_now = right_now.astimezone(ZoneInfo(tzname))
101104
tries_left -= 1
102105
if tries_left <= 0:
103106
raise RuntimeError('Unable to find a time zone not near midnight')

0 commit comments

Comments
 (0)