aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/quarg/actions.py
diff options
context:
space:
mode:
Diffstat (limited to 'quarg/actions.py')
-rw-r--r--quarg/actions.py18
1 files changed, 15 insertions, 3 deletions
diff --git a/quarg/actions.py b/quarg/actions.py
index 990327e..9361ac2 100644
--- a/quarg/actions.py
+++ b/quarg/actions.py
@@ -1,4 +1,5 @@
import argparse
+import re
from abc import ABCMeta, abstractmethod
import dateutil.relativedelta
@@ -49,14 +50,25 @@ class ParseAround(argparse.Action):
if not rangespec:
errx('Missing range for --around')
+ match = re.match(r'^(?P<range>\d+)(?P<unit>[hm])?$', rangespec)
+
+ if not match:
+ errx(f'Invalid range for --around: \'{rangespec}\'')
+
+ unit = match.group('unit')
+
try:
- hour_range = int(rangespec)
+ time_range = int(match.group('range'))
except ValueError as err:
errx(f'Error when parsing range for --around: {err}')
else:
- datespec, hour_range = (aroundspec, 12)
+ datespec, time_range, unit = (aroundspec, 12, 'h')
date = parse_isodate(datespec)
- offset = dateutil.relativedelta.relativedelta(hours=hour_range)
+ if unit == 'm':
+ offset = dateutil.relativedelta.relativedelta(minutes=time_range)
+ else:
+ offset = dateutil.relativedelta.relativedelta(hours=time_range)
+
setattr(namespace, self.dest, (date - offset, date + offset))