Skip to content

fix: formatting nanoseconds to Flux AST #457

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
### Features
1. [#440](https://github.com/influxdata/influxdb-client-python/pull/440): Add possibility to specify timestamp column and its timezone [DataFrame]

### Bug Fixes
1. [#457](https://github.com/influxdata/influxdb-client-python/pull/457): Formatting nanoseconds to Flux AST

### Dependencies
1. [#449](https://github.com/influxdata/influxdb-client-python/pull/449): Remove `pytz` library

Expand Down
4 changes: 3 additions & 1 deletion influxdb_client/client/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,9 @@ def _parm_to_extern_ast(value) -> Union[Expression, None]:
return FloatLiteral("FloatLiteral", value)
elif isinstance(value, datetime):
value = get_date_helper().to_utc(value)
return DateTimeLiteral("DateTimeLiteral", value.strftime('%Y-%m-%dT%H:%M:%S.%fZ'))
nanoseconds = getattr(value, 'nanosecond', 0)
fraction = f'{(value.microsecond * 1000 + nanoseconds):09d}'
return DateTimeLiteral("DateTimeLiteral", value.strftime('%Y-%m-%dT%H:%M:%S.') + fraction + 'Z')
elif isinstance(value, timedelta):
_micro_delta = int(value / timedelta(microseconds=1))
if _micro_delta < 0:
Expand Down
20 changes: 18 additions & 2 deletions tests/test_QueryApi.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def test_parameter_ast(self):
},
"init": {
"type": "DateTimeLiteral",
"value": "2021-03-20T15:59:10.607352Z"
"value": "2021-03-20T15:59:10.607352000Z"
},
"type": "VariableAssignment"
},
Expand All @@ -150,7 +150,7 @@ def test_parameter_ast(self):
},
"init": {
"type": "DateTimeLiteral",
"value": "2021-03-20T15:59:10.607352Z"
"value": "2021-03-20T15:59:10.607352000Z"
},
"type": "VariableAssignment"
},
Expand Down Expand Up @@ -514,6 +514,22 @@ def test_profiler_mock(self):
'available': 5727718400, 'free': 35330048,
'used': 11452150784})

def test_time_to_ast(self):
from influxdb_client.extras import pd
import dateutil.parser

literals = [
(pd.Timestamp('1996-02-25T21:20:00.001001231Z'), '1996-02-25T21:20:00.001001231Z'),
(dateutil.parser.parse('1996-02-25T21:20:00.001001231Z'), '1996-02-25T21:20:00.001001000Z'),
(dateutil.parser.parse('1996-02-25'), '1996-02-25T00:00:00.000000000Z'),
(datetime.datetime(2021, 5, 24, 8, 40, 44, 785000, tzinfo=timezone.utc), '2021-05-24T08:40:44.785000000Z'),
]

for literal in literals:
ast = QueryApi._build_flux_ast({'date': literal[0]})
self.assertEqual('DateTimeLiteral', ast.body[0].assignment.init.type)
self.assertEqual(literal[1], ast.body[0].assignment.init.value)


if __name__ == '__main__':
unittest.main()
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