Skip to content

Commit

Permalink
fix: Fix date/datetime_to_string handling of non-date/datetime inputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Kistler committed Feb 1, 2020
1 parent 26c478c commit 8251b82
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 6 additions & 2 deletions ibm_cloud_sdk_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ def datetime_to_string(val: datetime.datetime) -> str:
Returns:
datetime serialized to iso8601 format.
"""
return val.isoformat().replace('+00:00', 'Z')
if isinstance(val, datetime.datetime):
return val.isoformat().replace('+00:00', 'Z')
return val

def string_to_datetime(string: str) -> datetime.datetime:
"""De-serializes string to datetime.
Expand All @@ -99,7 +101,9 @@ def date_to_string(val: datetime.date) -> str:
Returns:
date serialized to `YYYY-MM-DD` format.
"""
return str(val)
if isinstance(val, datetime.date):
return str(val)
return val

def string_to_date(string: str) -> datetime.date:
"""De-serializes string to date.
Expand Down
2 changes: 2 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ def test_datetime_conversion():
assert date.day == 6
res = datetime_to_string(date)
assert res == '2017-03-06T16:00:04.159338'
assert datetime_to_string(None) is None

def test_date_conversion():
date = string_to_date('2017-03-06')
assert date.day == 6
res = date_to_string(date)
assert res == '2017-03-06'
assert date_to_string(None) is None

def test_convert_model():

Expand Down

0 comments on commit 8251b82

Please sign in to comment.