Skip to content

Commit ccaf3be

Browse files
committed
* Improve unit testing of cylc/flow/wallclock.py
* Modernise docstring.
1 parent d13212a commit ccaf3be

File tree

2 files changed

+104
-24
lines changed

2 files changed

+104
-24
lines changed

cylc/flow/wallclock.py

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
from calendar import timegm
1919
from datetime import datetime, timedelta, timezone
20+
from typing import Dict, Optional
2021

2122
from metomi.isodatetime.timezone import (
2223
get_local_time_zone_format, get_local_time_zone, TimeZoneFormatMode)
@@ -108,31 +109,45 @@ def get_current_time_string(display_sub_seconds=False, override_use_utc=None,
108109
use_basic_format=use_basic_format)
109110

110111

111-
def get_time_string(date_time, display_sub_seconds=False,
112-
override_use_utc=None, use_basic_format=False,
113-
date_time_is_local=False, custom_time_zone_info=None):
112+
def get_time_string(
113+
date_time: datetime,
114+
display_sub_seconds: bool = False,
115+
override_use_utc: Optional[bool] = None,
116+
use_basic_format: bool = False,
117+
date_time_is_local: bool = False,
118+
custom_time_zone_info: Optional[Dict] = None,
119+
):
114120
"""Return a string representing the current system time.
115121
116-
Arguments:
117-
date_time - a datetime.datetime object.
118-
119-
Keyword arguments:
120-
display_sub_seconds (default False) - a boolean that, if True,
121-
switches on microsecond reporting
122-
override_use_utc (default None) - a boolean (or None) that, if
123-
True, switches on utc time zone reporting. If False, it switches
124-
off utc time zone reporting (even if _FLAGS['utc_mode'] is True). If None,
125-
the _FLAGS['utc_mode'] boolean is used.
126-
use_basic_format (default False) - a boolean that, if True,
127-
represents the date/time without "-" or ":" delimiters. This is
128-
most useful for filenames where ":" may cause problems.
129-
date_time_is_local - a boolean that, if True, indicates that
130-
the date_time argument object is in the local time zone, not UTC.
131-
custom_time_zone_info (default None) - a dictionary that enforces
132-
a particular time zone. It looks like {"hours": _hours,
133-
"minutes": _minutes, "string": _string} where _hours and _minutes
134-
are the hours and minutes offset from UTC and _string is the string
135-
to use as the time zone designator.
122+
Args:
123+
date_time: Datetime to operate on.
124+
display_sub_seconds:
125+
Switch on microsecond reporting.
126+
override_use_utc:
127+
Switch on utc time zone reporting.
128+
If False, it switches off utc time zone reporting even
129+
if ``_FLAGS['utc_mode']`` is True).
130+
If None, the ``_FLAGS['utc_mode']`` boolean is used.
131+
use_basic_format:
132+
Represent the date/time without "-" or ":"
133+
delimiters. This is useful for filenames, where ":" may
134+
cause problems.
135+
date_time_is_local:
136+
Indicates that the date_time argument
137+
object is in the local time zone, not UTC.
138+
custom_time_zone_info:
139+
A dictionary that enforces a particular time zone:
140+
141+
.. code-block:: python
142+
{
143+
"hours": _hours, # offset from UTC
144+
"minutes": _minutes, # offset from utc
145+
"string_basic": _string, # timezone designators
146+
"string_extened": _string
147+
}
148+
149+
Usage of ``string_basic`` or ``string_extended`` is
150+
switched by ``use_basic_format``.
136151
137152
"""
138153
time_zone_string = None

tests/unit/test_wallclock.py

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
# You should have received a copy of the GNU General Public License
1515
# along with this program. If not, see <http://www.gnu.org/licenses/>.
1616

17+
from datetime import datetime
1718
import pytest
19+
from pytest import param
1820

1921
from metomi.isodatetime.data import CALENDAR
2022
from cylc.flow.wallclock import (
21-
get_unix_time_from_time_string,
2223
get_current_time_string,
24+
get_time_string,
25+
get_unix_time_from_time_string,
2326
)
2427

2528

@@ -73,3 +76,65 @@ def test_get_current_time_string(set_timezone):
7376
set_timezone()
7477
res = get_current_time_string()
7578
assert res[-6:] == '+19:17'
79+
80+
81+
@pytest.mark.parametrize(
82+
'arg, kwargs, expect',
83+
(
84+
param(
85+
datetime(2000, 12, 13, 15, 30, 12, 123456),
86+
{},
87+
'2000-12-14T10:47:12+19:17',
88+
id='good',
89+
),
90+
param(
91+
datetime(2000, 12, 13, 15, 30, 12, 123456),
92+
{'date_time_is_local': True},
93+
'2000-12-13T15:30:12+19:17',
94+
id='dt_is_local',
95+
),
96+
param(
97+
datetime(2000, 12, 13, 15, 30, 12, 123456),
98+
{
99+
'custom_time_zone_info': {
100+
'hours': 0,
101+
'minutes': -20,
102+
'string_basic': 'XXX+00:20',
103+
},
104+
'use_basic_format': True
105+
},
106+
'20001213T151012XXX+00:20',
107+
id='custom_time_zone_info_string_basic',
108+
),
109+
param(
110+
datetime(2000, 12, 13, 15, 30, 12, 123456),
111+
{
112+
'custom_time_zone_info': {
113+
'hours': 0,
114+
'minutes': -20,
115+
'string_extended': ':UK/Exeter',
116+
},
117+
'use_basic_format': False
118+
},
119+
'2000-12-13T15:10:12:UK/Exeter',
120+
id='custom_time_zone_info_string_extended',
121+
),
122+
param(
123+
datetime(2000, 12, 13, 15, 30, 12, 123456),
124+
{
125+
'custom_time_zone_info': {
126+
'hours': 0,
127+
'minutes': -20,
128+
'string_extended': ':UK/Exeter',
129+
},
130+
'use_basic_format': False,
131+
'date_time_is_local': True,
132+
},
133+
'2000-12-12T19:53:12:UK/Exeter',
134+
id='date_time_is_local',
135+
),
136+
),
137+
)
138+
def test_get_time_string(set_timezone, arg, kwargs, expect):
139+
set_timezone()
140+
assert get_time_string(arg, **kwargs) == expect

0 commit comments

Comments
 (0)