Postgres: как конвертировать из эпохи unix на сегодняшний день? - программирование
Подтвердить что ты не робот

Postgres: как конвертировать из эпохи unix на сегодняшний день?

В заявлении дается дата и время.

Как я могу изменить инструкцию так, чтобы она возвращала только дату?

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

Ответ 1

/* Current time */
 select now(); 

/* 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; 

 /* For column epoch_ms */
 select to_timestamp(extract(epoch epoch_ms))::date;

Документы PostgreSQL