Debian Codegraph @

metomi/isodatetime/tests/test_datetimeoper.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 # ----------------------------------------------------------------------------
3 # Copyright (C) British Crown (Met Office) & Contributors.
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU Lesser General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17 # ----------------------------------------------------------------------------
18 """Test isodatetime.datetimeoper functionalities."""
19
20
21 import os
22 import unittest
23 from unittest.mock import patch
24
25
26 from metomi.isodatetime.data import (
27 get_timepoint_from_seconds_since_unix_epoch as seconds2point)
28 import metomi.isodatetime.datetimeoper as idt_dtoper
29 from metomi.isodatetime.exceptions import OffsetValueError
30
31
32 class TestDateTimeOperator(unittest.TestCase):
33 """Test isodatetime.datetimeoper.TestDateTimeOperator functionalities."""
34
35 @patch('metomi.isodatetime.datetimeoper.now2point')
36 def test_process_time_point_str_now_0(self, mock_now_func):
37 """DateTimeOperator.process_time_point_str()"""
38 # 2009-02-13T23:31:30Z
39 mock_now = seconds2point(1234567890)
40 mock_now_func.return_value = mock_now
41 datetimeoper = idt_dtoper.DateTimeOperator()
42 self.assertEqual(str(mock_now), datetimeoper.process_time_point_str())
43 self.assertEqual(
44 str(mock_now),
45 datetimeoper.process_time_point_str(datetimeoper.STR_NOW))
46
47 @patch('metomi.isodatetime.datetimeoper.now2point')
48 def test_process_time_point_str_ref_0(self, mock_now_func):
49 """DateTimeOperator.process_time_point_str('ref')
50
51 But without explicit reference time, so default to now.
52 """
53 # 2009-02-13T23:31:30Z
54 mock_now = seconds2point(1234567890)
55 mock_now_func.return_value = mock_now
56 datetimeoper = idt_dtoper.DateTimeOperator()
57 # Ensure that the ISODATETIMEREF environment variable is not set
58 # Or the test may not work.
59 environ = os.environ.copy()
60 if datetimeoper.ENV_REF in environ:
61 del environ[datetimeoper.ENV_REF]
62 with patch.dict(os.environ, environ, clear=True):
63 self.assertEqual(
64 str(mock_now),
65 datetimeoper.process_time_point_str(datetimeoper.STR_REF))
66
67 def test_process_time_point_str_ref_1(self):
68 """DateTimeOperator.process_time_point_str('ref')
69
70 With explicit reference time.
71 """
72 # 2009-02-13T23:31:30Z
73 ref_point_str = str(seconds2point(1234567890))
74 datetimeoper = idt_dtoper.DateTimeOperator(
75 ref_point_str=ref_point_str)
76 self.assertEqual(
77 ref_point_str,
78 datetimeoper.process_time_point_str(datetimeoper.STR_REF))
79
80 def test_process_time_point_str_ref_2(self):
81 """DateTimeOperator.process_time_point_str('ref')
82
83 With explicit reference time as ISODATETIMEREF environment variable.
84 """
85 # 2009-02-13T23:31:30Z
86 ref_point_str = str(seconds2point(1234567890))
87 # Set ISODATETIMEREF.
88 # Or the test may not work.
89 environ = os.environ.copy()
90 environ[idt_dtoper.DateTimeOperator.ENV_REF] = (
91 ref_point_str)
92 with patch.dict(os.environ, environ):
93 datetimeoper = idt_dtoper.DateTimeOperator()
94 self.assertEqual(
95 ref_point_str,
96 datetimeoper.process_time_point_str(datetimeoper.STR_REF))
97
98 def test_process_time_point_str_x(self):
99 """DateTimeOperator.process_time_point_str(...)
100
101 Basic parse and dump of a time point string.
102 """
103 # 2009-02-13T23:31:30Z
104 point_str = str(seconds2point(1234567890))
105 datetimeoper = idt_dtoper.DateTimeOperator()
106 # Unix time
107 self.assertEqual(
108 '2019-01-11T10:40:15Z',
109 datetimeoper.process_time_point_str(
110 'Fri 11 Jan 10:40:15 UTC 2019',
111 print_format=datetimeoper.CURRENT_TIME_DUMP_FORMAT_Z))
112 # Basic
113 self.assertEqual(
114 point_str,
115 datetimeoper.process_time_point_str(point_str))
116 # +ve offset
117 point_str_1 = str(seconds2point(1234567890 + 3600))
118 self.assertEqual(
119 point_str_1,
120 datetimeoper.process_time_point_str(point_str, ['PT1H']))
121 # +ve offset, time point like duration
122 point_str_1 = str(seconds2point(1234567890 + 3600))
123 self.assertEqual(
124 point_str_1,
125 datetimeoper.process_time_point_str(point_str, ['P0000-00-00T01']))
126 # -ve offset
127 point_str_2 = str(seconds2point(1234567890 - 86400))
128 self.assertEqual(
129 point_str_2,
130 datetimeoper.process_time_point_str(point_str, ['-P1D']))
131 # offsets that cancel out
132 self.assertEqual(
133 point_str,
134 datetimeoper.process_time_point_str(point_str, ['PT1H', '-PT60M']))
135 # Multiple offsets in 1 string
136 point_str_3 = str(seconds2point(1234567890 - 86400 - 3600))
137 self.assertEqual(
138 point_str_3,
139 datetimeoper.process_time_point_str(point_str, ['-P1DT1H']))
140 # Multiple offsets
141 self.assertEqual(
142 point_str_3,
143 datetimeoper.process_time_point_str(point_str, ['-P1D', '-PT1H']))
144 # Bad time point string
145 self.assertRaises(
146 ValueError,
147 datetimeoper.process_time_point_str, 'teatime')
148 # Bad offset string
149 with self.assertRaises(
150 OffsetValueError,
151 ) as ctxmgr:
152 datetimeoper.process_time_point_str(point_str, ['ages'])
153 self.assertEqual('ages: bad offset value', str(ctxmgr.exception))
154 # Bad offset string, unsupported time point like duration
155 with self.assertRaises(
156 OffsetValueError,
157 ) as ctxmgr:
158 datetimeoper.process_time_point_str(point_str, ['P0000-W01-1'])
159 self.assertEqual(
160 'P0000-W01-1: bad offset value',
161 str(ctxmgr.exception))
162
163 def test_process_time_point_str_calendar(self):
164 """DateTimeOperator.process_time_point_str(...)
165
166 Alternate calendars.
167 """
168 self.assertEqual(
169 'gregorian',
170 idt_dtoper.DateTimeOperator.get_calendar_mode())
171 self.assertRaises(
172 KeyError,
173 idt_dtoper.DateTimeOperator.set_calendar_mode,
174 'milkywaygalactic')
175 for cal, str_in, offsets, str_out in [
176 # 360day
177 ('360day', '20130301', ['-P1D'], '20130230'),
178 ('360day', '20130230', ['P1D'], '20130301'),
179 # 360_day
180 ('360_day', '20130301', ['-P1D'], '20130230'),
181 ('360_day', '20130230', ['P1D'], '20130301'),
182 # 365day
183 ('365day', '20130301', ['-P1D'], '20130228'),
184 ('365day', '20130228', ['P1D'], '20130301'),
185 # 365_day
186 ('365_day', '20130301', ['-P1D'], '20130228'),
187 ('365_day', '20130228', ['P1D'], '20130301'),
188 # 366day
189 ('366day', '20130301', ['-P1D'], '20130229'),
190 ('366day', '20130229', ['P1D'], '20130301'),
191 # 366_day
192 ('366_day', '20130301', ['-P1D'], '20130229'),
193 ('366_day', '20130229', ['P1D'], '20130301'),
194 ]:
195 # Calendar mode, is unfortunately, a global variable,
196 # so needs to reset value on return.
197 calendar_mode = (
198 idt_dtoper.DateTimeOperator.get_calendar_mode())
199 # Calendar mode by constructor.
200 try:
201 datetimeoper = idt_dtoper.DateTimeOperator(
202 calendar_mode=cal)
203 self.assertEqual(
204 str_out,
205 datetimeoper.process_time_point_str(str_in, offsets))
206 finally:
207 idt_dtoper.DateTimeOperator.set_calendar_mode(
208 calendar_mode)
209 # Calendar mode by environment variable
210 try:
211 environ = os.environ.copy()
212 key = (
213 idt_dtoper.DateTimeOperator.ENV_CALENDAR_MODE
214 )
215 environ[key] = cal
216 with patch.dict(os.environ, environ, clear=True):
217 datetimeoper = idt_dtoper.DateTimeOperator()
218 self.assertEqual(
219 str_out,
220 datetimeoper.process_time_point_str(
221 str_in, offsets))
222 finally:
223 idt_dtoper.DateTimeOperator.set_calendar_mode(
224 calendar_mode)
225
226 def test_process_time_point_str_format(self):
227 """DateTimeOperator.process_time_point_str(...)
228
229 With parse_format and print_format.
230 """
231 for parse_format, print_format, point_str_in, point_str_out in [
232 ('%d/%m/%Y %H:%M:%S', '%Y-%m-%dT%H:%M:%S',
233 '24/12/2012 06:00:00', '2012-12-24T06:00:00'),
234 ('%Y,%M,%d,%H', '%Y%M%d%H', '2014,01,02,05', '2014010205'),
235 ('%Y%m%d', '%y%m%d', '20141231', '141231'),
236 ('%Y%m%d%H%M%S', '%s', '20140402100000', '1396432800'),
237 ('%s', '%Y%m%dT%H%M%S%z', '1396429200', '20140402T090000+0000'),
238 ('%d/%m/%Y %H:%M:%S', 'CCYY-MM-DDThh:mm',
239 '24/12/2012 06:00:00', '2012-12-24T06:00'),
240 (None, 'CCYY-MM-DDThh:mm+01:00',
241 '2014-091T15:14:03Z', '2014-04-01T16:14+01:00'),
242 (None, '%m', '2014-02-01T04:05:06', '02'),
243 (None, '%Y', '2014-02-01T04:05:06', '2014'),
244 (None, '%H', '2014-02-01T04:05:06', '04'),
245 (None, '%Y%m%d_%H%M%S', '2014-02-01T04:05:06', '20140201_040506'),
246 (None, '%Y.file', '2014-02-01T04:05:06', '2014.file'),
247 (None, 'y%Ym%md%d', '2014-02-01T04:05:06', 'y2014m02d01'),
248 (None, '%F', '2014-02-01T04:05:06', '2014-02-01'),
249
250 ]:
251 datetimeoper = idt_dtoper.DateTimeOperator(
252 utc_mode=True,
253 parse_format=parse_format)
254 self.assertEqual(
255 point_str_out,
256 datetimeoper.process_time_point_str(
257 point_str_in, print_format=print_format))
258 # Bad parse format
259 datetimeoper = idt_dtoper.DateTimeOperator(
260 parse_format='%o')
261 with self.assertRaises(ValueError) as ctxmgr:
262 datetimeoper.process_time_point_str('0000')
263 self.assertEqual(
264 "'o' is a bad directive in format '%o'",
265 str(ctxmgr.exception))
266
267 def test_format_duration_str_x(self):
268 """DateTimeOperator.format_duration_str(...)"""
269 datetimeoper = idt_dtoper.DateTimeOperator()
270 # Good ones
271 for print_format, duration_str_in, duration_out in [
272 ('s', 'PT1M', 60.0),
273 ('s', 'P1DT1H1M1S', 90061.0),
274 ('m', 'PT1S', 0.0166666666667),
275 ('h', 'P832DT23H12M45S', 19991.2125),
276 ('S', '-PT1M1S', -61.0),
277 ]:
278 self.assertAlmostEqual(
279 duration_out,
280 datetimeoper.format_duration_str(
281 duration_str_in, print_format))
282 # Bad ones
283 for print_format, duration_str_in in [
284 ('y', 'PT1M'),
285 ('s', 'quickquick'),
286 ]:
287 self.assertRaises(
288 ValueError,
289 datetimeoper.format_duration_str,
290 duration_str_in, print_format)
291
292 def test_diff_time_point_strs(self):
293 """DateTimeOperator.diff_time_point_strs(...)"""
294 datetimeoper = idt_dtoper.DateTimeOperator(
295 ref_point_str='20150106')
296 for (
297 time_point_str1,
298 time_point_str2,
299 offsets1, offsets2,
300 print_format,
301 duration_print_format,
302 duration_out,
303 ) in [
304 ( # Positive
305 '20130101T12',
306 '20130301',
307 None,
308 None,
309 None,
310 None,
311 'P58DT12H',
312 ),
313 ( # Positive, non integer seconds
314 # Use (3.1 - 3.0) to bypass str(float) precision issue
315 '20190101T010203',
316 '20190101T010203.1',
317 None,
318 None,
319 None,
320 None,
321 'PT%sS' % (str(3.1 - 3.0).replace('.', ',')),
322 ),
323 ( # Positive, non integer seconds, print format
324 # Use (3.1 - 3.0) to bypass str(float) precision issue
325 '20190101T010203',
326 '20190101T010203.1',
327 None,
328 None,
329 's',
330 None,
331 str(3.1 - 3.0),
332 ),
333 ( # Offset 1, reference time 2, positive
334 '20140101',
335 'ref',
336 ['P11M24D'],
337 None,
338 None,
339 None,
340 'P12D',
341 ),
342 ( # Offset 2, positive
343 '20100101T00',
344 '20100201T00',
345 None,
346 ['P1D'],
347 None,
348 None,
349 'P32D',
350 ),
351 ( # Neutral
352 '20151225T00',
353 '20151225',
354 None,
355 None,
356 None,
357 None,
358 'P0Y',
359 ),
360 ( # Negative
361 '20150101T12',
362 '20130301',
363 None,
364 None,
365 None,
366 None,
367 '-P671DT12H',
368 ),
369 ( # Alternate format
370 '20130101T12',
371 '20130301',
372 None,
373 None,
374 'y,m,d,h,M,s',
375 None,
376 '0,0,58,12,0,0',
377 ),
378 ( # Offset 2, alternate format
379 '0000',
380 '0000',
381 ['-PT2M'],
382 None,
383 'y,m,d,h,M,s',
384 None,
385 '0,0,0,0,2,0',
386 ),
387 ( # As seconds, positive
388 '2000-01-01T00:00:00',
389 '2000-01-01T01:00:00',
390 None,
391 None,
392 None,
393 's',
394 3600.0,
395 ),
396 ( # As seconds, neutral
397 '2000-01-01T00:00:00',
398 '2000-01-01T00:00:00',
399 None,
400 None,
401 None,
402 's',
403 0.0,
404 ),
405 ( # As seconds, negative
406 '2000-01-01T00:00:00',
407 '1999-12-31T23:00:00',
408 None,
409 None,
410 None,
411 's',
412 -3600.0,
413 ),
414 ]:
415 self.assertEqual(
416 duration_out,
417 datetimeoper.diff_time_point_strs(
418 time_point_str1,
419 time_point_str2,
420 offsets1,
421 offsets2,
422 print_format,
423 duration_print_format))
424
425
426 if __name__ == '__main__':
427 unittest.main()