Skip to content
This repository was archived by the owner on Oct 29, 2024. It is now read-only.

chore(line_protocol): fix nanosecond timestamp resolution for points #811

Merged
merged 1 commit into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Clean up stale CI config (#755)
- Add legacy client test (#752 & #318 thx @oldmantaiter & @sebito91)
- Update make_lines section in line_protocol.py to split out core function (#375 thx @aisbaa)
- Fix nanosecond time resolution for points (#407 thx @AndreCAndersen && @clslgrnc)

### Removed

Expand Down
20 changes: 14 additions & 6 deletions influxdb/line_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
EPOCH = UTC.localize(datetime.utcfromtimestamp(0))


def _to_nanos(timestamp):
delta = timestamp - EPOCH
nanos_in_days = delta.days * 86400 * 10 ** 9
nanos_in_seconds = delta.seconds * 10 ** 9
nanos_in_micros = delta.microseconds * 10 ** 3
return nanos_in_days + nanos_in_seconds + nanos_in_micros


def _convert_timestamp(timestamp, precision=None):
if isinstance(timestamp, Integral):
return timestamp # assume precision is correct if timestamp is int
Expand All @@ -27,24 +35,24 @@ def _convert_timestamp(timestamp, precision=None):
if not timestamp.tzinfo:
timestamp = UTC.localize(timestamp)

ns = (timestamp - EPOCH).total_seconds() * 1e9
ns = _to_nanos(timestamp)
if precision is None or precision == 'n':
return ns

if precision == 'u':
return ns / 1e3
return ns / 10**3

if precision == 'ms':
return ns / 1e6
return ns / 10**6

if precision == 's':
return ns / 1e9
return ns / 10**9

if precision == 'm':
return ns / 1e9 / 60
return ns / 10**9 / 60

if precision == 'h':
return ns / 1e9 / 3600
return ns / 10**9 / 3600

raise ValueError(timestamp)

Expand Down