【问题标题】:PostgreSQL: how to convert from Unix epoch to date?PostgreSQL:如何从 Unix 纪元转换为日期?
【发布时间】:2013-05-12 15:53:10
【问题描述】:

声明给了我日期和时间。

如何修改语句以使其仅返回日期(而不是时间)?

SELECT to_timestamp( TRUNC( CAST( epoch_ms AS bigint ) / 1000 ) );

【问题讨论】:

    标签: sql postgresql date type-conversion epoch


    【解决方案1】:

    您使用to_timestamp 函数,然后将时间戳转换为date

     select to_timestamp(epoch_column)::date;
    

    更多细节:

    /* Current time */
     select now();  -- returns timestamp
    
    /* Epoch from current time;
       Epoch is number of seconds since 1970-01-01 00:00:00+00 */
     select extract(epoch from now()); 
    
    /* Get back time from epoch */
     -- Option 1 - use to_timestamp function
     select to_timestamp( extract(epoch from now()));
     -- Option 2 - add seconds to 'epoch'
     select timestamp with time zone 'epoch' 
             + extract(epoch from now()) * interval '1 second';
    
    /* Cast timestamp to date */
     -- Based on Option 1
     select to_timestamp(extract(epoch from now()))::date;
     -- Based on Option 2
     select (timestamp with time zone 'epoch' 
              + extract(epoch from now()) * interval '1 second')::date; 
    

    在你的情况下:

     select to_timestamp(epoch_ms / 1000)::date;
    

    PostgreSQL Docs

    【讨论】:

    • 似乎不起作用,我收到语法错误。到 2018 年有变化吗?
    • 好吧,我逐字逐句地运行了select to_timestamp(extract(epoch epoch_ms))::date;,它给出了syntax error near epoch_ms。我去寻找其他解决方案,最终这对我有用SELECT TIMESTAMP 'epoch' + (start_dt) * INTERVAL '1 second' as started_on。你能解释一下TIMESTAMPto_timestamp()之间的区别吗?
    • TIMESTAMP 只是列类型,其中 to_timestamp 是一个内置函数,可将 unix 纪元转换为从 '1970-01-01 00:00:00+00' 开始计算的时间戳
    【解决方案2】:
    select to_timestamp(cast(epoch_ms/1000 as bigint))::date
    

    为我工作

    【讨论】:

    • 知道如何使它适用于旧版本(Postgres 8.0 / Redshift)吗?
    【解决方案3】:

    在 Postgres 10 上:

    SELECT to_timestamp(CAST(epoch_ms as bigint)/1000)

    【讨论】:

      【解决方案4】:

      上述解决方案不适用于 PostgreSQL 上的最新版本。我发现这种方法可以转换存储在数字中的纪元时间,而 int 列类型在 PostgreSQL 13 上:

      SELECT TIMESTAMP 'epoch' + (<table>.field::int) * INTERVAL '1 second' as started_on from <table>;
      

      更详细的解释可以看这里https://www.yodiw.com/convert-epoch-time-to-timestamp-in-postgresql/#more-214

      【讨论】:

      • 上述解决方案绝对适用于 Postgres 12 或 13。但您可以将解决方案简化为 make_timestamp(sec =&gt; the_column)
      【解决方案5】:

      这对我来说很好:

      SELECT t.*,
         to_timestamp(cast(t.prev_fire_time/1000 as bigint)) as prev_fire_time,
         to_timestamp(cast(t.next_fire_time/1000 as bigint)) as next_fire_time,
         to_timestamp(cast(t.start_time/1000 as bigint)) as start_time
      FROM public.qrtz_triggers t;
      

      【讨论】:

        【解决方案6】:

        自 GNU date 纪元以来的秒数:

        $ date +%s.%N
        1627059870.945134901
        

        这适用于 PostgreSQL 11:

        # select to_timestamp (1627059870.945134901);
                 to_timestamp          
        -------------------------------
         2021-07-23 19:04:30.945135+02
        (1 row)
        
        # select to_timestamp (1627059870.945134901)::date;
         to_timestamp 
        --------------
         2021-07-23
        (1 row)
        

        【讨论】:

          猜你喜欢
          • 2012-11-07
          • 2016-09-15
          • 2019-08-21
          • 1970-01-01
          • 2013-04-02
          • 2023-02-26
          相关资源
          最近更新 更多