views:

20

answers:

1

I am facing problem in retrieving long value from PostgreSQL

I use the following SQL command :

SELECT *, (extract(epoch FROM start_timestamp) * 1000) FROM lot 
WHERE EXTRACT(EPOCH FROM lot.start_timestamp) * 1000 >=1265299200000 AND 
EXTRACT(EPOCH FROM lot.start_timestamp) * 1000 <=1265990399999 
ORDER BY start_timestamp DESC limit 9 offset 0 

I am interested in the unix timestamp in ms

I run this command manually through pgAdmin.

From pgAdmin, I saw 1265860762817 for column (extract(epoch FROM start_timestamp) * 1000)

However, when I retrieve through psycopg2, here is what I use :

cur = conn.cursor()
cur.execute(sql)
rows = cur.fetchall()
for row in rows :
    print row[3]

And here is what I get :

1.26586076282e+12

I pass this value, from python cgi to JavaScript in JSON

If I do manual conversion in JavaScript

1.26586076282e+12

// summary.date is 1.26586076282e+12
var timestamp = summary.date * 1;
// Get 1265860762820
alert(timestamp);

There are 3ms error difference

1265860762817
1265860762820-
==============
            3
==============

How can I avoid such error? I can I make sure psycopg2 is returning me 1265860762817, not 1.26586076282e+12

+1  A: 

I have the same results using both psycopg2 and pygres. While this select returns float value you can modify your code to format returned value:

def format_float_fld(v):
    #return str(v)
    return ('%20.0f' % (v)).strip()

If you use str(s) then you will get scientific notation.

You can also change query to return bigint instead of float:

 SELECT (EXTRACT(EPOCH FROM TIMESTAMP '2010-02-16 20:38:40.123') * 1000)::bigint;
Michał Niklas