3

我从 PostgreSQL 获得了一条记录,它的类型是timestamp without time zone

我正在使用 psycopg2

如果我datetime使用timestamp with time zone. 但是,目前情况并非如此。 http://initd.org/psycopg/docs/usage.html#time-zones-handling

我意识到我正在获得浮点类型。

我怎么能假设从给定的浮点数中获得价值?

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'])
4

2 回答 2

3

您获得的浮点值似乎是从纪元开始的毫秒数。所以这似乎给了你正在寻找的东西:

#!/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()
                    )
于 2010-11-11T18:06:59.000 回答
0

为新人更新。至少从 psycopg2 2.7.1 开始,“没有时区的时间戳”返回一个日期时间。

所以your_time.strftime("%a, %d %b %Y %H:%M:%S +0000")现在就可以了。

于 2018-01-05T18:39:36.210 回答