Skip to content

Commit

Permalink
metomi#2: support 2nd recurrence format e.g. R/P2Y.
Browse files Browse the repository at this point in the history
  • Loading branch information
benfitzpatrick committed Feb 26, 2014
1 parent 34faf60 commit 4fd6d1d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
28 changes: 26 additions & 2 deletions isodatetime/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,28 @@ def __init__(self, timepoint_parser=None, timeinterval_parser=None):
else:
self.timepoint_parser = timeinterval_parser

def parse(self, expression):
"""Parse a recurrence string into a TimeRecurrence instance."""
def parse(self, expression, context_point=None):
"""Parse a recurrence string into a TimeRecurrence instance.
expression should be a string that looks like one of the four
ISO 8601 recurrence formats - e.g.
"R5/20140228T0630Z/20140228T0830Z"
"R5/PT10M" (with a supplied context)
"R5/20140228T0630Z/PT10M"
"R5/PT10M/20140228T0830Z"
where the slash is a delimiter, the number following the R is
an optional limit for the number of recurrences, strings
beginning with "2014" are ISO 8601 date/time representations,
and string beginning with "P" are ISO 8601 duration
representations.
context_point should be given for the second format, which will
then behave in the same way as the third format - start at
the context point and apply the interval counting forwards in
time.
"""
for regex in self.RECURRENCE_REGEXES:
result = regex.search(expression)
if not result:
Expand All @@ -87,6 +107,10 @@ def parse(self, expression):
if "intv" in result_map:
interval = self.timeinterval_parser.parse(
result_map["intv"])
if (context_point is not None and start_point is None and
end_point is None):
# Second ISO 8601 recurrence format.
start_point = context_point
return data.TimeRecurrence(
repetitions=repetitions,
start_point=start_point,
Expand Down
12 changes: 11 additions & 1 deletion isodatetime/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -444,6 +444,12 @@ def get_timerecurrenceparser_tests():
yield expr_1, {"repetitions": reps,
"start_point": start_point,
"end_point": end_point}
expr_2 = ("R" + reps_string + "/" + str(interval))
yield (expr_2, start_point), {
"repetitions": reps,
"start_point": start_point,
"interval": interval
}
expr_3 = ("R" + reps_string + "/" + str(start_point) +
"/" + str(interval))
yield expr_3, {"repetitions": reps,
Expand Down Expand Up @@ -588,8 +594,12 @@ def test_timerecurrence_parser(self):
"""Test the recurring date/time series parsing."""
parser = parsers.TimeRecurrenceParser()
for expression, test_info in get_timerecurrenceparser_tests():
context_point=None
if isinstance(expression, tuple):
expression, context_point = expression
try:
test_data = str(parser.parse(expression))
test_data = str(parser.parse(expression,
context_point=context_point))
except parsers.TimeSyntaxError:
raise ValueError("Parsing failed for %s" % expression)
ctrl_data = str(data.TimeRecurrence(**test_info))
Expand Down

0 comments on commit 4fd6d1d

Please sign in to comment.