Proper conversion of timestamp

MRAB python at
Wed Mar 5 01:42:51 CET 2014

On 2014-03-04 21:55, Igor Korot wrote:
> Hi, Mark,
> On Tue, Mar 4, 2014 at 1:44 PM, Mark Lawrence <breamoreboy at
> <mailto:breamoreboy at>> wrote:
>     On 04/03/2014 20:57, Igor Korot wrote:
>         Hi, ALL,
>         I'm getting this:
>         timestamp out of range for platform localtime()/gmtime() function
>         trying to convert the timestamp with milliseconds into the
>         datetime object.
>         The first hit of Google gives me this:
>         <>
>         but the solution described is not good for me since it does not
>         gives
>         me the milliseconds value.
>         How do I get the proper datetime value including milliseconds
>         from the
>         timestamp?
>         Thank you.
>     You have a long record of asking timestamp related questions so you
>     should know where the docs are that provide the answer to this
>     question.  I'll leave you to go off and read them.  If you don't
>     understand them, please cut and paste your code here, state what you
>     expected to happen, what actually happened, including any traceback
>     if applicable, and then we'll be happy to point you the error of
>     your ways.
> Working with the dates is not that easy and not just in Python.
> There are too many different formatting involved with many different
> representation.
> And on top of it it is possible to use one system in completely
> different environment.
> But this particular question is easy.
> What I have is a timestamp which reads: 1289410678L.
That's an integer. It looks like the timestamp is a whole number of
seconds, so the number of milliseconds is 0. (I make it '2010-11-10

> Trying to convert this into the datetime object in Python using:
> import datetime
> datetime.datetime.fromtimestamp( stamp )
> produces the error: timestamp out of range for platform
> localtime()/gmtime() function.
> This is because this timestamp is not in seconds, but rather in
> milliseconds.
> Now the question I have is: how do I properly convert this timestamp
> into the datetime object with the milliseconds?
Using the datetime's .strftime method, you can include the number of
microseconds in the format with '%f' (it'll write the microseconds as 6

If you want it to the nearest millisecond (the timestamp would be a
float), you could round the timestamp to 3 decimal places, use the '%f'
in the format, and then truncate the string result to remove the last 3

More information about the Python-list mailing list