2
0
mirror of https://github.com/inventree/InvenTree.git synced 2025-05-16 03:53:07 +00:00

Switch to zoneinfo from pytz (#7645)

* switch to zoneinfo from pytz

* fix lookup

* fix assert

* fix another round of assertions
This commit is contained in:
Matthias Mair 2024-12-22 22:46:31 +01:00 committed by GitHub
parent 0bcad6b340
commit aa905166c5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 19 additions and 20 deletions

View File

@ -22,9 +22,10 @@ from django.utils import timezone
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
import bleach import bleach
import pytz
from bleach import clean from bleach import clean
from djmoney.money import Money from djmoney.money import Money
from PIL import Image
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
from common.currency import currency_code_default from common.currency import currency_code_default
@ -138,8 +139,6 @@ def getStaticUrl(filename):
def TestIfImage(img): def TestIfImage(img):
"""Test if an image file is indeed an image.""" """Test if an image file is indeed an image."""
from PIL import Image
try: try:
Image.open(img).verify() Image.open(img).verify()
return True return True
@ -962,15 +961,15 @@ def to_local_time(time, target_tz: Optional[str] = None):
if not source_tz: if not source_tz:
# Default to UTC if not provided # Default to UTC if not provided
source_tz = pytz.utc source_tz = ZoneInfo('UTC')
if not target_tz: if not target_tz:
target_tz = server_timezone() target_tz = server_timezone()
try: try:
target_tz = pytz.timezone(str(target_tz)) target_tz = ZoneInfo(str(target_tz))
except pytz.UnknownTimeZoneError: except ZoneInfoNotFoundError:
target_tz = pytz.utc target_tz = ZoneInfo('UTC')
target_time = time.replace(tzinfo=source_tz).astimezone(target_tz) target_time = time.replace(tzinfo=source_tz).astimezone(target_tz)

View File

@ -19,9 +19,9 @@ import django.core.exceptions
from django.core.validators import URLValidator from django.core.validators import URLValidator
from django.http import Http404 from django.http import Http404
import pytz
import structlog import structlog
from dotenv import load_dotenv from dotenv import load_dotenv
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
from InvenTree.cache import get_cache_config, is_global_cache_enabled from InvenTree.cache import get_cache_config, is_global_cache_enabled
from InvenTree.config import get_boolean_setting, get_custom_file, get_setting from InvenTree.config import get_boolean_setting, get_custom_file, get_setting
@ -1040,8 +1040,8 @@ TIME_ZONE = get_setting('INVENTREE_TIMEZONE', 'timezone', 'UTC')
# Check that the timezone is valid # Check that the timezone is valid
try: try:
pytz.timezone(TIME_ZONE) ZoneInfo(TIME_ZONE)
except pytz.exceptions.UnknownTimeZoneError: # pragma: no cover except ZoneInfoNotFoundError: # pragma: no cover
raise ValueError(f"Specified timezone '{TIME_ZONE}' is not valid") raise ValueError(f"Specified timezone '{TIME_ZONE}' is not valid")
USE_I18N = True USE_I18N = True

View File

@ -16,12 +16,12 @@ from django.urls import reverse
from django.utils import timezone from django.utils import timezone
import pint.errors import pint.errors
import pytz
from djmoney.contrib.exchange.exceptions import MissingRate from djmoney.contrib.exchange.exceptions import MissingRate
from djmoney.contrib.exchange.models import Rate, convert_money from djmoney.contrib.exchange.models import Rate, convert_money
from djmoney.money import Money from djmoney.money import Money
from maintenance_mode.core import get_maintenance_mode, set_maintenance_mode from maintenance_mode.core import get_maintenance_mode, set_maintenance_mode
from sesame.utils import get_user from sesame.utils import get_user
from zoneinfo import ZoneInfo
import InvenTree.conversion import InvenTree.conversion
import InvenTree.format import InvenTree.format
@ -736,14 +736,14 @@ class TestTimeFormat(TestCase):
month=1, month=1,
day=1, day=1,
hour=0, hour=0,
minute=0, minute=1,
second=0, second=0,
tzinfo=pytz.timezone('Europe/London'), tzinfo=ZoneInfo('Europe/London'),
) )
tests = [ tests = [
('UTC', '2000-01-01 00:01:00+00:00'), ('UTC', '2000-01-01 00:01:00+00:00'),
('Europe/London', '2000-01-01 00:00:00-00:01'), ('Europe/London', '2000-01-01 00:01:00+00:00'),
('America/New_York', '1999-12-31 19:01:00-05:00'), ('America/New_York', '1999-12-31 19:01:00-05:00'),
# All following tests should result in the same value # All following tests should result in the same value
('Australia/Sydney', '2000-01-01 11:01:00+11:00'), ('Australia/Sydney', '2000-01-01 11:01:00+11:00'),

View File

@ -10,8 +10,8 @@ from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.utils.safestring import SafeString from django.utils.safestring import SafeString
import pytz
from PIL import Image from PIL import Image
from zoneinfo import ZoneInfo
import report.models as report_models import report.models as report_models
from build.models import Build from build.models import Build
@ -182,17 +182,17 @@ class ReportTagTest(TestCase):
hour=12, hour=12,
minute=30, minute=30,
second=0, second=0,
tzinfo=pytz.timezone('Australia/Sydney'), tzinfo=ZoneInfo('Australia/Sydney'),
) )
# Format a set of tests: timezone, format, expected # Format a set of tests: timezone, format, expected
tests = [ tests = [
(None, None, '2024-03-12T22:25:00-04:00'), (None, None, '2024-03-12T21:30:00-04:00'),
(None, '%d-%m-%y', '12-03-24'), (None, '%d-%m-%y', '12-03-24'),
('UTC', None, '2024-03-13T02:25:00+00:00'), ('UTC', None, '2024-03-13T01:30:00+00:00'),
('UTC', '%d-%B-%Y', '13-March-2024'), ('UTC', '%d-%B-%Y', '13-March-2024'),
('Europe/Amsterdam', None, '2024-03-13T03:25:00+01:00'), ('Europe/Amsterdam', None, '2024-03-13T02:30:00+01:00'),
('Europe/Amsterdam', '%y-%m-%d %H:%M', '24-03-13 03:25'), ('Europe/Amsterdam', '%y-%m-%d %H:%M', '24-03-13 02:30'),
] ]
for tz, fmt, expected in tests: for tz, fmt, expected in tests: