Debian Codegraph @

cylc/flow/cycling/util.py

raw
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 """Cycling utility functions."""
17
18 from metomi.isodatetime.parsers import TimePointParser, DurationParser
19
20
21 def add_offset(cycle_point, offset, dmp_fmt=None):
22 """Add a (positive or negative) offset to a cycle point.
23
24 Return the result.
25
26 """
27 my_parser = TimePointParser()
28 if dmp_fmt is None:
29 my_target_point = my_parser.parse(cycle_point, dump_as_parsed=True)
30 else:
31 my_target_point = my_parser.parse(cycle_point, dump_format=dmp_fmt)
32 my_offset_parser = DurationParser()
33
34 oper = "+"
35 if offset.startswith(("-", "+")):
36 oper = offset[0]
37 offset = offset[1:]
38
39 if not offset.startswith("P"):
40 # TODO - raise appropriate exception
41 raise ValueError("ERROR, bad offset format: %s" % offset)
42
43 my_shift = my_offset_parser.parse(offset)
44 if oper == "-":
45 my_target_point -= my_shift
46 else:
47 my_target_point += my_shift
48
49 return my_target_point