[#59] Add support for dateutil.parser dayfirst and yearfirst arguments
authorEvgeni Kunev <evgeni.kunev@gmail.com>
Mon, 25 Aug 2014 15:00:56 +0300
changeset 43 f7bf8d6f0547
parent 42 d187963bb7e9
child 44 5cd71f1dc42b
[#59] Add support for dateutil.parser dayfirst and yearfirst arguments The WhoisEntry object should expose a dayfirst and yearfist argument to signify how the datetime format for the TLD should be parsed by dateutil. The base WhoisEntry class gives both those fields a value of False. Those are also the default values dateutil assumes.
whois/parser.py
--- a/whois/parser.py	Fri Aug 22 15:00:14 2014 +0300
+++ b/whois/parser.py	Mon Aug 25 15:00:56 2014 +0300
@@ -51,12 +51,17 @@
     return s
 
 
-def cast_date(s):
+def cast_date(s, dayfirst=False, yearfirst=False):
     """Convert any date string found in WHOIS to a datetime object.
     """
     if DATEUTIL:
         try:
-            return dp.parse(s.strip(), tzinfos=tz_data).replace(tzinfo=None)
+            return dp.parse(
+                s.strip(),
+                tzinfos=tz_data,
+                dayfirst=dayfirst,
+                yearfirst=yearfirst
+            ).replace(tzinfo=None)
         except Exception:
             return datetime_parse(s)
     else:
@@ -81,6 +86,8 @@
         'emails':           '[\w.-]+@[\w.-]+\.[\w]{2,4}',  # list of email s
         'dnssec':           'dnssec:\s*([\S]+)',
     }
+    dayfirst = False
+    yearfirst = False
 
     def __init__(self, domain, text, regex=None):
         self.domain = domain
@@ -98,7 +105,9 @@
             for value in re.findall(whois_regex, self.text, re.IGNORECASE):
                 if isinstance(value, basestring):
                     # try casting to date format
-                    value = cast_date(value.strip())
+                    value = cast_date(value.strip(),
+                                      dayfirst=self.dayfirst,
+                                      yearfirst=self.yearfirst)
                 if value and value not in values:
                     # avoid duplicates
                     values.append(value)
@@ -640,6 +649,8 @@
         'expiration_date': 'expires at:\s*(.+)',
     }
 
+    dayfirst = True
+
     def __init__(self, domain, text):
         if text.strip() == 'No entries found':
             raise PywhoisError(text)