dabo Commit
Revision 5215
Date: 2009-05-13 16:26:40 -0700 (Wed, 13 May 2009)
Author: Paul
Trac: http://trac.dabodev.com/changeset/5215

Changed:
U   trunk/dabo/lib/dates.py

Log:
Changed getDateFromString to look at previously-used date formats, too, as a 
last fallback. This will never happen in real code, but I noticed an
oddity in testing. We still fall back to the default locale's format at the
end, after trying the current and prior dabo dateFormats.


Diff:
Modified: trunk/dabo/lib/dates.py
===================================================================
--- trunk/dabo/lib/dates.py     2009-05-13 20:14:34 UTC (rev 5214)
+++ trunk/dabo/lib/dates.py     2009-05-13 23:26:40 UTC (rev 5215)
@@ -12,7 +12,9 @@
 _dtregex = {}
 _tregex = {}
 
+_usedDateFormats = ["%x"]
 
+
 def _getDateRegex(format):
        elements = {}
        elements["year"] = "(?P<year>[0-9]{4,4})"              ## year 0000-9999
@@ -100,17 +102,23 @@
 def getDateFromString(strVal, formats=None):
        """Given a string in a defined format, return a date object or None."""
        global _dregex
+       global _usedDateFormats
 
-       dateFormat = dabo.settings.dateFormat
        ret = None
 
        if formats is None:
                formats = ["ISO8601"]
 
-       if dateFormat is not None:
-               # Take the date format as set in dabo into account, when trying 
-               # to make a date out of the string.
-               formats.append(dateFormat)
+       sdf = dabo.settings.dateFormat
+       if sdf is not None:
+               if _usedDateFormats[0] == sdf:
+                       # current date format is already first in the list; do 
nothing
+                       pass
+               else:
+                       if sdf in _usedDateFormats:
+                               
del(_usedDateFormats[_usedDateFormats.index(sdf)])
+                       _usedDateFormats.insert(0, sdf)
+       formats.extend(_usedDateFormats)
 
        # Try each format in order:
        for format in formats:
@@ -119,7 +127,10 @@
                except KeyError:
                        regex = _getDateRegex(format)
                        if regex is None:
-                               continue
+                               try:
+                                       return 
datetime.date(*time.strptime(strVal, format)[:3])
+                               except ValueError:
+                                       continue
                        _dregex[format] = regex
                m = regex.match(strVal)
                if m is not None:
@@ -141,13 +152,6 @@
                                pass
                if ret is not None:
                        break   
-       if ret is None:
-               if dateFormat is None:
-                       # Fall back to the current locale setting in user's os 
account:
-                       try:
-                               ret = datetime.date(*time.strptime(strVal, 
"%x")[:3])
-                       except ValueError:  ## ValueError from time.strptime()
-                               pass
        return ret
 
 



_______________________________________________
Post Messages to: [email protected]
Subscription Maintenance: http://leafe.com/mailman/listinfo/dabo-dev
Searchable Archives: http://leafe.com/archives/search/dabo-dev
This message: 
http://leafe.com/archives/byMID/[email protected]

Reply via email to