Skip to content

Commit 62436d4

Browse files
sebito91ocworld
authored andcommitted
chore(line_protocol): fix nanosecond timestamp resolution for points (influxdata#811)
1 parent 68b27ac commit 62436d4

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
2222
- Clean up stale CI config (#755)
2323
- Add legacy client test (#752 & #318 thx @oldmantaiter & @sebito91)
2424
- Update make_lines section in line_protocol.py to split out core function (#375 thx @aisbaa)
25+
- Fix nanosecond time resolution for points (#407 thx @AndreCAndersen && @clslgrnc)
2526

2627
### Removed
2728

influxdb/line_protocol.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,14 @@
1616
EPOCH = UTC.localize(datetime.utcfromtimestamp(0))
1717

1818

19+
def _to_nanos(timestamp):
20+
delta = timestamp - EPOCH
21+
nanos_in_days = delta.days * 86400 * 10 ** 9
22+
nanos_in_seconds = delta.seconds * 10 ** 9
23+
nanos_in_micros = delta.microseconds * 10 ** 3
24+
return nanos_in_days + nanos_in_seconds + nanos_in_micros
25+
26+
1927
def _convert_timestamp(timestamp, precision=None):
2028
if isinstance(timestamp, Integral):
2129
return timestamp # assume precision is correct if timestamp is int
@@ -27,24 +35,24 @@ def _convert_timestamp(timestamp, precision=None):
2735
if not timestamp.tzinfo:
2836
timestamp = UTC.localize(timestamp)
2937

30-
ns = (timestamp - EPOCH).total_seconds() * 1e9
38+
ns = _to_nanos(timestamp)
3139
if precision is None or precision == 'n':
3240
return ns
3341

3442
if precision == 'u':
35-
return ns / 1e3
43+
return ns / 10**3
3644

3745
if precision == 'ms':
38-
return ns / 1e6
46+
return ns / 10**6
3947

4048
if precision == 's':
41-
return ns / 1e9
49+
return ns / 10**9
4250

4351
if precision == 'm':
44-
return ns / 1e9 / 60
52+
return ns / 10**9 / 60
4553

4654
if precision == 'h':
47-
return ns / 1e9 / 3600
55+
return ns / 10**9 / 3600
4856

4957
raise ValueError(timestamp)
5058

0 commit comments

Comments
 (0)