diff --git a/ibm_cloud_sdk_core/utils.py b/ibm_cloud_sdk_core/utils.py index 4e838e7..5994804 100644 --- a/ibm_cloud_sdk_core/utils.py +++ b/ibm_cloud_sdk_core/utils.py @@ -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. @@ -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. diff --git a/test/test_utils.py b/test/test_utils.py index 9651230..e279570 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -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():