+def valid_duration(txt: str) -> datetime.timedelta:
+ """If the string is a valid time duration, return a
+ datetime.timedelta representing the period of time. Otherwise
+ maybe raise an ArgumentTypeError or potentially just treat the
+ time window as zero in length.
+
+ >>> valid_duration('3m')
+ datetime.timedelta(seconds=180)
+
+ >>> valid_duration('your mom')
+ datetime.timedelta(seconds=0)
+
+ """
+ from datetime_utils import parse_duration
+ try:
+ secs = parse_duration(txt)
+ except Exception as e:
+ raise argparse.ArgumentTypeError(e)
+ finally:
+ return datetime.timedelta(seconds=secs)
+
+