Skip to content

fix: Fix date/datetime_to_string handling of non-date/datetime inputs #55

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 4, 2020
Merged
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
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