feat: add make_utc() function, app method
as prep for dropping timezone from DB columns
This commit is contained in:
parent
1a3756f47c
commit
900937826c
4 changed files with 107 additions and 0 deletions
|
|
@ -1,5 +1,6 @@
|
|||
# -*- coding: utf-8; -*-
|
||||
|
||||
import datetime
|
||||
import sys
|
||||
from unittest import TestCase
|
||||
from unittest.mock import patch, MagicMock
|
||||
|
|
@ -164,6 +165,53 @@ class TestLoadObject(TestCase):
|
|||
self.assertIs(result, TestCase)
|
||||
|
||||
|
||||
class TestMakeUTC(TestCase):
|
||||
|
||||
def test_current_time(self):
|
||||
|
||||
# no tzinfo by default
|
||||
dt = mod.make_utc()
|
||||
self.assertIsInstance(dt, datetime.datetime)
|
||||
self.assertIsNone(dt.tzinfo)
|
||||
now = datetime.datetime.now(datetime.timezone.utc).replace(tzinfo=None)
|
||||
self.assertAlmostEqual(int(dt.timestamp()), int(now.timestamp()))
|
||||
|
||||
# with tzinfo
|
||||
dt = mod.make_utc(tzinfo=True)
|
||||
self.assertIsInstance(dt, datetime.datetime)
|
||||
self.assertIs(dt.tzinfo, datetime.timezone.utc)
|
||||
now = datetime.datetime.now(datetime.timezone.utc)
|
||||
self.assertAlmostEqual(int(dt.timestamp()), int(now.timestamp()))
|
||||
|
||||
def test_convert_with_tzinfo(self):
|
||||
sample = datetime.datetime(
|
||||
2024, 9, 15, 8, 30, tzinfo=datetime.timezone(-datetime.timedelta(hours=5))
|
||||
)
|
||||
|
||||
# no tzinfo by default
|
||||
dt = mod.make_utc(sample)
|
||||
self.assertEqual(dt, datetime.datetime(2024, 9, 15, 13, 30, tzinfo=None))
|
||||
|
||||
# with tzinfo
|
||||
dt = mod.make_utc(sample, tzinfo=True)
|
||||
self.assertEqual(
|
||||
dt, datetime.datetime(2024, 9, 15, 13, 30, tzinfo=datetime.timezone.utc)
|
||||
)
|
||||
|
||||
def test_convert_without_tzinfo(self):
|
||||
sample = datetime.datetime(2024, 9, 15, 8, 30)
|
||||
|
||||
# no tzinfo by default
|
||||
dt = mod.make_utc(sample)
|
||||
self.assertEqual(dt, datetime.datetime(2024, 9, 15, 8, 30, tzinfo=None))
|
||||
|
||||
# with tzinfo
|
||||
dt = mod.make_utc(sample, tzinfo=True)
|
||||
self.assertEqual(
|
||||
dt, datetime.datetime(2024, 9, 15, 8, 30, tzinfo=datetime.timezone.utc)
|
||||
)
|
||||
|
||||
|
||||
class TestMakeUUID(TestCase):
|
||||
|
||||
def test_basic(self):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue