Skip to content

Commit

Permalink
feat: use build-in Python parsing feature for python >= 3.11 (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
bednar authored Jun 12, 2024
1 parent c580008 commit 18cec22
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ commands:
pip install . --user
pip install .\[dataframe\] --user
pip install .\[test\] --user
pytest -m "<< parameters.pytest-marker >>" tests --junitxml=test-reports/junit.xml --cov=./ --cov-report xml:coverage.xml
pytest -m "<< parameters.pytest-marker >>" tests --junitxml=test-reports/junit.xml --cov=./influxdb_client_3 --cov-report xml:coverage.xml
- save_cache:
name: Saving Pip Cache
key: *cache-key
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

### Features

1. [#89](https://github.com/InfluxCommunity/influxdb3-python/pull/89): Use `datetime.fromisoformat` over `dateutil.parse` in Python 3.11+
1. [#92](https://github.com/InfluxCommunity/influxdb3-python/pull/92): Update `user-agent` header value to `influxdb3-python/{VERSION}` and add it to queries as well.

## 0.5.0 [2024-05-17]
Expand Down
6 changes: 5 additions & 1 deletion influxdb_client_3/write_client/client/util/date_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import datetime
import threading
from datetime import timezone as tz
from sys import version_info

from dateutil import parser

Expand Down Expand Up @@ -90,7 +91,10 @@ def get_date_helper() -> DateHelper:
import ciso8601
_date_helper.parse_date = ciso8601.parse_datetime
except ModuleNotFoundError:
_date_helper.parse_date = parser.parse
if (version_info.major, version_info.minor) >= (3, 11):
_date_helper.parse_date = datetime.datetime.fromisoformat
else:
_date_helper.parse_date = parser.parse
date_helper = _date_helper

return date_helper
21 changes: 21 additions & 0 deletions tests/test_date_helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import unittest
from datetime import datetime, timezone

from dateutil import tz

from influxdb_client_3.write_client.client.util.date_utils import DateHelper, get_date_helper


class TestDateHelper(unittest.TestCase):

def test_to_utc(self):
date = get_date_helper().to_utc(datetime(2021, 4, 29, 20, 30, 10, 0))
self.assertEqual(datetime(2021, 4, 29, 20, 30, 10, 0, timezone.utc), date)

def test_to_utc_different_timezone(self):
date = DateHelper(timezone=tz.gettz('ETC/GMT+2')).to_utc(datetime(2021, 4, 29, 20, 30, 10, 0))
self.assertEqual(datetime(2021, 4, 29, 22, 30, 10, 0, timezone.utc), date)

def test_parse(self):
date = get_date_helper().parse_date("2021-03-20T15:59:10.607352Z")
self.assertEqual(datetime(2021, 3, 20, 15, 59, 10, 607352, timezone.utc), date)
2 changes: 1 addition & 1 deletion tests/test_merge_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import influxdb_client_3


class MergeOptionsTests(unittest.TestCase):
class TestMergeOptions(unittest.TestCase):

def test_merge_with_empty_custom(self):
defaults = {"a": 1, "b": 2}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ def test_influx_default_query_headers():
_req_headers = {}


class QueryTests(unittest.TestCase):
class TestQuery(unittest.TestCase):

@patch('influxdb_client_3._InfluxDBClient')
@patch('influxdb_client_3._WriteApi')
Expand Down

0 comments on commit 18cec22

Please sign in to comment.