Skip to content

Commit d278446

Browse files
authored
Merge pull request influxdata#368 from tzonghao/fix-timezone
Add proper timestamp timezone handling
2 parents 0cd7a1f + cbde490 commit d278446

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

influxdb/line_protocol.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,26 @@
55
from __future__ import print_function
66
from __future__ import unicode_literals
77

8-
from calendar import timegm
98
from copy import copy
109
from datetime import datetime
1110
from numbers import Integral
1211

12+
from pytz import UTC
1313
from dateutil.parser import parse
1414
from six import binary_type, text_type, integer_types, PY2
1515

16+
EPOCH = UTC.localize(datetime.utcfromtimestamp(0))
17+
1618

1719
def _convert_timestamp(timestamp, precision=None):
1820
if isinstance(timestamp, Integral):
1921
return timestamp # assume precision is correct if timestamp is int
2022
if isinstance(_get_unicode(timestamp), text_type):
2123
timestamp = parse(timestamp)
2224
if isinstance(timestamp, datetime):
23-
ns = (
24-
timegm(timestamp.utctimetuple()) * 1e9 +
25-
timestamp.microsecond * 1e3
26-
)
25+
if not timestamp.tzinfo:
26+
timestamp = UTC.localize(timestamp)
27+
ns = (timestamp - EPOCH).total_seconds() * 1e9
2728
if precision is None or precision == 'n':
2829
return ns
2930
elif precision == 'u':
@@ -36,7 +37,6 @@ def _convert_timestamp(timestamp, precision=None):
3637
return ns / 1e9 / 60
3738
elif precision == 'h':
3839
return ns / 1e9 / 3600
39-
4040
raise ValueError(timestamp)
4141

4242

influxdb/tests/test_line_protocol.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from __future__ import print_function
66
from __future__ import unicode_literals
77

8+
from datetime import datetime
89
import unittest
10+
from pytz import UTC, timezone
911

1012
from influxdb import line_protocol
1113

@@ -40,6 +42,35 @@ def test_make_lines(self):
4042
'bool_val=True,float_val=1.1,int_val=1i,string_val="hello!"\n'
4143
)
4244

45+
def test_timezone(self):
46+
dt = datetime(2009, 11, 10, 23, 0, 0, 123456)
47+
utc = UTC.localize(dt)
48+
berlin = timezone('Europe/Berlin').localize(dt)
49+
eastern = berlin.astimezone(timezone('US/Eastern'))
50+
data = {
51+
"points": [
52+
{"measurement": "A", "fields": {"val": 1},
53+
"time": 0},
54+
{"measurement": "A", "fields": {"val": 1},
55+
"time": "2009-11-10T23:00:00.123456Z"},
56+
{"measurement": "A", "fields": {"val": 1}, "time": dt},
57+
{"measurement": "A", "fields": {"val": 1}, "time": utc},
58+
{"measurement": "A", "fields": {"val": 1}, "time": berlin},
59+
{"measurement": "A", "fields": {"val": 1}, "time": eastern},
60+
]
61+
}
62+
self.assertEqual(
63+
line_protocol.make_lines(data),
64+
'\n'.join([
65+
'A val=1i 0',
66+
'A val=1i 1257894000123456000',
67+
'A val=1i 1257894000123456000',
68+
'A val=1i 1257894000123456000',
69+
'A val=1i 1257890400123456000',
70+
'A val=1i 1257890400123456000',
71+
]) + '\n'
72+
)
73+
4374
def test_string_val_newline(self):
4475
data = {
4576
"points": [

0 commit comments

Comments
 (0)