【问题标题】:Insertion by removing '000000' in the unix timestamp通过删除 unix 时间戳中的 '000000' 插入
【发布时间】:2020-04-04 23:27:16
【问题描述】:

我正在为 python 插入一些数据,这些数据来自像 JSON 这样的 API FLASK 中的 GET,由于某种原因,当 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

转换正确:

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

【问题讨论】:

    标签: python sql postgresql psycopg2


    【解决方案1】:

    您的时间戳值 (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))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-13
      • 1970-01-01
      • 2011-03-26
      • 2022-01-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多