4m1nh4j1
4m1nh4j1

Reputation: 4356

Timestamp in a json file

I am testing this RESTFul whois API .

I am confused if the date is in unix timestamp format . But I don't think so, because :

print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int("1340578800000")))

gave me :

 ValueError: timestamp out of range for platform time_t

the date can be found here

updated: 1340578800000 

in this file :

http://www.restfulwhois.com/example

I can't find any email or support in the website, that's why I am asking here . What do you think ?

Upvotes: 1

Views: 4567

Answers (4)

Brian Cain
Brian Cain

Reputation: 14619

The value you have is the number of milliseconds since the epoch and time.localtime expects the number of seconds.

[from gmtime()]: Convert a time expressed in seconds since the epoch to ...

Note that while the string you recover is no different between these two, the more generally applicable solution is probably to do floating-point division in order to preserve the milliseconds.

>>> time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int("1340578800000") / 1000))
'2012-06-24 18:00:00'
>>> time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int("1340578800000") / 1000.))
'2012-06-24 18:00:00'

Upvotes: 2

alko
alko

Reputation: 48357

Those are milliseconds since epoch:

>>> print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int("1340578800000")/1000))
2012-06-25 03:00:00

Most ofted you will get milliseconds from JavaScript, returned by getTime.

And beware of timezone and local time variances, as show by simoultaneous yet different answers in this topic. For a UTC date use gmtime instead of localtime:

>>> print time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(int("1340578800000")/1000))
2012-06-24 23:00:00

Upvotes: 2

Pedro Werneck
Pedro Werneck

Reputation: 41928

It's in miliseconds, not seconds. Just divide it by 1000 before formatting:

>>> time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int("1340578800000") / 1000))
'2012-06-24 20:00:00'

Upvotes: 1

jabaldonedo
jabaldonedo

Reputation: 26582

Python time.localtime expects the timestamp to be in seconds, you are passing it in milliseconds, so you will have to divide by 1000 in order to get it in seconds.

Change your code to:

>>> print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int("1340578800000")/1000))
 '2012-06-24 20:00:00'

Upvotes: 1

Related Questions