Skip to content

Commit ac14941

Browse files
committed
Tidy & add test
1 parent ff3f3aa commit ac14941

File tree

2 files changed

+39
-12
lines changed

2 files changed

+39
-12
lines changed

cylc/flow/wallclock.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@
4545
TIME_ZONE_STRING_UTC = "Z"
4646
TIME_ZONE_UTC_UTC_OFFSET = (0, 0)
4747
TIME_ZONE_LOCAL_UTC_OFFSET = get_local_time_zone()
48-
TIME_ZONE_LOCAL_UTC_OFFSET_HOURS = TIME_ZONE_LOCAL_UTC_OFFSET[0]
49-
TIME_ZONE_LOCAL_UTC_OFFSET_MINUTES = TIME_ZONE_LOCAL_UTC_OFFSET[1]
5048

5149
TIME_ZONE_LOCAL_INFO = {
5250
"hours": TIME_ZONE_LOCAL_UTC_OFFSET[0],
@@ -151,8 +149,7 @@ def get_time_string(date_time, display_sub_seconds=False,
151149
else:
152150
custom_string = custom_time_zone_info["string_extended"]
153151
if date_time_is_local:
154-
date_time_hours = TIME_ZONE_LOCAL_UTC_OFFSET_HOURS
155-
date_time_minutes = TIME_ZONE_LOCAL_UTC_OFFSET_MINUTES
152+
date_time_hours, date_time_minutes = TIME_ZONE_LOCAL_UTC_OFFSET
156153
else:
157154
date_time_hours, date_time_minutes = (0, 0)
158155
diff_hours = custom_hours - date_time_hours
@@ -163,18 +160,15 @@ def get_time_string(date_time, display_sub_seconds=False,
163160
elif override_use_utc or (override_use_utc is None and _FLAGS['utc_mode']):
164161
time_zone_string = TIME_ZONE_STRING_UTC
165162
if date_time_is_local:
166-
date_time = date_time - timedelta(
167-
hours=TIME_ZONE_LOCAL_UTC_OFFSET_HOURS,
168-
minutes=TIME_ZONE_LOCAL_UTC_OFFSET_MINUTES
169-
)
163+
h, m = TIME_ZONE_LOCAL_UTC_OFFSET
164+
date_time = date_time - timedelta(hours=h, minutes=m)
170165
else:
171166
if use_basic_format:
172167
time_zone_string = TIME_ZONE_STRING_LOCAL_BASIC
173168
else:
174169
time_zone_string = TIME_ZONE_STRING_LOCAL_EXTENDED
175170
if not date_time_is_local:
176-
diff_hours = TIME_ZONE_LOCAL_UTC_OFFSET_HOURS
177-
diff_minutes = TIME_ZONE_LOCAL_UTC_OFFSET_MINUTES
171+
diff_hours, diff_minutes = TIME_ZONE_LOCAL_UTC_OFFSET
178172
date_time = date_time + timedelta(
179173
hours=diff_hours, minutes=diff_minutes)
180174
if use_basic_format:

tests/unit/test_wallclock.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,19 @@
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-
import pytest
17+
from datetime import (
18+
datetime,
19+
timedelta,
20+
timezone,
21+
)
1822

1923
from metomi.isodatetime.data import CALENDAR
20-
from cylc.flow.wallclock import get_unix_time_from_time_string
24+
import pytest
25+
26+
from cylc.flow.wallclock import (
27+
get_time_string,
28+
get_unix_time_from_time_string,
29+
)
2130

2231

2332
@pytest.mark.parametrize(
@@ -60,3 +69,27 @@ def test_get_unix_time_from_time_string_360(time_str, time_sec):
6069
def test_get_unix_time_from_time_string_error(value, error):
6170
with pytest.raises(error):
6271
get_unix_time_from_time_string(value)
72+
73+
74+
@pytest.mark.parametrize('tz_info', [
75+
pytest.param(None, id="naive"),
76+
pytest.param(timezone.utc, id="utc-tz-aware"),
77+
pytest.param(timezone(timedelta(hours=5)), id="custom-tz-aware"),
78+
])
79+
def test_get_time_string_tzinfo(tz_info, monkeypatch: pytest.MonkeyPatch):
80+
"""Basic check it handles naive and timezone-aware datetime objects.
81+
82+
Currently we just ignore the timezone information in the datetime object.
83+
"""
84+
# Mock UTC time zone:
85+
monkeypatch.setattr(
86+
'cylc.flow.wallclock.TIME_ZONE_LOCAL_UTC_OFFSET', (0, 0)
87+
)
88+
for fmt in ('BASIC', 'EXTENDED'):
89+
monkeypatch.setattr(
90+
f'cylc.flow.wallclock.TIME_ZONE_STRING_LOCAL_{fmt}', 'Z'
91+
)
92+
93+
assert get_time_string(
94+
datetime(2077, 2, 8, 13, 42, 39, 123456, tz_info)
95+
) == '2077-02-08T13:42:39Z'

0 commit comments

Comments
 (0)