From 654d5fc97841dfc81903f851a32fac7a67efc73f Mon Sep 17 00:00:00 2001 From: Norbert Kwizera Date: Wed, 6 Dec 2023 11:39:11 +0200 Subject: [PATCH] Use datetime timezone aliased as tzone --- dash/orgs/management/commands/orgtasks.py | 4 ++-- dash/orgs/templatetags/dashorgs.py | 4 ++-- dash/utils/__init__.py | 6 ++++-- dash/utils/tests.py | 8 ++++---- 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/dash/orgs/management/commands/orgtasks.py b/dash/orgs/management/commands/orgtasks.py index bd5a384..f14f50f 100644 --- a/dash/orgs/management/commands/orgtasks.py +++ b/dash/orgs/management/commands/orgtasks.py @@ -1,4 +1,4 @@ -from datetime import timezone +from datetime import timezone as tzone from django.core.management.base import BaseCommand, CommandError @@ -105,4 +105,4 @@ def cell(val, width): def format_date(dt): - return dt.astimezone(timezone.utc).strftime("%b %d, %Y %H:%M") if dt else "" + return dt.astimezone(tzone.utc).strftime("%b %d, %Y %H:%M") if dt else "" diff --git a/dash/orgs/templatetags/dashorgs.py b/dash/orgs/templatetags/dashorgs.py index 7c091da..c152d61 100644 --- a/dash/orgs/templatetags/dashorgs.py +++ b/dash/orgs/templatetags/dashorgs.py @@ -1,4 +1,4 @@ -from datetime import datetime, timezone +from datetime import datetime, timezone as tzone import phonenumbers @@ -13,7 +13,7 @@ def display_time(text_timestamp, org, time_format=None): if not time_format: time_format = "%b %d, %Y %H:%M" - parsed_time = datetime.strptime(text_timestamp, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=timezone.utc) + parsed_time = datetime.strptime(text_timestamp, "%Y-%m-%dT%H:%M:%SZ").replace(tzinfo=tzone.utc) output_time = parsed_time.astimezone(org.timezone) return output_time.strftime(time_format) diff --git a/dash/utils/__init__.py b/dash/utils/__init__.py index 713aec0..e2e9014 100644 --- a/dash/utils/__init__.py +++ b/dash/utils/__init__.py @@ -7,6 +7,8 @@ from itertools import islice from uuid import uuid4 +from datetime import datetime, timezone as tzone + from dateutil.relativedelta import relativedelta from django.core.cache import cache @@ -101,8 +103,8 @@ def ms_to_datetime(ms): """ Converts a millisecond accuracy timestamp to a datetime """ - dt = datetime.datetime.utcfromtimestamp(ms / 1000) - return dt.replace(microsecond=(ms % 1000) * 1000).replace(tzinfo=datetime.timezone.utc) + dt = datetime.utcfromtimestamp(ms / 1000) + return dt.replace(microsecond=(ms % 1000) * 1000).replace(tzinfo=tzone.utc) def get_month_range(d=None): diff --git a/dash/utils/tests.py b/dash/utils/tests.py index 9b14755..bbeb74d 100644 --- a/dash/utils/tests.py +++ b/dash/utils/tests.py @@ -1,6 +1,6 @@ import json import zoneinfo -from datetime import datetime, timezone +from datetime import datetime, timezone as tzone from itertools import chain from django.core.cache import cache @@ -76,16 +76,16 @@ def calculate(): self.assertEqual(get_obj_cacheable(self, "_test_value", calculate, recalculate=True), "CALCULATED") def test_datetime_to_ms(self): - d1 = datetime(2014, 1, 2, 3, 4, 5, 678900, tzinfo=timezone.utc) + d1 = datetime(2014, 1, 2, 3, 4, 5, 678900, tzinfo=tzone.utc) self.assertEqual(datetime_to_ms(d1), 1388631845678) # from http://unixtimestamp.50x.eu # conversion to millis loses some accuracy - self.assertEqual(ms_to_datetime(1388631845678), datetime(2014, 1, 2, 3, 4, 5, 678000, tzinfo=timezone.utc)) + self.assertEqual(ms_to_datetime(1388631845678), datetime(2014, 1, 2, 3, 4, 5, 678000, tzinfo=tzone.utc)) tz = zoneinfo.ZoneInfo("Africa/Kigali") d2 = datetime(2014, 1, 2, 3, 4, 5, 600000, tz) self.assertEqual(datetime_to_ms(d2), 1388624645600) - self.assertEqual(ms_to_datetime(1388624645600), d2.astimezone(timezone.utc)) + self.assertEqual(ms_to_datetime(1388624645600), d2.astimezone(tzone.utc)) def test_get_month_range(self): self.assertEqual(