Skip to content

Commit

Permalink
Merge pull request #31 from jamesridgway/james/datetime-parse-improve…
Browse files Browse the repository at this point in the history
…ments

Improvements to parsing datetime values
  • Loading branch information
jamesridgway authored Mar 5, 2022
2 parents 2611fa8 + 7d2953c commit 0d6810c
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
4 changes: 3 additions & 1 deletion attachment_downloader/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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
Expand Down
12 changes: 10 additions & 2 deletions tests/attachment_downloader/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)\
Expand Down

0 comments on commit 0d6810c

Please sign in to comment.