From 44b31d75432718107df7af1f4fa799a3e716f2d9 Mon Sep 17 00:00:00 2001 From: NG KA HEI Date: Tue, 23 Jun 2020 14:37:54 +0800 Subject: [PATCH 1/3] feat: convert timezone to UTC if tzinfo exists --- CHANGELOG.md | 1 + influxdb_client/client/write/point.py | 2 ++ 2 files changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e35605ac..a1553af2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Features 1. [#92](https://github.com/influxdata/influxdb-client-python/issues/92): Optimize serializing Pandas DataFrame for writing +1. [#112](https://github.com/influxdata/influxdb-client-python/issues/112): Support timestamp with different timezone in _convert_timestamp ### API 1. [#110](https://github.com/influxdata/influxdb-client-python/pull/110): Removed log system from Bucket, Dashboard, Organization, Task and Users API - [influxdb#18459](https://github.com/influxdata/influxdb/pull/18459), Update swagger to latest version diff --git a/influxdb_client/client/write/point.py b/influxdb_client/client/write/point.py index 3b21a17d..d50117c0 100644 --- a/influxdb_client/client/write/point.py +++ b/influxdb_client/client/write/point.py @@ -160,6 +160,8 @@ def _convert_timestamp(timestamp, precision=DEFAULT_WRITE_PRECISION): if isinstance(timestamp, datetime): if not timestamp.tzinfo: timestamp = UTC.localize(timestamp) + else: + timestamp = timestamp.astimezone(UTC) ns = (timestamp - EPOCH).total_seconds() * 1e9 else: ns = timestamp.total_seconds() * 1e9 From 532766c478f93a59ed73b265843e7ab10f7eeccb Mon Sep 17 00:00:00 2001 From: NG KA HEI Date: Tue, 23 Jun 2020 15:10:28 +0800 Subject: [PATCH 2/3] Add test case for datetime timezone auto-conversion --- tests/test_point.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_point.py b/tests/test_point.py index 82add68d..698af330 100644 --- a/tests/test_point.py +++ b/tests/test_point.py @@ -309,6 +309,14 @@ def test_from_dict_without_tags(self): point = Point.from_dict(json) self.assertEqual("my-org field1=1i,field2=2i", point.to_line_protocol()) + def test_points_from_different_timezones(self): + time_in_utc = UTC.localize(datetime(2020, 7, 4, 0, 0, 0, 123456)) + time_in_hk = timezone('Asia/Hong_Kong').localize(datetime(2020, 7, 4, 8, 0, 0, 123456)) # +08:00 + + point_utc = Point.measurement("h2o").field("val", 1).time(time_in_utc) + point_hk = Point.measurement("h2o").field("val", 1).time(time_in_hk) + self.assertEqual(point_utc.to_line_protocol(), point_hk.to_line_protocol()) + if __name__ == '__main__': unittest.main() From fd14e664411df40874aa3d429ac904ee1e340d91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Bedn=C3=A1=C5=99?= Date: Tue, 23 Jun 2020 09:39:35 +0200 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a1553af2..907eedff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,12 @@ ## 1.9.0 [unreleased] +### Features +1. [#112](https://github.com/influxdata/influxdb-client-python/pull/113): Support timestamp with different timezone in _convert_timestamp + ## 1.8.0 [2020-06-19] ### Features 1. [#92](https://github.com/influxdata/influxdb-client-python/issues/92): Optimize serializing Pandas DataFrame for writing -1. [#112](https://github.com/influxdata/influxdb-client-python/issues/112): Support timestamp with different timezone in _convert_timestamp ### API 1. [#110](https://github.com/influxdata/influxdb-client-python/pull/110): Removed log system from Bucket, Dashboard, Organization, Task and Users API - [influxdb#18459](https://github.com/influxdata/influxdb/pull/18459), Update swagger to latest version