M1ha 2019-06-28 11:29:15 +05:00
parent 355b8c7321
commit a3e8d71bd8

View File

@ -1,5 +1,5 @@
import calendar
import datetime
import time
from queue import Queue
from time import gmtime, localtime
@ -12,9 +12,27 @@ from django_clickhouse.utils import get_tz_offset, format_datetime, lazy_class_i
SingletonMeta
def system_tz_offset(): # type: () -> int
"""
ClickHouse timezone is equal to system zone offset in seconds.
THis function gets system timezone
:return: Time zone offset in minutes
"""
return int((calendar.timegm(gmtime()) - calendar.timegm(localtime())) / 60)
def local_dt_str(dt) -> str:
"""
Returns string representation of an aware datetime object, localized by adding system_tz_offset()
:param dt: Datetime to change
:return: Formatted string
"""
return (dt + datetime.timedelta(minutes=system_tz_offset())).strftime('%Y-%m-%d %H:%M:%S')
class GetTZOffsetTest(TestCase):
def test_func(self):
self.assertEqual(300, get_tz_offset())
self.assertEqual(system_tz_offset(), get_tz_offset())
class FormatDateTimeTest(TestCase):
@ -28,14 +46,13 @@ class FormatDateTimeTest(TestCase):
moscow_minute_offset = dt.utcoffset().total_seconds() / 60
zone_h, zone_m = abs(int(moscow_minute_offset / 60)), int(moscow_minute_offset % 60)
# +5 за счет времени тестового сервера ClickHouse
return (dt - datetime.timedelta(hours=zone_h - 5, minutes=zone_m)).strftime("%Y-%m-%d %H:%M:%S")
return local_dt_str(dt - datetime.timedelta(hours=zone_h, minutes=zone_m))
def test_conversion(self):
dt = datetime.datetime(2017, 1, 2, 3, 4, 5)
self.assertEqual(format_datetime(dt), '2017-01-02 08:04:05')
self.assertEqual(format_datetime(dt), local_dt_str(dt))
dt = datetime.datetime(2017, 1, 2, 3, 4, 5, tzinfo=pytz.utc)
self.assertEqual(format_datetime(dt), '2017-01-02 08:04:05')
self.assertEqual(format_datetime(dt), local_dt_str(dt))
dt = datetime.datetime(2017, 1, 2, 3, 4, 5, tzinfo=pytz.timezone('Europe/Moscow'))
self.assertEqual(format_datetime(dt), self._get_zone_time(dt))
dt = datetime.datetime(2017, 1, 2, 3, 4, 5, tzinfo=pytz.timezone('Europe/Moscow'))
@ -44,13 +61,14 @@ class FormatDateTimeTest(TestCase):
def test_date_conversion(self):
dt = datetime.date(2017, 1, 2)
self.assertEqual(format_datetime(dt), '2017-01-02 05:00:00')
self.assertEqual(format_datetime(dt), local_dt_str(datetime.datetime(2017, 1, 2, 0, 0, 0)))
dt = datetime.date(2017, 1, 2)
self.assertEqual(format_datetime(dt, day_end=True), '2017-01-03 04:59:59')
self.assertEqual(format_datetime(dt, day_end=True), local_dt_str(datetime.datetime(2017, 1, 2, 23, 59, 59)))
dt = datetime.date(2017, 1, 2)
self.assertEqual(format_datetime(dt, day_end=True, timezone_offset=60), '2017-01-03 03:59:59')
self.assertEqual(format_datetime(dt, day_end=True, timezone_offset=60),
local_dt_str(datetime.datetime(2017, 1, 2, 22, 59, 59)))
dt = datetime.date(2017, 1, 2)
self.assertEqual(format_datetime(dt, timezone_offset=60), '2017-01-02 04:00:00')
self.assertEqual(format_datetime(dt, timezone_offset=60), local_dt_str(datetime.datetime(2017, 1, 1, 23, 0, 0)))
class TestLazyClassImport(TestCase):