file time to dos time

Nick Collier nick at src.uchicago.edu
Wed Dec 22 13:37:58 EST 1999


I figured this out. The problem was that dos time begins at 01/01/1980.
I found comparable code in the Java java.util.zip.ZipEntry class and
used that as a model. For anyone watching at home, the code is as
follows:

 ttuple = time.localtime(os.path.getmtime(path))
 year = ttuple[0]

 if (year < 1980):
     entry.time = (1 << 21) | (1 << 16)
 else:
     year = year - 1980
     entry.time = (year << 25 | ttuple[1] << 21 |
                 ttuple[2] << 16 | ttuple[3] << 11 | ttuple[4] << 5 |
                 ttuple[5] >> 1)

where entry.time is in dos time format. By the way, I needed to know the
dos time as this is the appropriate format when writing a zip archive.

Nick

Gordon McMillan wrote:
> 
> Nick Collier writes:
> >
> > I'm trying to convert the results of os.path.getmtime(path) -
> > last modification time in seconds since the epoch - to the dos
> > time format which I think is a 36 bit number with bit fields for
> > the year, month, day, hour, seconds. I'm coming close with some
> > guessed at bitwise arthimetic, but can't get the year correct.
> > Any suggestions?
> 
> Since what you're doing is platform specific, why not use
> DosDateTimeToTime from the Win32 extensions?
> 
> MSVC Help text says (hope the paste comes out looking OK):
> wFatDate
> Specifies the MS-DOS date. The date is a packed 16-bit value
> with the following format:
> {PRIVATE}Bits 
> Contents 
> 
> 0-4 
> Day of the month (1-31) 
> 
> 5-8 
> Month (1 = January, 2 = February, and so on) 
> 
> 9-15 
> Year offset from 1980 (add 1980 to get actual year) 
> 
> wFatTime
> Specifies the MS-DOS time. The time is a packed 16-bit value with
> the following format:
> {PRIVATE}Bits 
> Contents 
> 
> 0-4 
> Second divided by 2 
> 
> 5-10 
> Minute (0-59) 
> 
> 11-15 
> Hour (0-23 on a 24-hour clock) 
> 
> 
> - Gordon

-- 

Nick Collier
nick at src.uchicago.edu
Social Science Research Computing
University of Chicago
Chicago, IL 60637



More information about the Python-list mailing list