Re: to_timestamp overloaded to convert from Unix epoch - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: to_timestamp overloaded to convert from Unix epoch
Date
Msg-id 200506091633.j59GXxL28103@candle.pha.pa.us
Whole thread Raw
In response to to_timestamp overloaded to convert from Unix epoch  (Michael Glaesemann <grzm@myrealbox.com>)
List pgsql-patches
Updated patch attached with both documentation portions --- applied.

---------------------------------------------------------------------------

Michael Glaesemann wrote:
> Please find attached a patch (diff -c against cvs HEAD) to add a
> function that accepts a double precision argument assumed to be a Unix
> epoch timestamp and returns timestamp with time zone, and accompanying
> documentation.
>
> Usage:
>
> test=# select to_timestamp(200120400);
>        to_timestamp
> ------------------------
>   1976-05-05 14:00:00+09
> (1 row)
>
> If regression tests are required, I will produce some. I'd appreciate
> any pointers as to what to look for, as they would be my first attempt
> at writing regression tests.
>
> Regards
>
> Michael Glaesemann
> grzm myrealbox com
>

[ Attachment, skipping... ]

>
> ---------------------------(end of broadcast)---------------------------
> TIP 2: you can get off all lists at once with the unregister command
>     (send "unregister YourEmailAddressHere" to majordomo@postgresql.org)

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: doc/src/sgml/func.sgml
===================================================================
RCS file: /cvsroot/pgsql/doc/src/sgml/func.sgml,v
retrieving revision 1.252
diff -c -c -r1.252 func.sgml
*** doc/src/sgml/func.sgml    7 Jun 2005 07:08:34 -0000    1.252
--- doc/src/sgml/func.sgml    9 Jun 2005 16:33:38 -0000
***************
*** 4082,4087 ****
--- 4082,4094 ----
      argument is the value to be formatted and the second argument is a
      template that defines the output or input format.
     </para>
+    <para>
+     The <function>to_timestamp</function> function can also take a single
+     <type>double precision</type> argument to convert from Unix epoch to
+     <type>timestamp with time zone</type>.
+     (<type>Integer</type> Unix epochs are implicitly cast to
+     <type>double precision</type>.)
+    </para>

      <table id="functions-formatting-table">
       <title>Formatting Functions</title>
***************
*** 4139,4144 ****
--- 4146,4157 ----
          <entry><literal>to_timestamp('05 Dec 2000', 'DD Mon YYYY')</literal></entry>
         </row>
         <row>
+         <entry><literal><function>to_timestamp</function>(<type>double precision</type>)</literal></entry>
+         <entry><type>timestamp with time zone</type></entry>
+         <entry>convert UNIX epoch to time stamp</entry>
+         <entry><literal>to_timestamp(200120400)</literal></entry>
+        </row>
+        <row>
          <entry><literal><function>to_number</function>(<type>text</type>, <type>text</type>)</literal></entry>
          <entry><type>numeric</type></entry>
          <entry>convert string to numeric</entry>
Index: src/include/catalog/pg_proc.h
===================================================================
RCS file: /cvsroot/pgsql/src/include/catalog/pg_proc.h,v
retrieving revision 1.364
diff -c -c -r1.364 pg_proc.h
*** src/include/catalog/pg_proc.h    7 Jun 2005 07:08:34 -0000    1.364
--- src/include/catalog/pg_proc.h    9 Jun 2005 16:33:45 -0000
***************
*** 1459,1464 ****
--- 1459,1466 ----
  DESCR("greater-than-or-equal");
  DATA(insert OID = 1157 (  timestamptz_gt   PGNSP PGUID 12 f f t f i 2 16 "1184 1184" _null_ _null_ _null_
timestamp_gt- _null_ )); 
  DESCR("greater-than");
+ DATA(insert OID = 1158 (  to_timestamp       PGNSP PGUID 14 f f t f i 1 1184 "701" _null_    _null_ _null_ "select
(\'epoch\'::timestamptz+ $1 * \'1 second\'::interval)" - _null_ )); 
+ DESCR("convert UNIX epoch to timestamptz");
  DATA(insert OID = 1159 (  timezone           PGNSP PGUID 12 f f t f i 2 1114 "25 1184" _null_ _null_ _null_
timestamptz_zone- _null_ )); 
  DESCR("adjust timestamp to new time zone");


pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: (8.1) to_timestamp correction (epoch to timestamptz)
Next
From: Tom Lane
Date:
Subject: Re: final light versions of Oracle compatibility (SQLSTATE, GREATEST,