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

Fixing rounding errors when inserting microseconds #407

Closed
wants to merge 5 commits into from
Closed
Changes from 2 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
10 changes: 9 additions & 1 deletion 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 @@ -24,7 +32,7 @@ def _convert_timestamp(timestamp, precision=None):
if isinstance(timestamp, datetime):
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
elif precision == 'u':
Expand Down