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 6, 2023
1 parent ae4658f commit cedc7d4
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 11 deletions.
4 changes: 2 additions & 2 deletions dash/orgs/management/commands/orgtasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import timezone
from datetime import timezone as tzone

from django.core.management.base import BaseCommand, CommandError

Expand Down Expand Up @@ -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 ""
4 changes: 2 additions & 2 deletions dash/orgs/templatetags/dashorgs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timezone
from datetime import datetime, timezone as tzone

import phonenumbers

Expand All @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions dash/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import calendar
import datetime
import json
import os
import random
from collections import OrderedDict
from datetime import datetime, timezone as tzone
from itertools import islice
from uuid import uuid4

Expand Down Expand Up @@ -101,8 +101,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):
Expand Down
8 changes: 4 additions & 4 deletions dash/utils/tests.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit cedc7d4

Please sign in to comment.