|
1 | 1 | # pylint: disable=missing-docstring
|
| 2 | +import datetime |
2 | 3 | import os
|
3 | 4 |
|
4 | 5 | from ibm_cloud_sdk_core import string_to_datetime, datetime_to_string, get_authenticator_from_environment
|
5 | 6 | from ibm_cloud_sdk_core import string_to_date, date_to_string
|
6 | 7 | from ibm_cloud_sdk_core import convert_model, convert_list
|
7 | 8 | from ibm_cloud_sdk_core.authenticators import BasicAuthenticator, IAMAuthenticator
|
8 | 9 |
|
9 |
| -def test_datetime_conversion(): |
| 10 | +def test_string_to_datetime(): |
| 11 | + # If the specified string does not include a timezone, it is assumed to be UTC |
10 | 12 | date = string_to_datetime('2017-03-06 16:00:04.159338')
|
11 | 13 | assert date.day == 6
|
12 |
| - res = datetime_to_string(date) |
13 |
| - assert res == '2017-03-06T16:00:04.159338' |
| 14 | + assert date.hour == 16 |
| 15 | + assert date.tzinfo.utcoffset(None) == datetime.timezone.utc.utcoffset(None) |
| 16 | + # Test date string with TZ specified as '+xxxx' |
| 17 | + date = string_to_datetime('2017-03-06 16:00:04.159338+0600') |
| 18 | + assert date.day == 6 |
| 19 | + assert date.hour == 16 |
| 20 | + assert date.tzinfo.utcoffset(None).total_seconds() == 6*60*60 |
| 21 | + # Test date string with TZ specified as 'Z' |
| 22 | + date = string_to_datetime('2017-03-06 16:00:04.159338Z') |
| 23 | + assert date.day == 6 |
| 24 | + assert date.hour == 16 |
| 25 | + assert date.tzinfo.utcoffset(None) == datetime.timezone.utc.utcoffset(None) |
| 26 | + |
| 27 | +def test_datetime_to_string(): |
| 28 | + # If specified date is None, return None |
14 | 29 | assert datetime_to_string(None) is None
|
| 30 | + # If the specified date is "naive", it is interpreted as a UTC date |
| 31 | + date = datetime.datetime(2017, 3, 6, 16, 0, 4, 159338) |
| 32 | + res = datetime_to_string(date) |
| 33 | + assert res == '2017-03-06T16:00:04.159338Z' |
| 34 | + # Test date with UTC timezone |
| 35 | + date = datetime.datetime(2017, 3, 6, 16, 0, 4, 159338, datetime.timezone.utc) |
| 36 | + res = datetime_to_string(date) |
| 37 | + assert res == '2017-03-06T16:00:04.159338Z' |
| 38 | + # Test date with non-UTC timezone |
| 39 | + tzn = datetime.timezone(datetime.timedelta(hours=-6)) |
| 40 | + date = datetime.datetime(2017, 3, 6, 10, 0, 4, 159338, tzn) |
| 41 | + res = datetime_to_string(date) |
| 42 | + assert res == '2017-03-06T16:00:04.159338Z' |
15 | 43 |
|
16 | 44 | def test_date_conversion():
|
17 | 45 | date = string_to_date('2017-03-06')
|
|
0 commit comments