testing - please ignore

Started by Hernan Gonzalezalmost 26 years ago5 messagesgeneral
Jump to latest
#1Hernan Gonzalez
hgonzal@sinectis.com.ar

testing - please ignore

In reply to: Hernan Gonzalez (#1)
Function to convert from (integer) UNIXTIME to DATETIME

Is there such a function (couldn't find it)

-Morten

#3Ed Loehr
eloehr@austin.rr.com
In reply to: Morten W. Petersen (#2)
Re: Function to convert from (integer) UNIXTIME to DATETIME

"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

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Morten W. Petersen (#2)
Re: Function to convert from (integer) UNIXTIME to DATETIME

"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

#5Thomas Lockhart
lockhart@alumni.caltech.edu
In reply to: Morten W. Petersen (#2)
Re: Function to convert from (integer) UNIXTIME to DATETIME

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