Epoch to timestamp conversion function patch - Mailing list pgsql-patches

From Michael Glaesemann
Subject Epoch to timestamp conversion function patch
Date
Msg-id D0B37D85-E4FE-11D8-8966-000A95C88220@myrealbox.com
Whole thread Raw
Responses Re: Epoch to timestamp conversion function patch
List pgsql-patches
Please find attached two patches (one for pg_proc.h and another for
supporting documentation) for two SQL functions:
epoch_to_timestamp(integer) and epoch_to_timestamptz(double precision),
which convert from UNIX epoch to the native PostgreSQL timestamp and
timestamptz data types. The equivalent SQL code is

create function epoch_to_timestamp(integer)
    returns timestamp
    language sql as '
    select (\'epoch\'::timestamptz + $1 * \'1
second\'::interval)::timestamp
    ';

create function epoch_to_timestamptz(double precision)
    returns timestamptz
    language sql as '
    select (\'epoch\'::timestamp + $1 * \'1 second\'::interval) at time
zone \'UTC\'
    ';

Some very simple tests (all should return TRUE):

test=# select epoch_to_timestamp(extract(epoch from
current_timestamp)::integer) = current_timestamp::timestamp(0);
  ?column?
----------
  t
(1 row)

test=# select epoch_to_timestamptz(extract(epoch from
current_timestamp)::integer) = current_timestamp(0);
  ?column?
----------
  t
(1 row)

test=# select epoch_to_timestamptz(extract(epoch from
current_timestamp)) = current_timestamp;
  ?column?
----------
  t
(1 row)

If regression tests are desired, I'll work some up. Any feedback
appreciated.

Michael Glaesemann
grzm myrealbox com


Attachment

pgsql-patches by date:

Previous
From: Christopher Kings-Lynne
Date:
Subject: Re: psql latex bugfixes
Next
From: "Matthew T. O'Connor"
Date:
Subject: Re: autovauum integration patch: Attempt #4