Skip to content

Commit

Permalink
feat: convert timezone to UTC if tzinfo exists (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
kaheicanaan authored Jun 23, 2020
1 parent 91ffed1 commit 5ddf643
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions influxdb_client/client/write/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions tests/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit 5ddf643

Please sign in to comment.