4
4
from decimal import Decimal
5
5
from numbers import Integral
6
6
7
+ from dateutil .parser import parser
7
8
from pytz import UTC
8
9
from six import iteritems
9
10
10
- from influxdb_client .client .flux_csv_parser import parse_string_to_datetime
11
+ from influxdb_client .client .flux_csv_parser import get_date_parse_function
11
12
from influxdb_client .domain .write_precision import WritePrecision
12
13
13
14
EPOCH = UTC .localize (datetime .utcfromtimestamp (0 ))
@@ -46,6 +47,9 @@ def __init__(self, measurement_name):
46
47
self ._time = None
47
48
self ._write_precision = DEFAULT_WRITE_PRECISION
48
49
50
+ self ._parse_function = get_date_parse_function ()
51
+ pass
52
+
49
53
def time (self , time , write_precision = DEFAULT_WRITE_PRECISION ):
50
54
"""
51
55
Specify timestamp for DataPoint with declared precision.
@@ -80,7 +84,7 @@ def to_line_protocol(self):
80
84
_fields = _append_fields (self ._fields )
81
85
if not _fields :
82
86
return ""
83
- _time = _append_time (self ._time , self ._write_precision )
87
+ _time = _append_time (self ._time , self ._write_precision , self . _parse_function )
84
88
85
89
return f"{ _measurement } { _tags } { _fields } { _time } "
86
90
@@ -127,10 +131,10 @@ def _append_fields(fields):
127
131
return f"{ ',' .join (_return )} "
128
132
129
133
130
- def _append_time (time , write_precision ):
134
+ def _append_time (time , write_precision , _parse_function ):
131
135
if time is None :
132
136
return ''
133
- return f" { int (_convert_timestamp (time , write_precision ))} "
137
+ return f" { int (_convert_timestamp (time , write_precision , _parse_function ))} "
134
138
135
139
136
140
def _escape_key (tag ):
@@ -148,12 +152,12 @@ def _escape_string(value):
148
152
return str (value ).translate (_ESCAPE_STRING )
149
153
150
154
151
- def _convert_timestamp (timestamp , precision = DEFAULT_WRITE_PRECISION ):
155
+ def _convert_timestamp (timestamp , precision = DEFAULT_WRITE_PRECISION , _parse_function = parser . parse ):
152
156
if isinstance (timestamp , Integral ):
153
157
return timestamp # assume precision is correct if timestamp is int
154
158
155
159
if isinstance (timestamp , str ):
156
- timestamp = parse_string_to_datetime (timestamp )
160
+ timestamp = _parse_function (timestamp )
157
161
158
162
if isinstance (timestamp , timedelta ) or isinstance (timestamp , datetime ):
159
163
0 commit comments