Re: Function to convert from (integer) UNIXTIME to DATETIME - Mailing list pgsql-general

From Tom Lane
Subject Re: Function to convert from (integer) UNIXTIME to DATETIME
Date
Msg-id 19392.962057402@sss.pgh.pa.us
Whole thread Raw
In response to Function to convert from (integer) UNIXTIME to DATETIME  ("Morten W. Petersen" <morten@src.no>)
List pgsql-general
"Morten W. Petersen" <morten@src.no> writes:
> Is there such a function (couldn't find it)

You can cast an integer representing Unix timestamp directly to abstime,
and thence to any other datetime type you want:

regression=# select abstime(962057134);
        abstime
------------------------
 2000-06-26 18:05:34-04
(1 row)

To go the other way, you can cast to abstime and then int4, or you can
use date_part('epoch',...):

regression=# select int4(abstime('2000-06-26 18:05:34-04'::timestamp));
   int4
-----------
 962057134
(1 row)

regression=# select date_part('epoch','2000-06-26 18:05:34-04'::timestamp);
 date_part
-----------
 962057134
(1 row)

This works in 7.0, not sure about earlier releases.

            regards, tom lane

pgsql-general by date:

Previous
From: Joseph Shraibman
Date:
Subject: Re: DateTime fields
Next
From: "Len Morgan"
Date:
Subject: Re: Conversion from MS Access to Postgresql