[Zope-Checkins] CVS: Zope/lib/python/DateTime - DateTime.py:1.85

Andreas Jung andreas@andreas-jung.com
Wed, 22 Jan 2003 09:23:47 -0500


Update of /cvs-repository/Zope/lib/python/DateTime
In directory cvs.zope.org:/tmp/cvs-serv1615

Modified Files:
	DateTime.py 
Log Message:
merge from ajung-european-datetime-support-branch

=== Zope/lib/python/DateTime/DateTime.py 1.84 => 1.85 ===
--- Zope/lib/python/DateTime/DateTime.py:1.84	Thu Jan 16 11:41:38 2003
+++ Zope/lib/python/DateTime/DateTime.py	Wed Jan 22 09:23:15 2003
@@ -15,13 +15,18 @@
 __version__='$Revision$'[11:-2]
 
 
-import re, math,  DateTimeZone
+import os, re, math,  DateTimeZone
 from time import time, gmtime, localtime, asctime
 from time import daylight, timezone, altzone, strftime
 from types import InstanceType,IntType,FloatType,StringType,UnicodeType
 try: from time import tzname
 except: tzname=('UNKNOWN','UNKNOWN')
 
+
+_default_datefmt = os.environ.get('DATETIME_FORMAT', "us").lower()
+if not _default_datefmt in ('us', 'international'):
+    raise ValueError, "DATETIME_FORMAT must be either 'us' or 'international'"
+
 # To control rounding errors, we round system time to the nearest
 # millisecond.  Then delicate calculations can rely on that the
 # maximum precision that needs to be preserved is known.
@@ -456,7 +461,7 @@
     __roles__=None
     __allow_access_to_unprotected_subobjects__=1
 
-    def __init__(self,*args):
+    def __init__(self,*args, **kw):
         """Return a new date-time object
 
         A DateTime object always maintains its value as an absolute
@@ -601,6 +606,13 @@
             effect of this is as if you had taken the value of time.time()
             at that time on a machine in the specified timezone).
 
+            New in Zope 2.7:
+            A new keyword parameter "datefmt" can be passed to the 
+            constructor. If set to "international", the constructor
+            is forced to treat ambigious dates as "days before month
+            before year". This useful if you need to parse non-US
+            dates in a reliable way
+
         In any case that a floating point number of seconds is given
         or derived, it's rounded to the nearest millisecond.
 
@@ -613,6 +625,9 @@
         timezones recognized by the DateTime module. Recognition of
         timezone names is case-insensitive.""" #'
 
+        datefmt = kw.get('datefmt', _default_datefmt)
+        assert datefmt in ('us', 'international')
+
         d=t=s=None
         ac=len(args)
         millisecs = None
@@ -656,7 +671,7 @@
                 if arg.find(' ')==-1 and arg[4]=='-':
                     yr,mo,dy,hr,mn,sc,tz=self._parse_iso8601(arg)
                 else:
-                    yr,mo,dy,hr,mn,sc,tz=self._parse(arg)
+                    yr,mo,dy,hr,mn,sc,tz=self._parse(arg, datefmt)
 
 
                 if not self._validDate(yr,mo,dy):
@@ -860,7 +875,7 @@
         tz = self.localZone(ltm)
         return tz
 
-    def _parse(self,st):
+    def _parse(self,st, datefmt=_default_datefmt):
         # Parse date-time components from a string
         month=year=tz=tm=None
         spaces        =self.space_chars
@@ -987,8 +1002,13 @@
                     day=ints[0]
                     month=ints[1]
                 else:
-                    day=ints[1]
-                    month=ints[0]
+                    if datefmt=="us":
+                        day=ints[1]
+                        month=ints[0]
+                    else:
+                        day=ints[0]
+                        month=ints[1]
+    
             elif ints[0] <= 12:
                 month=ints[0]
                 day=ints[1]
@@ -1685,3 +1705,4 @@
 def Timezones():
     """Return the list of recognized timezone names"""
     return _cache._zlst
+