reverse of extract epoch?

From: Gregory Seidman <gss+pg(at)cs(dot)brown(dot)edu>
To: pgsql-general(at)postgresql(dot)org
Subject: reverse of extract epoch?
Date: 2002-06-03 01:17:50
Message-ID: 20020603011750.GA12152@jamaica.cs.brown.edu
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

I happen to like using epoch-based timestamps. Given that, I make heavy use
of the following function as a default value in several places:

CREATE FUNCTION EPOCHNOW () RETURNS int
AS 'SELECT (EXTRACT (epoch FROM NOW()))::int'
LANGUAGE SQL;

Now I want to create its reverse so I can get a timestamp (with tz) from an
epoch-based integer. Is there some simple way of doing it? Right now I have
in place the following:

CREATE FUNCTION EPOCHtoTS (int) RETURNS timestamp
AS 'SELECT NOW() + (($1 - (EXTRACT (epoch FROM NOW()))::int) || \' seconds\')::interval'
LANGUAGE SQL;

It has to be easier than that, right? Realistically, if I can convert
from epoch to timestamp simply then I will move over to having things
stored in the db as timestamps and only use epoch-based times
programmatically.

--Greg

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Bruce Momjian 2002-06-03 02:24:01 Re: Subject: bool / vacuum full bug followup part 2
Previous Message Nick Fankhauser 2002-06-02 22:13:37 Re: Cannot build JDBC driver in 7.1.3.