Menu

#387 PyTime's .msec method always returns 0

open
nobody
win32 (141)
5
2008-10-30
2008-10-30
No

PyTime stores times internally as DATE objects, which support fractional second resolution (since they are just a double number of days).

However the method to retrieve attributes including msec uses the Win32 VariantTimeToSystemTime function to convert the m_time to a SYSTEMTIME.

This function is known not to convert the fractional part of seconds (see http://support.microsoft.com/kb/297463, and http://www.codeproject.com/KB/datetime/SysTimeToVarTimeWMillisec.aspx for some example workaround code).

So although the fractional part is actually stored in the PyTime object, it cannot be retrieved.

Discussion

  • David Fraser

    David Fraser - 2008-10-30

    A fairly simple workaround is to convert the PyTime directly to a float. For example, the following code converts to datetime:

    def WinPyTimeToDate(pytime):
    """Converts a pywintypes.PyTime object into a date object"""
    microseconds = int(((float(pytime)*24*60*60) % 1)*1000000)
    return date(pytime.year, pytime.month, pytime.day, pytime.hour, pytime.minute, pytime.second, microseconds)

     
  • David Fraser

    David Fraser - 2008-10-30

    Patch to PyTime.cpp to calculate milliseconds

     
  • David Fraser

    David Fraser - 2008-10-30

    I've attached a very simple patch that calculates the .msec value on request.
    Note that since .msec has never returned anything but 0, and the builtin datetime object has a microsecond attribute, it may be worthwhile to repurpose this .msec attribute to mean "microseconds" rather than "seconds"

    It would also be worthwhile to patch the various Time constructors to ensure that they preserve sub-second resolution if given (e.g. pywintypes.Time(time.time()) currently discards it)
    File Added: pywin32-pytime-calc-msec.patch

     
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.