web-dev-qa-db-fra.com

PostgreSQL: comment convertir d'Unix Epoch à ce jour?

La déclaration me donne la date et l'heure.

Comment pourrais-je modifier l'instruction afin qu'elle ne renvoie que la date (et non l'heure)?

SELECT to_timestamp( TRUNC( CAST( Epoch_ms AS bigint ) / 1000 ) );
37
sid_com
/* 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;

Documents PostgreSQL

65
Tomas Greif