| 1 |
# THIS FILE IS PART OF THE CYLC WORKFLOW ENGINE. |
| 2 |
# Copyright (C) NIWA & British Crown (Met Office) & Contributors. |
| 3 |
# |
| 4 |
# This program is free software: you can redistribute it and/or modify |
| 5 |
# it under the terms of the GNU General Public License as published by |
| 6 |
# the Free Software Foundation, either version 3 of the License, or |
| 7 |
# (at your option) any later version. |
| 8 |
# |
| 9 |
# This program is distributed in the hope that it will be useful, |
| 10 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
# GNU General Public License for more details. |
| 13 |
# |
| 14 |
# You should have received a copy of the GNU General Public License |
| 15 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 |
|
| 17 |
from datetime import datetime |
| 18 |
import pytest |
| 19 |
from pytest import param |
| 20 |
|
| 21 |
from metomi.isodatetime.data import CALENDAR |
| 22 |
from cylc.flow.wallclock import ( |
| 23 |
get_current_time_string, |
| 24 |
get_time_string, |
| 25 |
get_unix_time_from_time_string, |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
@pytest.mark.parametrize( |
| 30 |
'time_str,time_sec', |
| 31 |
[ |
| 32 |
('2016-09-08T09:09:00+01', 1473322140), |
| 33 |
('2016-09-08T08:09:00Z', 1473322140), |
| 34 |
('2016-09-07T20:09:00-12', 1473322140), |
| 35 |
] |
| 36 |
) |
| 37 |
def test_get_unix_time_from_time_string(time_str, time_sec): |
| 38 |
assert get_unix_time_from_time_string(time_str) == time_sec |
| 39 |
|
| 40 |
|
| 41 |
@pytest.mark.parametrize( |
| 42 |
'time_str,time_sec', |
| 43 |
[ |
| 44 |
('2016-09-08T09:09:00+01', 1473322140), |
| 45 |
('2016-09-08T08:09:00Z', 1473322140), |
| 46 |
('2016-09-07T20:09:00-12', 1473322140), |
| 47 |
('2016-08-31T18:09:00+01', 1472663340), |
| 48 |
] |
| 49 |
) |
| 50 |
def test_get_unix_time_from_time_string_360(time_str, time_sec): |
| 51 |
mode = CALENDAR.mode |
| 52 |
CALENDAR.set_mode(CALENDAR.MODE_360) |
| 53 |
try: |
| 54 |
assert get_unix_time_from_time_string(time_str) == time_sec |
| 55 |
finally: |
| 56 |
CALENDAR.set_mode(mode) |
| 57 |
|
| 58 |
|
| 59 |
@pytest.mark.parametrize( |
| 60 |
'value,error', |
| 61 |
[ |
| 62 |
(None, TypeError), |
| 63 |
(42, TypeError) |
| 64 |
] |
| 65 |
) |
| 66 |
def test_get_unix_time_from_time_string_error(value, error): |
| 67 |
with pytest.raises(error): |
| 68 |
get_unix_time_from_time_string(value) |
| 69 |
|
| 70 |
|
| 71 |
def test_get_current_time_string(set_timezone): |
| 72 |
"""It reacts to local time zone changes. |
| 73 |
|
| 74 |
https://github.com/cylc/cylc-flow/issues/6701 |
| 75 |
""" |
| 76 |
set_timezone() |
| 77 |
res = get_current_time_string() |
| 78 |
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 |