Skip to content

Commit 465476b

Browse files
authored
fix: formatting nanoseconds to Flux AST (#457)
1 parent 2089a33 commit 465476b

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
### Features
44
1. [#440](https://github.com/influxdata/influxdb-client-python/pull/440): Add possibility to specify timestamp column and its timezone [DataFrame]
55

6+
### Bug Fixes
7+
1. [#457](https://github.com/influxdata/influxdb-client-python/pull/457): Formatting nanoseconds to Flux AST
8+
69
### Dependencies
710
1. [#449](https://github.com/influxdata/influxdb-client-python/pull/449): Remove `pytz` library
811

influxdb_client/client/_base.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,9 @@ def _parm_to_extern_ast(value) -> Union[Expression, None]:
341341
return FloatLiteral("FloatLiteral", value)
342342
elif isinstance(value, datetime):
343343
value = get_date_helper().to_utc(value)
344-
return DateTimeLiteral("DateTimeLiteral", value.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
344+
nanoseconds = getattr(value, 'nanosecond', 0)
345+
fraction = f'{(value.microsecond * 1000 + nanoseconds):09d}'
346+
return DateTimeLiteral("DateTimeLiteral", value.strftime('%Y-%m-%dT%H:%M:%S.') + fraction + 'Z')
345347
elif isinstance(value, timedelta):
346348
_micro_delta = int(value / timedelta(microseconds=1))
347349
if _micro_delta < 0:

tests/test_QueryApi.py

+18-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def test_parameter_ast(self):
132132
},
133133
"init": {
134134
"type": "DateTimeLiteral",
135-
"value": "2021-03-20T15:59:10.607352Z"
135+
"value": "2021-03-20T15:59:10.607352000Z"
136136
},
137137
"type": "VariableAssignment"
138138
},
@@ -150,7 +150,7 @@ def test_parameter_ast(self):
150150
},
151151
"init": {
152152
"type": "DateTimeLiteral",
153-
"value": "2021-03-20T15:59:10.607352Z"
153+
"value": "2021-03-20T15:59:10.607352000Z"
154154
},
155155
"type": "VariableAssignment"
156156
},
@@ -514,6 +514,22 @@ def test_profiler_mock(self):
514514
'available': 5727718400, 'free': 35330048,
515515
'used': 11452150784})
516516

517+
def test_time_to_ast(self):
518+
from influxdb_client.extras import pd
519+
import dateutil.parser
520+
521+
literals = [
522+
(pd.Timestamp('1996-02-25T21:20:00.001001231Z'), '1996-02-25T21:20:00.001001231Z'),
523+
(dateutil.parser.parse('1996-02-25T21:20:00.001001231Z'), '1996-02-25T21:20:00.001001000Z'),
524+
(dateutil.parser.parse('1996-02-25'), '1996-02-25T00:00:00.000000000Z'),
525+
(datetime.datetime(2021, 5, 24, 8, 40, 44, 785000, tzinfo=timezone.utc), '2021-05-24T08:40:44.785000000Z'),
526+
]
527+
528+
for literal in literals:
529+
ast = QueryApi._build_flux_ast({'date': literal[0]})
530+
self.assertEqual('DateTimeLiteral', ast.body[0].assignment.init.type)
531+
self.assertEqual(literal[1], ast.body[0].assignment.init.value)
532+
517533

518534
if __name__ == '__main__':
519535
unittest.main()

0 commit comments

Comments
 (0)