Skip to content
Snippets Groups Projects
Commit 2f08a592 authored by Karl-Hermann Wieners's avatar Karl-Hermann Wieners
Browse files

Config: add function to convert ISO periods to seconds (period2sec)

parent 680c949e
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,14 @@ Make Experiments!
Release Changes
---------------
Release 1.4.2
=============
Config
------
* Add function to convert ISO periods to seconds (period2sec)
Release 1.4.1
=============
......
This diff is collapsed.
......@@ -283,6 +283,23 @@ class ExpConfig(ConfigObj):
h, m = divmod(minutes, 60)
return "{0:02}:{1:02}:{2:02}".format(h, m, s)
def period2sec(stamp):
# Do not support years and months for now
match = re.fullmatch(
r"P(?:(?P<days>\d+)D)?"
r"(?:T(?:(?P<hours>\d+)H)?"
r"(?:(?P<minutes>\d+)M)?"
r"(?:(?P<seconds>\d+)S)?)?",
stamp
)
if match:
values = match.groupdict(0)
return str(
(( int(values['days'])*24 + int(values['hours']) )*60
+ int(values['minutes']) )*60 + int(values['seconds'])
)
raise ValueError(f"unsupported period '{stamp}'")
def split_date(value):
'''Re-format datetime string to list for use in namelists'''
match = re.match(r'^0*(\d+)-0*(\d+)-0*(\d+)'
......@@ -392,6 +409,10 @@ class ExpConfig(ConfigObj):
if match:
return sec2time(match.group(1))
match = re.match(r'^period2sec\((.*)\)$', value, re.S)
if match:
return period2sec(match.group(1))
match = re.match(r'^read\((.*)\)$', value, re.S)
if match:
return read_value(match.group(1))
......
......@@ -456,6 +456,15 @@ class ContentTestCase(MkexpSimpleTestCase):
FALSE = eval(is_set('F'))
""")
def test_period2sec(self):
self.run_test(u"""
%{VALUE}
""", u"""
93784
""", u"""
VALUE = period2sec(P1DT2H3M4S)
""")
def test_initial_comment_boilerplate(self):
writeconfig(self.exp_id, u"""
######
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment