Skip to content

feat: Add date_to_string and string_to_date utility methods #52

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
Dec 19, 2019
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
3 changes: 3 additions & 0 deletions ibm_cloud_sdk_core/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
functions:
datetime_to_string: Serializes a datetime to a string.
string_to_datetime: De-serializes a string to a datetime.
date_to_string: Serializes a date to a string.
string_to_date: De-serializes a string to a date.
convert_model: Convert a model object into an equivalent dict.
convert_list: Convert a list of strings into comma-separated string.
read_external_sources: Get config object from external sources.
Expand All @@ -38,5 +40,6 @@
from .cp4d_token_manager import CP4DTokenManager
from .api_exception import ApiException
from .utils import datetime_to_string, string_to_datetime, read_external_sources
from .utils import date_to_string, string_to_date
from .utils import convert_model, convert_list
from .get_authenticator import get_authenticator_from_environment
22 changes: 22 additions & 0 deletions ibm_cloud_sdk_core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,28 @@ def string_to_datetime(string: str) -> datetime.datetime:
"""
return date_parser.parse(string)

def date_to_string(val: datetime.date) -> str:
"""Convert a date object to string.

Args:
val: The date object.

Returns:
date serialized to `YYYY-MM-DD` format.
"""
return str(val)

def string_to_date(string: str) -> datetime.date:
"""De-serializes string to date.

Args:
string: string containing date in 'YYYY-MM-DD' format.

Returns:
the de-serialized string as a date object.
"""
return date_parser.parse(string).date()

def convert_model(val: any) -> dict:
"""Convert a model object into an equivalent dict.

Expand Down
7 changes: 7 additions & 0 deletions test/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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

Expand All @@ -11,6 +12,12 @@ def test_datetime_conversion():
res = datetime_to_string(date)
assert res == '2017-03-06T16:00:04.159338'

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'

def test_convert_model():

class MockModel:
Expand Down