Search code examples
pythonpywin32

Using pywin32 DosDateTimeToTime to unpack DOS packed time


Has anyone used pywin32 pywintypes.DosDateTimetoTime to convert a DOS packed date/time structure to a readable time format in Python?

I am unable find much documentation on how to use this function, what parameters are required and in what format.

I'm working on a script to extract files from an old DOS backup file, basically trying to replicate the old DOS restore command. I'm working on extracting files based off the format of a backup file found http://www.ibiblio.org/pub/micro/pc-stuff/freedos/files/dos/restore/brtecdoc.htm

Thanks, Jay


Solution

  • It takes two parameters (16 bit integers) which are identical to the first two parameters of DosDateTimeToFileTime

    You can see that in the source code PyWinTypesmodule.cpp for pywin32:

    static PyObject *PyWin_DosDateTimeToTime(PyObject *self, PyObject *args)
    { 
        WORD wFatDate, wFatTime;
        if (!PyArg_ParseTuple(args, "hh", (WORD *)&wFatDate, (WORD *)&wFatTime))
            return NULL;
        FILETIME fd;
        If (!DosDateTimeToFileTime(wFatDate, wFatTime, &fd))
          return PyWin_SetAPIError("DosDateTimeToFileTime");
    }
    

    Those have to be of the format described in this MSDN link with the relevant parts copied below for convenience:

    wFatDate [in]
    The MS-DOS date. The date is a packed value with the following format.
        Bits    Description
        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 [in]
    The MS-DOS time. The time is a packed value with the following format.
        Bits    Description
        0-4     Second divided by 2
        5-10    Minute (0–59)
       11-15    Hour (0–23 on a 24-hour clock)