changeset 3629:025ea418032f

tools (common/date_utils): parse `now` alone (when used without relative delta)
author Goffi <goffi@goffi.org>
date Fri, 27 Aug 2021 14:48:38 +0200
parents d685299645fa
children 8ee0140cc8b6
files sat/tools/common/date_utils.py
diffstat 1 files changed, 10 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/sat/tools/common/date_utils.py	Fri Aug 27 14:48:29 2021 +0200
+++ b/sat/tools/common/date_utils.py	Fri Aug 27 14:48:38 2021 +0200
@@ -25,6 +25,7 @@
 from dateutil import tz, parser
 from dateutil.relativedelta import relativedelta
 from dateutil.utils import default_tzinfo
+from dateutil.parser import ParserError
 from babel import dates
 import calendar
 import time
@@ -50,9 +51,15 @@
     value = str(value).strip()
     dayfirst = False if YEAR_FIRST_RE.match(value) else True
 
-    dt = default_tzinfo(
-        parser.parse(value, default=DEFAULT_DATETIME, dayfirst=dayfirst),
-        default_tz)
+    try:
+        dt = default_tzinfo(
+            parser.parse(value, default=DEFAULT_DATETIME, dayfirst=dayfirst),
+            default_tz)
+    except ParserError as e:
+        if value == "now":
+            dt = datetime.datetime.now(tz.tzutc())
+        else:
+            raise e
     return calendar.timegm(dt.utctimetuple())
 
 def date_parse_ext(value, default_tz=TZ_UTC):