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

Commit

Permalink
chore(line_protocol): fix nanosecond timestamp resolution for points (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
sebito91 authored Apr 8, 2020
1 parent 4799c58 commit 04205cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
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

0 comments on commit 04205cf

Please sign in to comment.