Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Proper conversion of timestamp

1,564 views
Skip to first unread message

Igor Korot

unread,
Mar 4, 2014, 3:57:08 PM3/4/14
to pytho...@python.org
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:

http://stackoverflow.com/questions/12458595/convert-epoch-timestamp-in-python

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.

MRAB

unread,
Mar 4, 2014, 4:38:06 PM3/4/14
to pytho...@python.org
Are you using Python 2? If yes, then try dividing by 1000.0.

Mark Lawrence

unread,
Mar 4, 2014, 4:44:04 PM3/4/14
to pytho...@python.org
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:
>
> http://stackoverflow.com/questions/12458595/convert-epoch-timestamp-in-python
>
> 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.

--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com


Igor Korot

unread,
Mar 4, 2014, 4:46:21 PM3/4/14
to pytho...@python.org
MRAB,


On Tue, Mar 4, 2014 at 1:38 PM, MRAB <pyt...@mrabarnett.plus.com> wrote:
On 2014-03-04 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:

http://stackoverflow.com/questions/12458595/convert-epoch-timestamp-in-python

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.

Are you using Python 2? If yes, then try dividing by 1000.0.

Yes, I'm using python 2.7.
But dividing by 1000 will give the precision in seconds, i.e. "YYYY-MM-DD HH:MM:SS".

What I want is to have this: "YYYY-MM-DD HH:MM:SS.XXX", where "XXX" is a milliseconds.

Thank you.
 

--
https://mail.python.org/mailman/listinfo/python-list

Mark Lawrence

unread,
Mar 4, 2014, 4:45:50 PM3/4/14
to pytho...@python.org
On 04/03/2014 21:38, MRAB wrote:
> On 2014-03-04 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:
>>
>> http://stackoverflow.com/questions/12458595/convert-epoch-timestamp-in-python
>>
>>
>> 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.
>>
> Are you using Python 2? If yes, then try dividing by 1000.0.
>

You learn something new every day, I wasn't aware that you could
multiply or divide timestamps.

Igor Korot

unread,
Mar 4, 2014, 4:55:11 PM3/4/14
to pytho...@python.org
Hi, Mark,


On Tue, Mar 4, 2014 at 1:44 PM, Mark Lawrence <bream...@yahoo.co.uk> 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:

http://stackoverflow.com/questions/12458595/convert-epoch-timestamp-in-python

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.

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?

Thank you.
 

--
My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language.

Mark Lawrence

---
This email is free from viruses and malware because avast! Antivirus protection is active.
http://www.avast.com


Chris Angelico

unread,
Mar 4, 2014, 4:55:43 PM3/4/14
to pytho...@python.org
On Wed, Mar 5, 2014 at 8:46 AM, Igor Korot <ikor...@gmail.com> wrote:
>> Are you using Python 2? If yes, then try dividing by 1000.0.
>
>
> Yes, I'm using python 2.7.
> But dividing by 1000 will give the precision in seconds, i.e. "YYYY-MM-DD
> HH:MM:SS".

Did you notice the bit at the end there? Try dividing by 1000.0, see
if that's any different.

ChrisA

Igor Korot

unread,
Mar 4, 2014, 4:57:56 PM3/4/14
to pytho...@python.org
Mark,


On Tue, Mar 4, 2014 at 1:45 PM, Mark Lawrence <bream...@yahoo.co.uk> wrote:
On 04/03/2014 21:38, MRAB wrote:
On 2014-03-04 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:

http://stackoverflow.com/questions/12458595/convert-epoch-timestamp-in-python


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.

Are you using Python 2? If yes, then try dividing by 1000.0.

You learn something new every day, I wasn't aware that you could multiply or divide timestamps.

Of course you can. Its just the number.
And this is exactly what happens on the stackoverflow question I referenced in the OP.
Problem is I want the milliseconds in the datetime object.

Chris Angelico

unread,
Mar 4, 2014, 5:03:05 PM3/4/14
to pytho...@python.org
On Wed, Mar 5, 2014 at 8:55 AM, Igor Korot <ikor...@gmail.com> wrote:
>
> 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?

Read elsewhere in the thread, two people have explained this already.

ChrisA

Ethan Furman

unread,
Mar 4, 2014, 5:05:35 PM3/4/14
to pytho...@python.org
On 03/04/2014 01:55 PM, Igor Korot wrote:
>
> Now the question I have is: how do I properly convert this timestamp
> into the datetime object with the milliseconds?

And Mark's point is: How do the docs say to do it? What fails when you
try it that way?

--
~Ethan~

Mark Lawrence

unread,
Mar 4, 2014, 5:46:38 PM3/4/14
to pytho...@python.org
On 04/03/2014 21:55, Igor Korot wrote:
>
> But this particular question is easy.
>

If it's easy why can't you answer it via the docs rather than ask here?

MRAB

unread,
Mar 4, 2014, 7:42:51 PM3/4/14
to pytho...@python.org
On 2014-03-04 21:55, Igor Korot wrote:
> Hi, Mark,
>
>
> On Tue, Mar 4, 2014 at 1:44 PM, Mark Lawrence <bream...@yahoo.co.uk
> <mailto:bream...@yahoo.co.uk>> 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:
>
> http://stackoverflow.com/__questions/12458595/convert-__epoch-timestamp-in-python
> <http://stackoverflow.com/questions/12458595/convert-epoch-timestamp-in-python>
>
> 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
17:37:58'.)

> 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
digits).

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
digits.

Igor Korot

unread,
Mar 4, 2014, 9:13:43 PM3/4/14
to pytho...@python.org
Hi, Mark,

On Tue, Mar 4, 2014 at 2:46 PM, Mark Lawrence <bream...@yahoo.co.uk> wrote:
> On 04/03/2014 21:55, Igor Korot wrote:
>>
>>
>> But this particular question is easy.
>>
>
> If it's easy why can't you answer it via the docs rather than ask here?

What I meant to say is: it is easy for all you people and it is
definitely easier than
trying to figure out how to process and display different dates.

Thank you.

>
>
> --
> My fellow Pythonistas, ask not what our language can do for you, ask what
> you can do for our language.
>
> Mark Lawrence
>
> ---
> This email is free from viruses and malware because avast! Antivirus
> protection is active.
> http://www.avast.com
>
>
> --
> https://mail.python.org/mailman/listinfo/python-list

Igor Korot

unread,
Mar 4, 2014, 9:19:48 PM3/4/14
to pytho...@python.org
MRAB,

On Tue, Mar 4, 2014 at 4:42 PM, MRAB <pyt...@mrabarnett.plus.com> wrote:
> On 2014-03-04 21:55, Igor Korot wrote:
>>
>> Hi, Mark,
>>
>>
>> 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
> 17:37:58'.)

Well it is this particular timestamp.
But I have a lot of files to process and some do have a timestamp with
the milliseconds.

>
>
>> 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
> digits).
>
> 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
> digits.

Right.
The question is: how to get the number of milliseconds out of timestamp?
Once again: I can get the datetime object with the seconds precision
by dividing it on 1000. But that will produce the datetime object with
the seconds precision.

I can actually produce another timestamp with the milliseconds from a
different file...

Ben Finney

unread,
Mar 4, 2014, 9:33:47 PM3/4/14
to pytho...@python.org
Igor Korot <ikor...@gmail.com> writes:

> On Tue, Mar 4, 2014 at 4:42 PM, MRAB <pyt...@mrabarnett.plus.com> wrote:
> >> 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
> > 17:37:58'.)
>
> Well it is this particular timestamp.

Which is what you presented as “what I have”.

> But I have a lot of files to process and some do have a timestamp with
> the milliseconds.

So, if you want help with such timestamps, you'll need to present a real
example (or preferably several exmaples) of timestamps that need this
handling.

> Right.
> The question is: how to get the number of milliseconds out of
> timestamp?

>From the timestamp you showed: The number of milliseconds is zero, since
it's an integer. That datatype will *always* have zero milliseconds.

> Once again: I can get the datetime object with the seconds precision
> by dividing it on 1000. But that will produce the datetime object with
> the seconds precision.

Because that's the data you're showing us: A timestamp with an integer
number of seconds.

> I can actually produce another timestamp with the milliseconds from a
> different file...

Then you won't be able to represent it as an integer number of seconds.
What are you receiving in the data? What data type is it?

--
\ “He who allows oppression, shares the crime.” —Erasmus Darwin, |
`\ grandfather of Charles Darwin |
_o__) |
Ben Finney

Mark Lawrence

unread,
Mar 4, 2014, 9:39:34 PM3/4/14
to pytho...@python.org
On 05/03/2014 02:13, Igor Korot wrote:
> Hi, Mark,
>
> On Tue, Mar 4, 2014 at 2:46 PM, Mark Lawrence <bream...@yahoo.co.uk> wrote:
>> On 04/03/2014 21:55, Igor Korot wrote:
>>>
>>>
>>> But this particular question is easy.
>>>
>>
>> If it's easy why can't you answer it via the docs rather than ask here?
>
> What I meant to say is: it is easy for all you people and it is
> definitely easier than
> trying to figure out how to process and display different dates.
>
> Thank you.
>

You've never once referred to anything that you've read in the docs but
don't understand, so how can we help you?

Gregory Ewing

unread,
Mar 4, 2014, 11:06:47 PM3/4/14
to
Igor Korot wrote:
> What I have is a timestamp which reads: 1289410678L.
>
> 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.

Divide the timestamp by 1000.0 to give floating point
seconds, and then use datetime.fromtimestamp().

>>> d = datetime.datetime.fromtimestamp(1289410678 / 1000.0)
>>> d
datetime.datetime(1970, 1, 16, 10, 10, 10, 678000)
>>> d.strftime("%Y-%m-%d-%H:%M:%S.%f")[:-3]
'1970-01-16-10:10:10.678'

--
Greg

MRAB

unread,
Mar 4, 2014, 11:11:05 PM3/4/14
to pytho...@python.org
On 2014-03-05 02:33, Ben Finney wrote:
> Igor Korot <ikor...@gmail.com> writes:
>
>> On Tue, Mar 4, 2014 at 4:42 PM, MRAB <pyt...@mrabarnett.plus.com> wrote:
>> >> 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
>> > 17:37:58'.)
>>
>> Well it is this particular timestamp.
>
> Which is what you presented as “what I have”.
>
I'd expect all of the timestamps to be the same type. That example is
an integer (a 'long' to be exact).

>> But I have a lot of files to process and some do have a timestamp with
>> the milliseconds.
>
> So, if you want help with such timestamps, you'll need to present a real
> example (or preferably several exmaples) of timestamps that need this
> handling.
>
>> Right.
>> The question is: how to get the number of milliseconds out of
>> timestamp?
>
> From the timestamp you showed: The number of milliseconds is zero, since
> it's an integer. That datatype will *always* have zero milliseconds.
>
>> Once again: I can get the datetime object with the seconds precision
>> by dividing it on 1000. But that will produce the datetime object with
>> the seconds precision.
>
> Because that's the data you're showing us: A timestamp with an integer
> number of seconds.
>
And in Python 2, an integer divided by an integer gives an integer.
0 new messages