changeset 2825:1b6547fb80da

tools(common/date_utils): fixed order on year first + set default date for date_parse
author Goffi <goffi@goffi.org>
date Fri, 01 Mar 2019 15:35:55 +0100
parents 0ae25883e223
children 177f11163ed0
files sat/tools/common/date_utils.py
diffstat 1 files changed, 9 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/sat/tools/common/date_utils.py	Fri Mar 01 14:45:50 2019 +0100
+++ b/sat/tools/common/date_utils.py	Fri Mar 01 15:35:55 2019 +0100
@@ -33,9 +33,11 @@
 RELATIVE_RE = re.compile(ur"(?P<date>.*?)(?P<direction>[-+]?) *(?P<quantity>\d+) *"
                          ur"(?P<unit>(second|minute|hour|day|week|month|year))s?"
                          ur"(?P<ago> +ago)?", re.I)
-
+YEAR_FIRST_RE = re.compile(ur"\d{4}[^\d]+")
 TZ_UTC = tz.tzutc()
 TZ_LOCAL = tz.gettz()
+# used to replace values when something is missing
+DEFAULT_DATETIME = datetime.datetime(2000, 01, 01)
 
 
 def date_parse(value, default_tz=TZ_UTC):
@@ -45,7 +47,12 @@
     @param default_tz(datetime.tzinfo): default timezone
     @return (int): timestamp
     """
-    dt = default_tzinfo(parser.parse(unicode(value), dayfirst=True), default_tz)
+    value = unicode(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)
     return calendar.timegm(dt.utctimetuple())
 
 def date_parse_ext(value, default_tz=TZ_UTC):