psycopg2处理没有时区的时间戳 [英] psycopg2 to deal with timestamp without time zone
问题描述
我从PostgreSQL获得一条记录,其类型为没有时区的时间戳
I obtain a record from PostgreSQL, which its type is timestamp without time zone
我正在使用psycopg2
I am using psycopg2
如果我将带有时区的时间戳记使用,则可能会得到 datetime
对象.但是,目前情况并非如此. http://initd.org/psycopg/docs/usage.html#time-zones-handling
I might get datetime
object if I am using timestamp with time zone
. But, currently, this is not the case.
http://initd.org/psycopg/docs/usage.html#time-zones-handling
我意识到我正在获取浮点类型.
I realize I am getting float type instead.
我应该如何从给定的浮点数中获取价值?
How can I suppose to get value like from the given float?
Jan 07, 2010
16:38:49
connection.cursor.execute(sql, data)
records = connection.cursor.fetchall()
# In PostgreSQL, type is "timestamp without time zone"
# <type 'float'>
print type(record['_start_timestamp'])
# 1.28946608161e+12
print record['_start_timestamp']
# TypeError: argument must be 9-item sequence, not float
print time.strftime("%a, %d %b %Y %H:%M:%S +0000", record['_start_timestamp'])
推荐答案
您获得的浮点值似乎是从纪元开始的毫秒数.因此,这似乎可以满足您的需求:
The float value that you're getting seems to be milliseconds from the epoch. So this seems to give what you're looking for:
#!/usr/bin/python
from datetime import datetime
import time
your_time = 1.28946608161e+12
print time.strftime("%a, %d %b %Y %H:%M:%S +0000",
datetime.fromtimestamp(your_time/1000).timetuple()
)
这篇关于psycopg2处理没有时区的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!