From 5ddf643564bdd63aeb922a88e0a2c27d3a123de6 Mon Sep 17 00:00:00 2001 From: Ka Hei Date: Tue, 23 Jun 2020 15:42:20 +0800 Subject: [PATCH] feat: convert timezone to UTC if tzinfo exists (#113) --- CHANGELOG.md | 3 +++ influxdb_client/client/write/point.py | 2 ++ tests/test_point.py | 8 ++++++++ 3 files changed, 13 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e35605ac..907eedff 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ## 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 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 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()