Use a PyTime value

Introduction

The pywin32 extensions package includes an external PyTime type to handle time values coming back from COM objects, say from an Excel spreadsheet or via ADO. It was built before the Python standard library had the datetime types. If you need to do anything with the value you'll need to translate it into something the datetime types can use.

There are two approaches here. If your datetime value is "sane" (ie is anywhere between 1970 and 2038) you can use the datetime's .from_timestamp constructor method. If not, you'll have to use PyTime's individual properties to construct the datetime object

Sane Dates: 1970 - 2038

This approach relies on the fact that Unix time is traditionally stored as an integer offset from 1st Jan 1970 up to the limit of an integer. The PyTime object has a converter to return its integer equivalent, and the datetime type has a .fromtimestamp constructor.

import time
import datetime
import pywintypes

now_seconds = time.time ()
now_pytime = pywintypes.Time (now_seconds)
now_datetime = datetime.datetime.fromtimestamp (int (now_pytime))
print now_datetime

All Dates

This technique should work for all dates and makes use of the PyTime's individual elements representing the day, month, year etc. and the conventional datetime constructor using those elements.

import time
import datetime
import pywintypes

now_seconds = time.time ()
now_pytime = pywintypes.Time (now_seconds)
now_datetime = datetime.datetime (
  year=now_pytime.year,
  month=now_pytime.month,
  day=now_pytime.day,
  hour=now_pytime.hour,
  minute=now_pytime.minute,
  second=now_pytime.second
)
print now_datetime