From 7d2953c7fb83554647836766e565ef7f3a7b3bd9 Mon Sep 17 00:00:00 2001 From: James Ridgway Date: Sat, 5 Mar 2022 16:48:21 +0000 Subject: [PATCH] Improvements to parsing datetime values --- .github/workflows/ci.yml | 2 +- attachment_downloader/cli.py | 4 +++- requirements.txt | 1 + tests/attachment_downloader/test_cli.py | 12 ++++++++++-- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 256776d..d6718a3 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -8,7 +8,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - python-version: [3.7, 3.8] + python-version: [3.6, 3.7, 3.8] steps: - uses: actions/checkout@v2 - name: Set up Python ${{ matrix.python-version }} diff --git a/attachment_downloader/cli.py b/attachment_downloader/cli.py index 8caac2f..9436dba 100644 --- a/attachment_downloader/cli.py +++ b/attachment_downloader/cli.py @@ -2,10 +2,12 @@ import sys from getpass import getpass +from iso8601 import iso8601 + def valid_date(_, opt, value): try: - parsed_value = datetime.datetime.fromisoformat(value) + parsed_value = iso8601.parse_date(value) if not parsed_value.tzinfo: parsed_value = parsed_value.replace(tzinfo=datetime.timezone.utc) return parsed_value diff --git a/requirements.txt b/requirements.txt index 47e8962..cff3007 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,6 @@ assertpy==1.1 imbox==0.9.8 +iso8601==1.0.2 jinja2==2.11.3 python-dateutil==2.8.2 pylint==2.12.2 diff --git a/tests/attachment_downloader/test_cli.py b/tests/attachment_downloader/test_cli.py index cb98635..7de6a2f 100644 --- a/tests/attachment_downloader/test_cli.py +++ b/tests/attachment_downloader/test_cli.py @@ -8,9 +8,17 @@ class TestCli: def test_valid_date(self): - dt = pytz.utc.localize(datetime.datetime(2022, 3, 3, 13, 0, 0, 0)) assert_that(valid_date(None, '--date-after', '2022-03-03T13:00:00'))\ - .is_equal_to(dt) + .is_equal_to(pytz.utc.localize(datetime.datetime(2022, 3, 3, 13, 0, 0, 0))) + + assert_that(valid_date(None, '--date-after', '2022-03-03T13:00:00Z')) \ + .is_equal_to(pytz.utc.localize(datetime.datetime(2022, 3, 3, 13, 0, 0, 0))) + + assert_that(valid_date(None, '--date-after', '2022-03-03T13:00:00+00:00')) \ + .is_equal_to(pytz.utc.localize(datetime.datetime(2022, 3, 3, 13, 0, 0, 0))) + + assert_that(valid_date(None, '--date-after', '2022-03-03T13:00:00.123456+00:00')) \ + .is_equal_to(pytz.utc.localize(datetime.datetime(2022, 3, 3, 13, 0, 0, 123456))) def test_valid_date_exception_if_invalid(self): assert_that(valid_date)\