[Zope-Checkins] CVS: Zope/lib/python/Products/PluginIndexes/DateIndex - DateIndex.py:1.12.2.3

Gary Poster gary at zope.com
Thu Feb 5 12:49:32 EST 2004


Update of /cvs-repository/Zope/lib/python/Products/PluginIndexes/DateIndex
In directory cvs.zope.org:/tmp/cvs-serv24501/DateIndex

Modified Files:
      Tag: Zope-2_7-branch
	DateIndex.py 
Log Message:
Make DateIndex friendly to Python 2.3 datetime package.




=== Zope/lib/python/Products/PluginIndexes/DateIndex/DateIndex.py 1.12.2.2 => 1.12.2.3 ===
--- Zope/lib/python/Products/PluginIndexes/DateIndex/DateIndex.py:1.12.2.2	Thu Jan 29 16:21:13 2004
+++ Zope/lib/python/Products/PluginIndexes/DateIndex/DateIndex.py	Thu Feb  5 12:49:01 2004
@@ -14,8 +14,11 @@
 """$Id$
 """
 
+from datetime import tzinfo, timedelta
 from types import StringType, FloatType, IntType
 from DateTime.DateTime import DateTime
+from OFS.PropertyManager import PropertyManager
+from datetime import date, datetime
 from Products.PluginIndexes import PluggableIndex
 from Products.PluginIndexes.common.UnIndex import UnIndex
 from Products.PluginIndexes.common.util import parseIndexRequest
@@ -29,8 +32,48 @@
 
 _marker = []
 
+###############################################################################
+# copied from Python 2.3 datetime.tzinfo docs
+# A class capturing the platform's idea of local time.
+
+ZERO = timedelta(0)
+STDOFFSET = timedelta(seconds = -time.timezone)
+if time.daylight:
+    DSTOFFSET = timedelta(seconds = -time.altzone)
+else:
+    DSTOFFSET = STDOFFSET
+
+DSTDIFF = DSTOFFSET - STDOFFSET
+
+class LocalTimezone(tzinfo):
+
+    def utcoffset(self, dt):
+        if self._isdst(dt):
+            return DSTOFFSET
+        else:
+            return STDOFFSET
+
+    def dst(self, dt):
+        if self._isdst(dt):
+            return DSTDIFF
+        else:
+            return ZERO
+
+    def tzname(self, dt):
+        return time.tzname[self._isdst(dt)]
+
+    def _isdst(self, dt):
+        tt = (dt.year, dt.month, dt.day,
+              dt.hour, dt.minute, dt.second,
+              dt.weekday(), 0, -1)
+        stamp = time.mktime(tt)
+        tt = time.localtime(stamp)
+        return tt.tm_isdst > 0
+
+Local = LocalTimezone()
+###############################################################################
 
-class DateIndex(UnIndex):
+class DateIndex(UnIndex, PropertyManager):
     """ Index for Dates """
 
     __implements__ = (PluggableIndex.UniqueValueIndex,
@@ -38,13 +81,18 @@
 
     meta_type = 'DateIndex'
     query_options = ['query', 'range']
+    
+    index_naive_time_as_local = True # False means index as UTC
+    _properties=({'id':'index_naive_time_as_local', 
+                  'type':'boolean',
+                  'mode':'w'},)
 
     manage = manage_main = DTMLFile( 'dtml/manageDateIndex', globals() )
     manage_main._setName( 'manage_main' )
     manage_options = ( { 'label' : 'Settings'
                        , 'action' : 'manage_main'
                        },
-                     )
+                     ) + PropertyManager.manage_options
 
     def clear( self ):
         """ Complete reset """
@@ -179,6 +227,13 @@
         elif type( value ) is StringType and value:
             t_obj = DateTime( value ).toZone('UTC')
             t_tup = t_obj.parts()
+        elif type( value ) is date:
+            t_tup = value.timetuple()
+        elif type( value ) is datetime:
+            if self.index_naive_time_as_local and value.tzinfo is None:
+                value = value.replace(tzinfo=Local)
+            # else if tzinfo is None, naive time interpreted as UTC
+            t_tup = value.utctimetuple()
         else:
             return default
 




More information about the Zope-Checkins mailing list