Skip to content

Commit 2a4260c

Browse files
committed
feat: add datetime to string utils for lists
1 parent 28df1f8 commit 2a4260c

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

ibm_cloud_sdk_core/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
functions:
2626
datetime_to_string: Serializes a datetime to a string.
2727
string_to_datetime: De-serializes a string to a datetime.
28+
datetime_to_string_list: Serializes a list of datetimes to a list of strings.
29+
string_to_datetime_list: De-serializes a list of strings to a list of datetimes.
2830
date_to_string: Serializes a date to a string.
2931
string_to_date: De-serializes a string to a date.
3032
convert_model: Convert a model object into an equivalent dict.
@@ -41,6 +43,7 @@
4143
from .cp4d_token_manager import CP4DTokenManager
4244
from .api_exception import ApiException
4345
from .utils import datetime_to_string, string_to_datetime, read_external_sources
46+
from .utils import datetime_to_string_list, string_to_datetime_list
4447
from .utils import date_to_string, string_to_date
4548
from .utils import convert_model, convert_list
4649
from .utils import get_query_param

ibm_cloud_sdk_core/utils.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,40 @@ def string_to_datetime(string: str) -> datetime.datetime:
117117
return val.replace(tzinfo=datetime.timezone.utc)
118118

119119

120+
def string_to_datetime_list(string_list: List[str]) -> List[datetime.datetime]:
121+
"""De-serializes each string in a list to a datetime.
122+
123+
Args:
124+
string_list: list of strings containing datetime in iso8601 format.
125+
126+
Returns:
127+
the de-serialized list of strings as a list of datetime objects.
128+
"""
129+
if not isinstance(string_list, list):
130+
raise ValueError("Invalid argument type: " + str(type(string_list))
131+
+ ". Argument string_list must be of type List[str]")
132+
datetime_list = []
133+
for string_val in string_list:
134+
datetime_list.append(string_to_datetime(string_val))
135+
return datetime_list
136+
137+
def datetime_to_string_list(datetime_list: List[datetime.datetime]) -> List[str]:
138+
"""Convert a list of datetime objects to a list of strings.
139+
140+
Args:
141+
datetime_list: The list of datetime objects.
142+
143+
Returns:
144+
list of datetimes serialized as strings in iso8601 format.
145+
"""
146+
if not isinstance(datetime_list, list):
147+
raise ValueError("Invalid argument type: " + str(type(datetime_list))
148+
+ ". Argument datetime_list must be of type List[datetime.datetime]")
149+
string_list = []
150+
for datetime_val in datetime_list:
151+
string_list.append(datetime_to_string(datetime_val))
152+
return string_list
153+
120154
def date_to_string(val: datetime.date) -> str:
121155
"""Convert a date object to string.
122156

test/test_utils.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import pytest
2222

2323
from ibm_cloud_sdk_core import string_to_datetime, datetime_to_string, get_authenticator_from_environment
24+
from ibm_cloud_sdk_core import string_to_datetime_list, datetime_to_string_list
2425
from ibm_cloud_sdk_core import string_to_date, date_to_string
2526
from ibm_cloud_sdk_core import convert_model, convert_list
2627
from ibm_cloud_sdk_core import get_query_param
@@ -116,6 +117,63 @@ def test_datetime_to_string():
116117
res = datetime_to_string(date)
117118
assert res == '2017-03-06T16:00:04.159338Z'
118119

120+
def test_string_to_datetime_list():
121+
# Assert ValueError is raised for invalid argument type
122+
with pytest.raises(ValueError):
123+
string_to_datetime_list(None)
124+
# If the specified string does not include a timezone, it is assumed to be UTC
125+
date_list = string_to_datetime_list([ '2017-03-06 16:00:04.159338' ])
126+
assert date_list[0].day == 6
127+
assert date_list[0].hour == 16
128+
assert date_list[0].tzinfo.utcoffset(None) == datetime.timezone.utc.utcoffset(None)
129+
# Test date string with TZ specified as '+xxxx'
130+
date_list = string_to_datetime_list([ '2017-03-06 16:00:04.159338+0600' ])
131+
assert date_list[0].day == 6
132+
assert date_list[0].hour == 16
133+
assert date_list[0].tzinfo.utcoffset(None).total_seconds() == 6 * 60 * 60
134+
# Test date string with TZ specified as 'Z'
135+
date_list = string_to_datetime_list([ '2017-03-06 16:00:04.159338Z' ])
136+
assert date_list[0].day == 6
137+
assert date_list[0].hour == 16
138+
assert date_list[0].tzinfo.utcoffset(None) == datetime.timezone.utc.utcoffset(None)
139+
# Test multiple datetimes in a list
140+
date_list = string_to_datetime_list([ '2017-03-06 16:00:04.159338', '2017-03-07 17:00:04.159338' ])
141+
assert date_list[0].day == 6
142+
assert date_list[0].hour == 16
143+
assert date_list[0].tzinfo.utcoffset(None) == datetime.timezone.utc.utcoffset(None)
144+
assert date_list[1].day == 7
145+
assert date_list[1].hour == 17
146+
assert date_list[1].tzinfo.utcoffset(None) == datetime.timezone.utc.utcoffset(None)
147+
148+
149+
def test_datetime_to_string_list():
150+
# Assert ValueError is raised for invalid argument type
151+
with pytest.raises(ValueError):
152+
datetime_to_string_list(None)
153+
# If specified datetime list item is None, return list of None
154+
assert datetime_to_string_list([None]) == [None]
155+
# If specified datetime list is empty, return empty list
156+
assert datetime_to_string_list([]) == []
157+
# If the specified date list item is "naive", it is interpreted as a UTC date
158+
date_list = [ datetime.datetime(2017, 3, 6, 16, 0, 4, 159338) ]
159+
res = datetime_to_string_list(date_list)
160+
assert res == [ '2017-03-06T16:00:04.159338Z' ]
161+
# Test date list item with UTC timezone
162+
date_list = [ datetime.datetime(2017, 3, 6, 16, 0, 4, 159338,
163+
datetime.timezone.utc) ]
164+
res = datetime_to_string_list(date_list)
165+
assert res == [ '2017-03-06T16:00:04.159338Z' ]
166+
# Test date list item with non-UTC timezone
167+
tzn = datetime.timezone(datetime.timedelta(hours=-6))
168+
date_list = [ datetime.datetime(2017, 3, 6, 10, 0, 4, 159338, tzn) ]
169+
res = datetime_to_string_list(date_list)
170+
assert res == [ '2017-03-06T16:00:04.159338Z' ]
171+
# Test specified date list with multiple items
172+
date_list = [ datetime.datetime(2017, 3, 6, 16, 0, 4, 159338),
173+
datetime.datetime(2017, 3, 6, 16, 0, 4, 159338,
174+
datetime.timezone.utc) ]
175+
res = datetime_to_string_list(date_list)
176+
assert res == [ '2017-03-06T16:00:04.159338Z', '2017-03-06T16:00:04.159338Z' ]
119177

120178
def test_date_conversion():
121179
date = string_to_date('2017-03-06')

0 commit comments

Comments
 (0)