You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ZipFile.write fails with bad modification time #45243
Running on Windows XP against a networked file server with Windows 2003 I have a file which due to some obscure reason it has a negative modification time as returned by Python os functions. When I look at the file in the explorer the modification time column is blank, and when I view the properties of the file there the modification time is set to the 25th of April of 1906 at 2:03:44.
If I try to zip this file the zip creation fails with a Value error when the file's st_mtime value is passed to time.localtime().
I'm attaching a debug session log along with a possible patch to avoid such problems, though maybe the function that returns st_mtime should never return negative values because other library functions expect zero or positive values too.
In the log you will see that it is a custom rastertech\zipfile.py file that is failing. This is just the SVN development version of zipfile along with some modifications since changes like 1739648 seem to be very low priority. The patch is generated against a clean SVN version.
At the moment I don't have a Windows machine available, but on my Mac,
time.localtime doesn't seem to mind interpreting negative input values.
So I doubt that forcing timestamps to be non-negative is the way to fix
this.
I poked around a bit in the current 2.6 source, and it seems to me that
in this case the Windows XP localtime function doesn't like a timestamp
value that was generated by the same platform's stat function. It seems
like raising a ValueError is the correct thing for the time module to
do, since it relies on the platform to do the conversions; what should
zipfile.writestr do? I don't like the idea of silently using some dummy
timestamp if the conversion of os.stat results fails, and I can't think
of an alternative at the moment.
@serhiy-storchaka I think this is already addressed in the current versions of Python.
>>> os.stat("hello.txt")os.stat_result(st_mode=33188, st_ino=31001686, st_dev=16777233, st_nlink=1, st_uid=501, st_gid=0, st_size=0, st_atime=-2019621600, st_mtime=-2019621600, st_ctime=1720029468)>>> os.stat("hello.txt").st_mtime-2019621600.0>>> zi.from_file("hello.txt")Traceback (most recent call last): File "<stdin>", line 1, in <module>AttributeError: 'ZipFile' object has no attribute 'from_file'>>> info = zipfile.ZipInfo.from_file("hello.txt")Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/zipfile.py", line 517, in from_file zinfo = cls(arcname, date_time) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/zipfile.py", line 361, in __init__ raise ValueError('ZIP does not support timestamps before 1980')ValueError: ZIP does not support timestamps before 1980>>> zi.write("hello.txt")Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/zipfile.py", line 1727, in write zinfo = ZipInfo.from_file(filename, arcname, File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/zipfile.py", line 517, in from_file zinfo = cls(arcname, date_time) File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/zipfile.py", line 361, in __init__ raise ValueError('ZIP does not support timestamps before 1980')ValueError: ZIP does not support timestamps before 1980
I checked on windows, and on 3.14 and earlier, I got OSError, but on 3.15 I get "ZIP does not support timestamps before 1980" and passing strict_timestamps=False will fix that.
Fix was presumably #143463, which fixes negative timestamps on windows.
Activity
gradha commentedon Jul 25, 2007
Running on Windows XP against a networked file server with Windows 2003 I have a file which due to some obscure reason it has a negative modification time as returned by Python os functions. When I look at the file in the explorer the modification time column is blank, and when I view the properties of the file there the modification time is set to the 25th of April of 1906 at 2:03:44.
If I try to zip this file the zip creation fails with a Value error when the file's st_mtime value is passed to time.localtime().
I'm attaching a debug session log along with a possible patch to avoid such problems, though maybe the function that returns st_mtime should never return negative values because other library functions expect zero or positive values too.
In the log you will see that it is a custom rastertech\zipfile.py file that is failing. This is just the SVN development version of zipfile along with some modifications since changes like 1739648 seem to be very low priority. The patch is generated against a clean SVN version.
gradha commentedon Jul 25, 2007
File Added: ignore_negative_modification_time.txt
gradha commentedon Aug 26, 2007
Any progress report on this issue, please?
alanmcintyre commentedon Dec 23, 2007
At the moment I don't have a Windows machine available, but on my Mac,
time.localtime doesn't seem to mind interpreting negative input values.
So I doubt that forcing timestamps to be non-negative is the way to fix
this.
I poked around a bit in the current 2.6 source, and it seems to me that
in this case the Windows XP localtime function doesn't like a timestamp
value that was generated by the same platform's stat function. It seems
like raising a ValueError is the correct thing for the time module to
do, since it relies on the platform to do the conversions; what should
zipfile.writestr do? I don't like the idea of silently using some dummy
timestamp if the conversion of os.stat results fails, and I can't think
of an alternative at the moment.
serhiy-storchaka commentedon Apr 28, 2012
An alternative is to use the current time, as for stdin.
[-]ZipFile.write fails with bad modification time[/-][+]Loveubylulu@gmail.com[/+][-]ZipFile.write fails with bad modification time[/-][+]Loveubylulu@gmail.com[/+]tirkarthi commentedon Jul 28, 2019
Linsey Alvarez, I am not sure how it's related to email. So I am removing the email tag and changing the subject back.
1 remaining item
[-]Loveubylulu@gmail.com[/-][+]ZipFile.write fails with bad modification time[/+]Akasurde commentedon Jul 3, 2024
@serhiy-storchaka I think this is already addressed in the current versions of Python.
msullivan commentedon Oct 13, 2026
I checked on windows, and on 3.14 and earlier, I got
OSError, but on 3.15 I get "ZIP does not support timestamps before 1980" and passingstrict_timestamps=Falsewill fix that.Fix was presumably #143463, which fixes negative timestamps on windows.
I think this can be closed.