Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions beanquery/query_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,11 @@ def date_part(field, x):

@function([str], relativedelta)
def interval(x):
"""Construct a relative time interval."""
m = re.fullmatch(r'([-+]?[0-9]+)\s+(day|month|year)s?', x)
"""Construct a relative time interval. Example argument: '2 weeks'.
Further options are day, month, year, century, millenium (plural s can be
appended). Use to modify dates: `date + interval(...)`"""
x = x.lower()
m = re.fullmatch(r'([-+]?[0-9]+)\s+([a-z]+?)s?', x)
if not m:
return None
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Insted of returning None, would it not be better to raise some kind of syntax error (e.g. 'cannot match interval string with any of of allowed ones... ')

number = int(m.group(1))
Expand All @@ -702,7 +705,7 @@ def interval(x):
return relativedelta(years=number * 10)
if unit == 'century':
return relativedelta(years=number * 100)
if unit == 'millennium':
if unit in ['millennium', 'millenia']:
return relativedelta(years=number * 1000)
return None
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Insted of returning None, would it not be better to raise some kind of syntax error (e.g. 'cannot match interval string with any of of allowed ones... ')


Expand Down