Skip to content

Commit

Permalink
Use datetime timezone aliased as tzone
Browse files Browse the repository at this point in the history
  • Loading branch information
norkans7 committed Dec 5, 2023
1 parent cad277d commit 4e54070
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 11 deletions.
6 changes: 2 additions & 4 deletions smartmin/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import json
import traceback
import zoneinfo
from datetime import datetime
from datetime import datetime, timezone as tzone

from xlrd import XL_CELL_DATE, XLRDError, open_workbook, xldate_as_tuple

Expand Down Expand Up @@ -220,9 +220,7 @@ def import_xls(cls, filename, user, import_params, log=None, import_results=None
# timezone for date cells can be specified as an import parameter or defaults to UTC
# use now to determine a relevant timezone
naive_timezone = (
zoneinfo.ZoneInfo(import_params["timezone"])
if import_params and "timezone" in import_params
else timezone.timezone.utc
zoneinfo.ZoneInfo(import_params["timezone"]) if import_params and "timezone" in import_params else tzone.utc
)
tz = timezone.now().astimezone(naive_timezone).tzinfo

Expand Down
8 changes: 4 additions & 4 deletions smartmin/templatetags/smartmin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import re
import zoneinfo
from datetime import datetime, timedelta
from datetime import datetime, timedelta, timezone as tzone

from django import template
from django.conf import settings
Expand Down Expand Up @@ -34,7 +34,7 @@ def format_datetime(time):
"""
user_time_zone = timezone.get_current_timezone()
if time.tzinfo is None:
time = time.replace(tzinfo=timezone.timezone.utc)
time = time.replace(tzinfo=tzone.utc)
user_time_zone = zoneinfo.ZoneInfo(getattr(settings, "USER_TIME_ZONE", "GMT"))

time = time.astimezone(user_time_zone)
Expand Down Expand Up @@ -164,7 +164,7 @@ def map(string, args):
@register.filter
def gmail_time(dtime, now=None):
if dtime.tzinfo is None:
dtime = dtime.replace(tzinfo=timezone.timezone.utc)
dtime = dtime.replace(tzinfo=tzone.utc)
user_time_zone = zoneinfo.ZoneInfo(getattr(settings, "USER_TIME_ZONE", "GMT"))
dtime = dtime.astimezone(user_time_zone)
else:
Expand All @@ -174,7 +174,7 @@ def gmail_time(dtime, now=None):
now = timezone.now()

if now.tzinfo is None:
now = now.replace(tzinfo=timezone.timezone.utc)
now = now.replace(tzinfo=tzone.utc)

twelve_hours_ago = now - timedelta(hours=12)

Expand Down
6 changes: 3 additions & 3 deletions test_runner/blog/tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import json
from datetime import datetime, timedelta, timezone as dt_timezone
from datetime import datetime, timedelta, timezone as tzone
from unittest.mock import patch

from zoneinfo import ZoneInfo
Expand Down Expand Up @@ -1141,7 +1141,7 @@ def setUp(self):
def test_value_from_view(self):
context = dict(view=self.read_view, object=self.post)
self.assertEquals(self.post.title, get_value_from_view(context, "title"))
local_created = self.post.created_on.replace(tzinfo=timezone.timezone.utc).astimezone(ZoneInfo("Africa/Kigali"))
local_created = self.post.created_on.replace(tzinfo=tzone.utc).astimezone(ZoneInfo("Africa/Kigali"))
self.assertEquals(local_created.strftime("%b %d, %Y %H:%M"), get_value_from_view(context, "created_on"))

def test_view_as_json(self):
Expand Down Expand Up @@ -1183,7 +1183,7 @@ def test_gmail_time(self):
self.assertEqual(test_date.strftime("%b") + " 2", gmail_time(test_date, now))

# but a different year is different
jan_2 = datetime(2012, 1, 2, 17, 5, 0, 0).replace(tzinfo=timezone.timezone.utc)
jan_2 = datetime(2012, 1, 2, 17, 5, 0, 0).replace(tzinfo=tzone.utc)
self.assertEquals("2/1/12", gmail_time(jan_2, now))

def test_user_as_string(self):
Expand Down

0 comments on commit 4e54070

Please sign in to comment.