datetime in microseconds

John Machin sjmachin at
Mon Aug 20 15:15:46 CEST 2007

On Aug 20, 9:52 pm, mroelo... at wrote:
> Hi I have a time in microseconds, for example 0x8C905CBA7F84AF4. I
> want this to a normal view in hh:mm:ss DD:MM:YYYY. I tried with
> datetime, but it only takes a max of 1000000 microseconds is there
> another solution?

Your question can be interpreted in two possible ways:

1. You have an interval or duration (independent of a calendar point)
and you want to express it in years, months, days, hours, etc. This is
not possible, due to the variable number of days in a month. The best
that you can do is express it as days, hours, etc.

>>> microsecs = 0x8C905CBA7F84AF4
>>> secs = microsecs // 1000000 # or round to nearest if you prefer
>>> mins, secs = divmod(secs, 60)
>>> hrs, mins = divmod(mins, 60)
>>> days, hrs = divmod(hrs, 24)
>>> days, hrs, mins, secs
(7326893L, 11L, 1L, 16L)

2. You want to know the (Gregorian) calendar point that is
0x8C905CBA7F84AF4 microseconds after some epoch. In this case you need
to specify what the epoch is. Then you can try something like:

>>> datetime.datetime.fromordinal(1) + datetime.timedelta(microseconds=microsecs
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: date value out of range
>>> # Whoops!
>>> years_approx = days / 365.25
>>> years_approx

Hmmm, one of us seems to be missing something ...

More information about the Python-list mailing list