From d2d4a44705511cfcc461a7f98228d96346429cdf Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Sat, 13 Jul 2024 23:22:45 +0200 Subject: [PATCH] switch to zoneinfo from pytz --- src/backend/InvenTree/InvenTree/helpers.py | 10 +++++----- src/backend/InvenTree/InvenTree/settings.py | 6 +++--- src/backend/InvenTree/InvenTree/tests.py | 4 ++-- src/backend/InvenTree/report/tests.py | 4 ++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/backend/InvenTree/InvenTree/helpers.py b/src/backend/InvenTree/InvenTree/helpers.py index ae72f59578..002c5b9149 100644 --- a/src/backend/InvenTree/InvenTree/helpers.py +++ b/src/backend/InvenTree/InvenTree/helpers.py @@ -21,11 +21,11 @@ from django.core.files.storage import Storage, default_storage from django.http import StreamingHttpResponse from django.utils.translation import gettext_lazy as _ -import pytz import regex from bleach import clean from djmoney.money import Money from PIL import Image +from zoneinfo import ZoneInfo, ZoneInfoNotFoundError import InvenTree.version from common.currency import currency_code_default @@ -926,15 +926,15 @@ def to_local_time(time, target_tz: str = None): if not source_tz: # Default to UTC if not provided - source_tz = pytz.utc + source_tz = ZoneInfo('UTC') if not target_tz: target_tz = server_timezone() try: - target_tz = pytz.timezone(str(target_tz)) - except pytz.UnknownTimeZoneError: - target_tz = pytz.utc + target_tz = ZoneInfo(target_tz) + except ZoneInfoNotFoundError: + target_tz = ZoneInfo('UTC') target_time = time.replace(tzinfo=source_tz).astimezone(target_tz) diff --git a/src/backend/InvenTree/InvenTree/settings.py b/src/backend/InvenTree/InvenTree/settings.py index cedeb4e215..75016baf57 100644 --- a/src/backend/InvenTree/InvenTree/settings.py +++ b/src/backend/InvenTree/InvenTree/settings.py @@ -20,8 +20,8 @@ from django.core.validators import URLValidator from django.http import Http404 from django.utils.translation import gettext_lazy as _ -import pytz from dotenv import load_dotenv +from zoneinfo import ZoneInfo, ZoneInfoNotFoundError from InvenTree.cache import get_cache_config, is_global_cache_enabled from InvenTree.config import get_boolean_setting, get_custom_file, get_setting @@ -942,8 +942,8 @@ TIME_ZONE = get_setting('INVENTREE_TIMEZONE', 'timezone', 'UTC') # Check that the timezone is valid try: - pytz.timezone(TIME_ZONE) -except pytz.exceptions.UnknownTimeZoneError: # pragma: no cover + ZoneInfo(TIME_ZONE) +except ZoneInfoNotFoundError: # pragma: no cover raise ValueError(f"Specified timezone '{TIME_ZONE}' is not valid") USE_I18N = True diff --git a/src/backend/InvenTree/InvenTree/tests.py b/src/backend/InvenTree/InvenTree/tests.py index 165c981edd..c5a822e0e9 100644 --- a/src/backend/InvenTree/InvenTree/tests.py +++ b/src/backend/InvenTree/InvenTree/tests.py @@ -17,12 +17,12 @@ from django.urls import reverse from django.utils import timezone import pint.errors -import pytz from djmoney.contrib.exchange.exceptions import MissingRate from djmoney.contrib.exchange.models import Rate, convert_money from djmoney.money import Money from maintenance_mode.core import get_maintenance_mode, set_maintenance_mode from sesame.utils import get_user +from zoneinfo import ZoneInfo import InvenTree.conversion import InvenTree.format @@ -739,7 +739,7 @@ class TestTimeFormat(TestCase): hour=0, minute=0, second=0, - tzinfo=pytz.timezone('Europe/London'), + tzinfo=ZoneInfo('Europe/London'), ) tests = [ diff --git a/src/backend/InvenTree/report/tests.py b/src/backend/InvenTree/report/tests.py index b5ac753fd9..11999ffc29 100644 --- a/src/backend/InvenTree/report/tests.py +++ b/src/backend/InvenTree/report/tests.py @@ -10,8 +10,8 @@ from django.urls import reverse from django.utils import timezone from django.utils.safestring import SafeString -import pytz from PIL import Image +from zoneinfo import ZoneInfo import report.models as report_models from build.models import Build @@ -169,7 +169,7 @@ class ReportTagTest(TestCase): hour=12, minute=30, second=0, - tzinfo=pytz.timezone('Australia/Sydney'), + tzinfo=ZoneInfo('Australia/Sydney'), ) # Format a set of tests: timezone, format, expected