views:

36

answers:

2

Python 3.1.2
Windows XP SP3

I am running into a problem with some files and their timestamps in python. I have a bunch of files in a directory that I received from an external source. It's not every file I am having a problem with but for some files python is showing an hour difference from what explorer or cmd show in XP. I am specifically seeing this problem when using the zipfile module in which after a file is zipped the "date modified" timestamp is changed to what python interprets it as, shown below.

CMD - before zipping

C:\forms>dir /T:W "C:\forms\7aihy56.fmx"
02/02/2007  12:50 PM           195,148 7aihy56.fmx
               1 File(s)        195,148 bytes
               0 Dir(s)  985,520,533,504 bytes free

Python - get mtime ctime

>>>import os
>>>st = os.stat("C:\\forms\\7aihy56.fmx")
>>>print(time.asctime(time.localtime(st[8])))
>>>print(time.asctime(time.localtime(st[9])))
Fri Feb 02 11:50:24 2007
Fri Feb 02 11:50:24 2007

List contents of zip file after zipping using python zipfile module

>>>import datetime
>>>import zipfile
>>>zf = zipfile.ZipFile("C:\\daily_forms_auto_backup.zip")
>>>for info in zf.infolist():
>>> print(info.filename)
>>> print('\tModified:\t', datetime.datetime(*info.date_time))
>>> print
forms/7aihy56.fmx
    Modified:    2007-02-02 11:50:24

CMD - after extracting from zip file

C:\forms>dir /T:W "C:\forms\7aihy56.fmx"
02/02/2007  11:50 AM           195,148 7aihy56.fmx
               1 File(s)        195,148 bytes
               0 Dir(s)  984,923,164,672 bytes free
A: 

Sounds like a daylight savings issue. Do you find that files in one half of the year are off by an hour and files in the other half of the year are correct?

Ned Batchelder
Yes. What can be done to fix the problem?
spaghettiwestern
I have all of the latest updates for XP installed including all of the DST updates. I have DST enabled in XP. I also tried this on two other workstations one of which was a 2003 server with the same results.
spaghettiwestern
A: 

Thanks for your help "Ned Batchelder", much appreciated.

This is the closest answer I could find to my question and according to the python developers this is normal and acceptable behavior see the following thread http://bytes.com/topic/python/answers/655606-python-2-5-1-broken-os-stat-module However in this thread they are referring to the os.stat module specifically. They are basically saying that the hour difference has to do with how Windows vs Python calculates DST time and that both Windows and Python are correct.

To solve my problem I have since used tarfile to first tar all of my files and then used zipfile to compress my tarfile. The tarfile module preserves file timestamps correctly. The other problem I found with the zipfile module is that when extracting a file it updates the "Date Modified" time to the current date and time rather than preserving the original date and time of the file that is being extracted.

spaghettiwestern