Thread: testing - please ignore

testing - please ignore

From
hgonzal@sinectis.com.ar
Date:
testing - please ignore

Function to convert from (integer) UNIXTIME to DATETIME

From
"Morten W. Petersen"
Date:
Is there such a function (couldn't find it)

-Morten


Re: Function to convert from (integer) UNIXTIME to DATETIME

From
Ed Loehr
Date:
"Morten W. Petersen" wrote:
>
> Is there such a function (couldn't find it)

I haven't seen one, but here's one that might work:

select '1-Jan-2000'::datetime + ( 962037634 - ( date_part( 'epoch',
'1-Jan-2000'::datetime) )||' second')::interval"

Regards,
Ed Loehr

Re: Function to convert from (integer) UNIXTIME to DATETIME

From
Tom Lane
Date:
"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

Re: Function to convert from (integer) UNIXTIME to DATETIME

From
Thomas Lockhart
Date:
> You can cast an integer representing Unix timestamp directly to
> abstime...

Or directly to timestamp (which may pass through abstime on the way):

lockhart=# select timestamp(962057134);
         timestamp
---------------------------
 2000-06-26 22:05:34.00+00
(1 row)

Probably works for 6.5.x too.

                    - Thomas