switch to zoneinfo from pytz

This commit is contained in:
Matthias Mair 2024-07-13 23:22:45 +02:00
parent e5814ea486
commit d2d4a44705
No known key found for this signature in database
GPG Key ID: A593429DDA23B66A
4 changed files with 12 additions and 12 deletions

View File

@ -21,11 +21,11 @@ from django.core.files.storage import Storage, default_storage
from django.http import StreamingHttpResponse from django.http import StreamingHttpResponse
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
import pytz
import regex import regex
from bleach import clean from bleach import clean
from djmoney.money import Money from djmoney.money import Money
from PIL import Image from PIL import Image
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
import InvenTree.version import InvenTree.version
from common.currency import currency_code_default from common.currency import currency_code_default
@ -926,15 +926,15 @@ def to_local_time(time, target_tz: 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(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

@ -20,8 +20,8 @@ from django.core.validators import URLValidator
from django.http import Http404 from django.http import Http404
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
import pytz
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
@ -942,8 +942,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

@ -17,12 +17,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
@ -739,7 +739,7 @@ class TestTimeFormat(TestCase):
hour=0, hour=0,
minute=0, minute=0,
second=0, second=0,
tzinfo=pytz.timezone('Europe/London'), tzinfo=ZoneInfo('Europe/London'),
) )
tests = [ tests = [

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
@ -169,7 +169,7 @@ 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