diff sat/tools/common/date_utils.py @ 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 7968c8b7b5e1
children ab2696e34d29
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):