Re: Trigger to convert UNIX time to timestamp without time zone. - Mailing list pgsql-general

From Adrian Klaver
Subject Re: Trigger to convert UNIX time to timestamp without time zone.
Date
Msg-id 5391C3A5.1030407@aklaver.com
Whole thread Raw
In response to Trigger to convert UNIX time to timestamp without time zone.  (Alberto Olivares <alberto.olivares@snowflakesoftware.com>)
List pgsql-general
On 06/06/2014 06:19 AM, Alberto Olivares wrote:
> Hello,
>
> I want to insert data into a column "timestamp without time zone" data
> type. The problem is I am receiving the data in UNIX time.
>
> How can I create a trigger to transform the time from UNIX to timestamp
> without time zone every time a new record is inserted into my database?

to_timesstamp will take the epoch, which I assume is what you are
talking about, and turn it into a timestamp with time zone. Don't worry
about the timezone.

test=> select to_timestamp(extract(epoch from now()));
          to_timestamp
-------------------------------
  2014-06-06 06:27:20.484509-07
(1 row)

test=> \d timestamp_test


           Table "public.timestamp_test"


  Column |            Type             | Modifiers


--------+-----------------------------+-----------


  id     | integer                     |


  ts     | timestamp without time zone |


  ts_z   | timestamp with time zone    |


Inserting a timestamp with time zone into a field that is timestamp
without timezone will strip the timezone automatically.

test=> insert into timestamp_test values (1, to_timestamp(extract(epoch
from now())), to_timestamp(extract(epoch from now())));
INSERT 0 1

test=> select * from timestamp_test ;
  id |             ts             |             ts_z
----+----------------------------+-------------------------------
   1 | 2014-06-06 06:30:58.080158 | 2014-06-06 06:30:58.080158-07
(1 row)

So you just need to wrap the above in a function and call it from your
trigger.


>
> Thank you in advance.
>
> Regards,
> Alberto.
>
>
> *Alberto Olivares Colas
> *Technical Consultant
> Snowflake Software



--
Adrian Klaver
adrian.klaver@aklaver.com


pgsql-general by date:

Previous
From: Rob Sargent
Date:
Subject: Re: interpret bytea output as text / double encode()
Next
From: Stefan Froehlich
Date:
Subject: Re: interpret bytea output as text / double encode()