Re: Trigger to convert UNIX time to timestamp without time zone.

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: Trigger to convert UNIX time to timestamp without time zone.
Date: 2014-06-06 13:35:33
Message-ID: 5391C3A5.1030407@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(dot)klaver(at)aklaver(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Stefan Froehlich 2014-06-06 13:37:59 Re: interpret bytea output as text / double encode()
Previous Message Rob Sargent 2014-06-06 13:22:57 Re: interpret bytea output as text / double encode()