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

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: "Morten W(dot) Petersen" <morten(at)src(dot)no>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: Function to convert from (integer) UNIXTIME to DATETIME
Date: 2000-06-26 22:10:02
Message-ID: 19392.962057402@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

"Morten W. Petersen" <morten(at)src(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Len Morgan 2000-06-26 22:22:08 Re: Conversion from MS Access to Postgresql
Previous Message Joseph Shraibman 2000-06-26 22:00:27 Re: DateTime fields