[Python-Dev] zipimport, round 3 (or would that be that 37?)

Skip Montanaro skip@pobox.com
Mon, 9 Dec 2002 08:36:01 -0600


    Just> However, I'd be *very* grateful for a recipe that translates a dos
    Just> time/date (it's two shorts, zipfile.py contains code that
    Just> translates it to a time tuple) to something compatible with
    Just> st.st_mtime. And it can't use the time module ;-)

Looking at the code in zipfile.py which converts a time tuple to DOS date
and time shorts (I didn't see code that went the other way), it appears that
dosdate is 7 bits for the year, 4 bits for the month and 5 bits for the day.
Dostime looks like 5 bits for the hour, 6 bits for the minute and 5 bits for
half the number of seconds.  You should be able to extract those chunks into
a tm structure then call the C lib mktime() function to convert to a time in
seconds since the epoch (this is just off the top of my head):

    stm = malloc(sizeof(struct tm));
    stm->tm_sec = (dostime & 0x1f) * 2;
    stm->tm_min = (dostime >> 5) & 0x3f;
    stm->tm_hour = dostime >> 11);
    stm->tm_mday = dosdate & 0x1f;
    stm->tm_mon = (dosdate >> 5) & 0xf;
    stm->tm_year = dosdate >> 9;
    mtime = mktime(stm);

Here's the layout of dosdate and dostime as I understand it from reading the
zipfile code:

    dosdate = yyyyyyymmmmddddd
    dostime = hhhhhmmmmmmsssss

where "sssss" is actually the seconds divided by 2.  I'll let others check
my code.  I have to dash off to a class.

Skip