Donate Share

Python for Windows extensions

Tracker: Bugs

5 PyTime's .msec method always returns 0 - ID: 2209864
Last Update: Comment added ( davidfraser )

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.


David Fraser ( davidfraser ) - 2008-10-30 14:36

5

Open

None

Nobody/Anonymous

win32

None

Public


Comments ( 2 )

Date: 2008-10-30 15:06
Sender: davidfraser

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


Date: 2008-10-30 14:47
Sender: davidfraser

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)


Attached File ( 1 )

Filename Description Download
pywin32-pytime-calc-msec.patch Patch to PyTime.cpp to calculate milliseconds Download

Change ( 1 )

Field Old Value Date By
File Added 299550: pywin32-pytime-calc-msec.patch 2008-10-30 15:06 davidfraser