File tree Expand file tree Collapse file tree 3 files changed +32
-0
lines changed Expand file tree Collapse file tree 3 files changed +32
-0
lines changed Original file line number Diff line number Diff line change 25
25
functions:
26
26
datetime_to_string: Serializes a datetime to a string.
27
27
string_to_datetime: De-serializes a string to a datetime.
28
+ date_to_string: Serializes a date to a string.
29
+ string_to_date: De-serializes a string to a date.
28
30
convert_model: Convert a model object into an equivalent dict.
29
31
convert_list: Convert a list of strings into comma-separated string.
30
32
read_external_sources: Get config object from external sources.
38
40
from .cp4d_token_manager import CP4DTokenManager
39
41
from .api_exception import ApiException
40
42
from .utils import datetime_to_string , string_to_datetime , read_external_sources
43
+ from .utils import date_to_string , string_to_date
41
44
from .utils import convert_model , convert_list
42
45
from .get_authenticator import get_authenticator_from_environment
Original file line number Diff line number Diff line change @@ -90,6 +90,28 @@ def string_to_datetime(string: str) -> datetime.datetime:
90
90
"""
91
91
return date_parser .parse (string )
92
92
93
+ def date_to_string (val : datetime .date ) -> str :
94
+ """Convert a date object to string.
95
+
96
+ Args:
97
+ val: The date object.
98
+
99
+ Returns:
100
+ date serialized to `YYYY-MM-DD` format.
101
+ """
102
+ return str (val )
103
+
104
+ def string_to_date (string : str ) -> datetime .date :
105
+ """De-serializes string to date.
106
+
107
+ Args:
108
+ string: string containing date in 'YYYY-MM-DD' format.
109
+
110
+ Returns:
111
+ the de-serialized string as a date object.
112
+ """
113
+ return date_parser .parse (string ).date ()
114
+
93
115
def convert_model (val : any ) -> dict :
94
116
"""Convert a model object into an equivalent dict.
95
117
Original file line number Diff line number Diff line change 2
2
import os
3
3
4
4
from ibm_cloud_sdk_core import string_to_datetime , datetime_to_string , get_authenticator_from_environment
5
+ from ibm_cloud_sdk_core import string_to_date , date_to_string
5
6
from ibm_cloud_sdk_core import convert_model , convert_list
6
7
from ibm_cloud_sdk_core .authenticators import BasicAuthenticator , IAMAuthenticator
7
8
@@ -11,6 +12,12 @@ def test_datetime_conversion():
11
12
res = datetime_to_string (date )
12
13
assert res == '2017-03-06T16:00:04.159338'
13
14
15
+ def test_date_conversion ():
16
+ date = string_to_date ('2017-03-06' )
17
+ assert date .day == 6
18
+ res = date_to_string (date )
19
+ assert res == '2017-03-06'
20
+
14
21
def test_convert_model ():
15
22
16
23
class MockModel :
You can’t perform that action at this time.
0 commit comments