【问题标题】:PostgreSQL epoch time conversionPostgreSQL 纪元时间转换
【发布时间】:2015-06-21 11:18:27
【问题描述】:

我正在使用 postgresSQL 函数 to_timestamp(双精度)将纪元时间转换为正常时间戳,但我遇到了时间戳不正确的问题

SELECT to_timestamp(1428058548491);

产生“47223-05-17 12:08:11.000064+02”

应该是 2015 年 4 月 3 日下午 12:55:48 GMT+2:00 DST

SELCT to_timestamp(1428058557697);

产生“47223-05-17 14:41:36.999936+02”

虽然应该是 2015 年 4 月 3 日,12:55:57

可以看出日期转换完全不正确

【问题讨论】:

  • $ date --date="@1428058548491" Wed, May 17, 47223 6:08:11 AM 您的“纪元时间”似乎不是预期的标准 Unix 纪元。它来自哪个平台?
  • to_timestamp() 需要秒,而不是毫秒:SELECT to_timestamp(1428058548491 / 1000);

标签: database postgresql postgresql-9.3 psql


【解决方案1】:

正如人们在 cmets 中为遇到同样问题的人所解释的那样。函数 to_timestamp() 期望它以秒而不是毫秒为单位,因此这是解决方案。

Quote from the manual:

它接受一个双精度参数并从 Unix 纪元(seconds since 1970-01-01 00:00:00+00)转换为带时区的时间戳

【讨论】:

    猜你喜欢
    • 2014-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-30
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多