[Zope3-checkins] SVN: Zope3/branches/zipimport-support/src/zope/filereference/ fix extraction of date/time information from ZIP archives

Fred L. Drake, Jr. fdrake at gmail.com
Fri Nov 11 15:00:55 EST 2005


Log message for revision 40051:
  fix extraction of date/time information from ZIP archives

Changed:
  U   Zope3/branches/zipimport-support/src/zope/filereference/README.txt
  U   Zope3/branches/zipimport-support/src/zope/filereference/reference.py

-=-
Modified: Zope3/branches/zipimport-support/src/zope/filereference/README.txt
===================================================================
--- Zope3/branches/zipimport-support/src/zope/filereference/README.txt	2005-11-11 18:41:52 UTC (rev 40050)
+++ Zope3/branches/zipimport-support/src/zope/filereference/README.txt	2005-11-11 20:00:54 UTC (rev 40051)
@@ -55,6 +55,14 @@
   >>> zope.filereference.getmtime(ref) == os.path.getmtime(filename)
   True
 
+File modification times reported by Python can be either integers or
+floating-point numbers; let's make sure we can convert this to a
+float::
+
+  >>> mtime = float(zope.filereference.getmtime(ref))
+  >>> type(mtime)
+  <type 'float'>
+
 The reference can be opened using the `open()` function (which
 also accepts simple strings)::
 
@@ -104,8 +112,15 @@
   >>> zope.filereference.isfile(ref)
   True
 
-Note that only read modes are supported::
+The `getmtime()` function still returns a numeric value that can be
+converted to a float::
 
+  >>> mtime = float(zope.filereference.getmtime(ref))
+  >>> type(mtime)
+  <type 'float'>
+
+Note that only read modes are supported by `open()`::
+
   >>> zope.filereference.open(ref, "w")
   Traceback (most recent call last):
     ...

Modified: Zope3/branches/zipimport-support/src/zope/filereference/reference.py
===================================================================
--- Zope3/branches/zipimport-support/src/zope/filereference/reference.py	2005-11-11 18:41:52 UTC (rev 40050)
+++ Zope3/branches/zipimport-support/src/zope/filereference/reference.py	2005-11-11 20:00:54 UTC (rev 40051)
@@ -21,6 +21,7 @@
 import os
 import StringIO
 import sys
+import time
 import zipimport
 
 import zope.interface
@@ -325,13 +326,22 @@
             info = self._loader._files[relpath]
         except KeyError:
             raise OSError(errno.ENOENT, "No such file or directory", self)
-        d = info[5]
-        t = info[4]
+
+        # Note these indexes; the comments in the zipimport module's C
+        # source code were wrong in Python 2.4.2 and earlier.
+        d = info[6]
+        t = info[5]
+
         # This next is taken from the zipfile module:
-        dt = ( (d>>9)+1980, (d>>5)&0xF, d&0x1F,
-               t>>11, (t>>5)&0x3F, (t&0x1F) * 2 )
-        return dt
+        dt = (d>>9)+1980, (d>>5)&0xF, d&0x1F, t>>11, (t>>5)&0x3F, (t&0x1F)*2
 
+        # Convert to seconds since the epoch.  This isn't ideal, since
+        # we don't know if the time was UTC or local (or what local
+        # referred to at the time), but the most important thing in
+        # practice is to be able to compare two values for equality,
+        # so this is "good enough".
+        return time.mktime(dt + (0, 0, -1))
+
     def _getpath(self):
         relpath = self._relpath.replace("/", os.sep)
         if os.altsep:



More information about the Zope3-Checkins mailing list