Skip to content

Handle conversions for naive datetime values #56

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 13, 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
11 changes: 10 additions & 1 deletion ibm_cloud_sdk_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,19 @@ def cleanup_value(value: any) -> any:
def datetime_to_string(val: datetime.datetime) -> str:
"""Convert a datetime object to string.

If the supplied datetime does not specify a timezone,
it is assumed to be UTC.

Args:
val: The datetime object.

Returns:
datetime serialized to iso8601 format.
"""
if isinstance(val, datetime.datetime):
if val.tzinfo is None:
return val.isoformat() + 'Z'
val = val.astimezone(datetime.timezone.utc)
return val.isoformat().replace('+00:00', 'Z')
return val

Expand All @@ -90,7 +96,10 @@ def string_to_datetime(string: str) -> datetime.datetime:
Returns:
the de-serialized string as a datetime object.
"""
return date_parser.parse(string)
val = date_parser.parse(string)
if val.tzinfo is not None:
return val
return val.replace(tzinfo=datetime.timezone.utc)

def date_to_string(val: datetime.date) -> str:
"""Convert a date object to string.
Expand Down
34 changes: 31 additions & 3 deletions test/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,45 @@
# pylint: disable=missing-docstring
import datetime
import os

from ibm_cloud_sdk_core import string_to_datetime, datetime_to_string, get_authenticator_from_environment
from ibm_cloud_sdk_core import string_to_date, date_to_string
from ibm_cloud_sdk_core import convert_model, convert_list
from ibm_cloud_sdk_core.authenticators import BasicAuthenticator, IAMAuthenticator

def test_datetime_conversion():
def test_string_to_datetime():
# If the specified string does not include a timezone, it is assumed to be UTC
date = string_to_datetime('2017-03-06 16:00:04.159338')
assert date.day == 6
res = datetime_to_string(date)
assert res == '2017-03-06T16:00:04.159338'
assert date.hour == 16
assert date.tzinfo.utcoffset(None) == datetime.timezone.utc.utcoffset(None)
# Test date string with TZ specified as '+xxxx'
date = string_to_datetime('2017-03-06 16:00:04.159338+0600')
assert date.day == 6
assert date.hour == 16
assert date.tzinfo.utcoffset(None).total_seconds() == 6*60*60
# Test date string with TZ specified as 'Z'
date = string_to_datetime('2017-03-06 16:00:04.159338Z')
assert date.day == 6
assert date.hour == 16
assert date.tzinfo.utcoffset(None) == datetime.timezone.utc.utcoffset(None)

def test_datetime_to_string():
# If specified date is None, return None
assert datetime_to_string(None) is None
# If the specified date is "naive", it is interpreted as a UTC date
date = datetime.datetime(2017, 3, 6, 16, 0, 4, 159338)
res = datetime_to_string(date)
assert res == '2017-03-06T16:00:04.159338Z'
# Test date with UTC timezone
date = datetime.datetime(2017, 3, 6, 16, 0, 4, 159338, datetime.timezone.utc)
res = datetime_to_string(date)
assert res == '2017-03-06T16:00:04.159338Z'
# Test date with non-UTC timezone
tzn = datetime.timezone(datetime.timedelta(hours=-6))
date = datetime.datetime(2017, 3, 6, 10, 0, 4, 159338, tzn)
res = datetime_to_string(date)
assert res == '2017-03-06T16:00:04.159338Z'

def test_date_conversion():
date = string_to_date('2017-03-06')
Expand Down