From cbde490024e439ab780af0e4fb1832ef5a4f0f00 Mon Sep 17 00:00:00 2001 From: "tzonghao.chen" Date: Fri, 9 Sep 2016 16:25:10 -0400 Subject: [PATCH] Add proper timestamp timezone handling Fix bug #250 --- influxdb/line_protocol.py | 12 +++++------ influxdb/tests/test_line_protocol.py | 31 ++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/influxdb/line_protocol.py b/influxdb/line_protocol.py index 9e0f0743..7c8c8f24 100644 --- a/influxdb/line_protocol.py +++ b/influxdb/line_protocol.py @@ -5,14 +5,16 @@ from __future__ import print_function from __future__ import unicode_literals -from calendar import timegm from copy import copy from datetime import datetime from numbers import Integral +from pytz import UTC from dateutil.parser import parse from six import binary_type, text_type, integer_types, PY2 +EPOCH = UTC.localize(datetime.utcfromtimestamp(0)) + def _convert_timestamp(timestamp, precision=None): if isinstance(timestamp, Integral): @@ -20,10 +22,9 @@ def _convert_timestamp(timestamp, precision=None): if isinstance(_get_unicode(timestamp), text_type): timestamp = parse(timestamp) if isinstance(timestamp, datetime): - ns = ( - timegm(timestamp.utctimetuple()) * 1e9 + - timestamp.microsecond * 1e3 - ) + if not timestamp.tzinfo: + timestamp = UTC.localize(timestamp) + ns = (timestamp - EPOCH).total_seconds() * 1e9 if precision is None or precision == 'n': return ns elif precision == 'u': @@ -36,7 +37,6 @@ def _convert_timestamp(timestamp, precision=None): return ns / 1e9 / 60 elif precision == 'h': return ns / 1e9 / 3600 - raise ValueError(timestamp) diff --git a/influxdb/tests/test_line_protocol.py b/influxdb/tests/test_line_protocol.py index 1d0b377c..726f8705 100644 --- a/influxdb/tests/test_line_protocol.py +++ b/influxdb/tests/test_line_protocol.py @@ -5,7 +5,9 @@ from __future__ import print_function from __future__ import unicode_literals +from datetime import datetime import unittest +from pytz import UTC, timezone from influxdb import line_protocol @@ -40,6 +42,35 @@ def test_make_lines(self): 'bool_val=True,float_val=1.1,int_val=1i,string_val="hello!"\n' ) + def test_timezone(self): + dt = datetime(2009, 11, 10, 23, 0, 0, 123456) + utc = UTC.localize(dt) + berlin = timezone('Europe/Berlin').localize(dt) + eastern = berlin.astimezone(timezone('US/Eastern')) + data = { + "points": [ + {"measurement": "A", "fields": {"val": 1}, + "time": 0}, + {"measurement": "A", "fields": {"val": 1}, + "time": "2009-11-10T23:00:00.123456Z"}, + {"measurement": "A", "fields": {"val": 1}, "time": dt}, + {"measurement": "A", "fields": {"val": 1}, "time": utc}, + {"measurement": "A", "fields": {"val": 1}, "time": berlin}, + {"measurement": "A", "fields": {"val": 1}, "time": eastern}, + ] + } + self.assertEqual( + line_protocol.make_lines(data), + '\n'.join([ + 'A val=1i 0', + 'A val=1i 1257894000123456000', + 'A val=1i 1257894000123456000', + 'A val=1i 1257894000123456000', + 'A val=1i 1257890400123456000', + 'A val=1i 1257890400123456000', + ]) + '\n' + ) + def test_string_val_newline(self): data = { "points": [ pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy