Skip to content
Open
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
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ Common
(#2229)
[0z5a - @0z5a]

- [Utils] Fix ``parse_date`` raising ``TypeError`` for valid ISO 8601 strings
without a time or without seconds, such as ``2020-01-01`` or
``2020-01-01T10:20Z``. Missing time fields now default to zero (and a
missing month or day to 1), as in upstream pyiso8601.
(#2245)
[@aad1tyaaaaa]

Compute
~~~~~~~

Expand Down
14 changes: 14 additions & 0 deletions libcloud/test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import platform
import unittest
import warnings
from datetime import datetime, timezone
from itertools import chain

import pytest
Expand All @@ -30,6 +31,7 @@
from libcloud.utils.misc import get_driver, set_driver, get_secure_random_string
from libcloud.common.types import LibcloudError
from libcloud.compute.types import Provider
from libcloud.utils.iso8601 import parse_date
from libcloud.utils.publickey import (
get_pubkey_comment,
get_pubkey_ssh2_fingerprint,
Expand Down Expand Up @@ -538,5 +540,17 @@ def foo():
foo()


@pytest.mark.parametrize(
"datestring, expected",
[
("2020-01-01", datetime(2020, 1, 1, tzinfo=timezone.utc)),
("2020-01-01T10:20Z", datetime(2020, 1, 1, 10, 20, tzinfo=timezone.utc)),
("2020-01-01T10:20+02:00", datetime(2020, 1, 1, 8, 20, tzinfo=timezone.utc)),
],
)
def test_parse_date_without_time_or_seconds(datestring, expected):
assert parse_date(datestring) == expected


if __name__ == "__main__":
sys.exit(unittest.main())
12 changes: 7 additions & 5 deletions libcloud/utils/iso8601.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,11 +131,13 @@ def parse_date(datestring, default_timezone=UTC, allow_empty=False):
groups["fraction"] = int(float("0.%s" % groups["fraction"]) * 1e6)
return datetime(
int(groups["year"]),
int(groups["month"]),
int(groups["day"]),
int(groups["hour"]),
int(groups["minute"]),
int(groups["second"]),
# Everything after the year is optional in the regex, so default the
# missing parts like upstream pyiso8601 does (e.g. "2020-01-01")
int(groups["month"] or 1),
int(groups["day"] or 1),
int(groups["hour"] or 0),
int(groups["minute"] or 0),
int(groups["second"] or 0),
int(groups["fraction"]),
tz,
)
Expand Down