From f43baf569991321549e42fc53e7a0cc214f76df5 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Thu, 29 Apr 2021 11:11:15 +0200 Subject: [PATCH 1/3] feat: add possibility to specify default timezone for datetimes without tzinfo --- influxdb_client/client/util/date_utils.py | 11 ++++++++- tests/test_DateHelper.py | 28 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/test_DateHelper.py diff --git a/influxdb_client/client/util/date_utils.py b/influxdb_client/client/util/date_utils.py index f7557b19..f1f6f39f 100644 --- a/influxdb_client/client/util/date_utils.py +++ b/influxdb_client/client/util/date_utils.py @@ -10,6 +10,15 @@ class DateHelper: """DateHelper to groups different implementations of date operations.""" + def __init__(self, timezone: datetime.tzinfo = UTC) -> None: + """ + Initialize defaults. + + :param timezone: Default timezone used for serialization "datetime" without "tzinfo". + Default value is "UTC". + """ + self.timezone = timezone + def parse_date(self, date_string: str): """ Parse string into Date or Timestamp. @@ -40,7 +49,7 @@ def to_utc(self, value: datetime): :return: datetime in UTC """ if not value.tzinfo: - return UTC.localize(value) + return self.to_utc(value.replace(tzinfo=self.timezone)) else: return value.astimezone(UTC) diff --git a/tests/test_DateHelper.py b/tests/test_DateHelper.py new file mode 100644 index 00000000..881a8a09 --- /dev/null +++ b/tests/test_DateHelper.py @@ -0,0 +1,28 @@ +# -*- coding: utf-8 -*- + +import unittest +from datetime import datetime, timezone + +from pytz import UTC, timezone + +from influxdb_client.client.util import date_utils +from influxdb_client.client.util.date_utils import DateHelper, get_date_helper + + +class DateHelperTest(unittest.TestCase): + + def setUp(self) -> None: + date_utils.date_helper = DateHelper() + + def test_to_utc(self): + date = get_date_helper().to_utc(datetime(2021, 4, 29, 20, 30, 10, 0)) + self.assertEqual(datetime(2021, 4, 29, 20, 30, 10, 0, UTC), date) + + def test_to_utc_different_timezone(self): + date_utils.date_helper = DateHelper(timezone=timezone('ETC/GMT+2')) + date = get_date_helper().to_utc(datetime(2021, 4, 29, 20, 30, 10, 0)) + self.assertEqual(datetime(2021, 4, 29, 22, 30, 10, 0, UTC), date) + + +if __name__ == '__main__': + unittest.main() From 80bdf3d9f461515d662800d110aed625abd31b60 Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Thu, 29 Apr 2021 11:20:40 +0200 Subject: [PATCH 2/3] docs: update CHANGELOG.md --- CHANGELOG.md | 1 + tests/test_DateHelper.py | 11 +++-------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dbc23dfb..217b9e2b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ ### Features 1. [#203](https://github.com/influxdata/influxdb-client-python/issues/219): Bind query parameters 1. [#225](https://github.com/influxdata/influxdb-client-python/pull/225): Exponential random backoff retry strategy +1. [#238](https://github.com/influxdata/influxdb-client-python/pull/238): Add possibility to specify default `timezone` for datetimes without `tzinfo` ### Bug Fixes 1. [#222](https://github.com/influxdata/influxdb-client-python/pull/222): Pass configured timeout to HTTP client diff --git a/tests/test_DateHelper.py b/tests/test_DateHelper.py index 881a8a09..abff4e56 100644 --- a/tests/test_DateHelper.py +++ b/tests/test_DateHelper.py @@ -5,22 +5,17 @@ from pytz import UTC, timezone -from influxdb_client.client.util import date_utils -from influxdb_client.client.util.date_utils import DateHelper, get_date_helper +from influxdb_client.client.util.date_utils import DateHelper class DateHelperTest(unittest.TestCase): - def setUp(self) -> None: - date_utils.date_helper = DateHelper() - def test_to_utc(self): - date = get_date_helper().to_utc(datetime(2021, 4, 29, 20, 30, 10, 0)) + date = DateHelper().to_utc(datetime(2021, 4, 29, 20, 30, 10, 0)) self.assertEqual(datetime(2021, 4, 29, 20, 30, 10, 0, UTC), date) def test_to_utc_different_timezone(self): - date_utils.date_helper = DateHelper(timezone=timezone('ETC/GMT+2')) - date = get_date_helper().to_utc(datetime(2021, 4, 29, 20, 30, 10, 0)) + date = DateHelper(timezone=timezone('ETC/GMT+2')).to_utc(datetime(2021, 4, 29, 20, 30, 10, 0)) self.assertEqual(datetime(2021, 4, 29, 22, 30, 10, 0, UTC), date) From 18712befc1ecd367280949df1a1c55e662667d3e Mon Sep 17 00:00:00 2001 From: Jakub Bednar Date: Fri, 30 Apr 2021 08:46:28 +0200 Subject: [PATCH 3/3] docs: update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 217b9e2b..891af116 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,13 +3,13 @@ ### Features 1. [#237](https://github.com/influxdata/influxdb-client-python/pull/237): Use kwargs to pass query parameters into API list call - useful for the ability to use pagination. 1. [#241](https://github.com/influxdata/influxdb-client-python/pull/241): Add detail error message for not supported type of `Point.field` +1. [#238](https://github.com/influxdata/influxdb-client-python/pull/238): Add possibility to specify default `timezone` for datetimes without `tzinfo` ## 1.17.0 [2021-04-30] ### Features 1. [#203](https://github.com/influxdata/influxdb-client-python/issues/219): Bind query parameters 1. [#225](https://github.com/influxdata/influxdb-client-python/pull/225): Exponential random backoff retry strategy -1. [#238](https://github.com/influxdata/influxdb-client-python/pull/238): Add possibility to specify default `timezone` for datetimes without `tzinfo` ### Bug Fixes 1. [#222](https://github.com/influxdata/influxdb-client-python/pull/222): Pass configured timeout to HTTP client