Skip to content

Commit 5ddf643

Browse files
authored
feat: convert timezone to UTC if tzinfo exists (#113)
1 parent 91ffed1 commit 5ddf643

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## 1.9.0 [unreleased]
22

3+
### Features
4+
1. [#112](https://github.com/influxdata/influxdb-client-python/pull/113): Support timestamp with different timezone in _convert_timestamp
5+
36
## 1.8.0 [2020-06-19]
47

58
### Features

influxdb_client/client/write/point.py

+2
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,8 @@ def _convert_timestamp(timestamp, precision=DEFAULT_WRITE_PRECISION):
160160
if isinstance(timestamp, datetime):
161161
if not timestamp.tzinfo:
162162
timestamp = UTC.localize(timestamp)
163+
else:
164+
timestamp = timestamp.astimezone(UTC)
163165
ns = (timestamp - EPOCH).total_seconds() * 1e9
164166
else:
165167
ns = timestamp.total_seconds() * 1e9

tests/test_point.py

+8
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,14 @@ def test_from_dict_without_tags(self):
309309
point = Point.from_dict(json)
310310
self.assertEqual("my-org field1=1i,field2=2i", point.to_line_protocol())
311311

312+
def test_points_from_different_timezones(self):
313+
time_in_utc = UTC.localize(datetime(2020, 7, 4, 0, 0, 0, 123456))
314+
time_in_hk = timezone('Asia/Hong_Kong').localize(datetime(2020, 7, 4, 8, 0, 0, 123456)) # +08:00
315+
316+
point_utc = Point.measurement("h2o").field("val", 1).time(time_in_utc)
317+
point_hk = Point.measurement("h2o").field("val", 1).time(time_in_hk)
318+
self.assertEqual(point_utc.to_line_protocol(), point_hk.to_line_protocol())
319+
312320

313321
if __name__ == '__main__':
314322
unittest.main()

0 commit comments

Comments
 (0)