[python-win32] com events while running wx main loop

Thomas Heller theller at ctypes.org
Fri Apr 20 20:20:02 CEST 2007


Christian K. schrieb:
> 
> I successfully read out the Outlook inbox using comtpyes but finally 
> switched back to pywin32 because I was not able to interprete the value 
> of MailItem.CreationTime,
> e.g:
> obj.Session.Folders['mail.server.com'].Folders['INBOX'].Items.Item(1).CreationTime
> which looks like a posix timestamp which dates back to the 70th. I guess 
> pywin32 is doing some conversion there and comtypes is not?

Here is a little hack that automatically converts COM date/time objects (doubles)
from or to Python datetime objects.  You have to insert this code near the top of the
generated comtypes.gen._00062FFF_0000_0000_C000_000000000046_0_9_1 module (this is the module
that is imported by comtypes.gen.Outlook on my system.  BTW I have outlook 2002, if you
have a different version the module name may be different).

It replaces the c_double datetype used in that module with a subclass that converts
automatically in COM method calls:

<code>
import datetime
_com_null_date = datetime.datetime(1899, 12, 30, 0, 0, 0)

class com_datetime(c_double):
    """COM represents date/time values as double.  The value is the number
    of days since 1899, Dec 30."""
    def __ctypes_from_outparam__(self):
        # When an [out] parameter is retrived from a COM method call,
        # this special method is called.  Return a datetime object.
        return datetime.timedelta(seconds=self.value * 86400.) + _com_null_date

    @classmethod
    def from_param(cls, value):
        if isinstance(value, (float, int, long)):
            # convert a unix timestamp into a datetime instance.
            value = datetime.datetime.fromtimestamp(value)
        if isinstance(value, datetime.datetime):
            # Convert a datetime instance into a COM date.
            delta = value - _com_null_date
            # a day has 24 * 60 * 60 = 86400 seconds
            com_days = delta.days + (delta.seconds + delta.microseconds * 1e-6) / 86400.
            return cls(com_days)
        raise TypeError("need timestamp or datetime object")

c_double = com_datetime
<code/>

Thomas



More information about the Python-win32 mailing list