通过删除 unix 时间戳中的“000000”来插入

Insertion by removing '000000' in the unix timestamp

我正在为 python 插入一些来自 API FLASK 中的 GET 的数据,例如 JSON,出于某种原因,当 python 将插入此数据或也许是 postgres 本身,它正在删除 000000

数据json:

{
"datecollect": 1585983000000
}

插入 py:

    conn.execute('''INSERT INTO "TBLNAME"
    (value1,value2,value3,value4,value5)
    VALUES ('{0}','{1}','{2}',to_timestamp({3}) AT TIME ZONE 'UTC',current_timestamp)'''.
    format(server,forecastmemory,levelerror,datecollect))

原始日期:

select to_timestamp(1585983000000) AT TIME ZONE 'UTC' --> 52227-10-20 17:20:00

python 插入 pgsql 的日期:

select to_timestamp(1585983) AT TIME ZONE 'UTC' --> 1970-01-19 08:33:03

转换正确:

即使使用网站的时区,它 returns 也有问题 select to_timestamp(1585983000000) AT TIME ZONE 'GMT-03:00' --> 52227-10-20 20:20:00

您的时间戳值 (datecollect) 显然以毫秒为单位;你只需要将它除以 1000 就可以将它从毫秒转换为秒:

SELECT TO_TIMESTAMP(1585983000000/1000) AT TIME ZONE 'UTC'

输出:

2020-04-04 06:50:00

所以对于您的查询:

conn.execute('''INSERT INTO "TBLNAME"
(value1,value2,value3,value4,value5)
VALUES ('{0}','{1}','{2}',to_timestamp({3}) AT TIME ZONE 'UTC',current_timestamp)'''.
format(server,forecastmemory,levelerror,datecollect/1000))