1
- <!-- $Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.53 2001/02/19 00:01:18 tgl Exp $ -->
1
+ <!-- $Header: /cvsroot/pgsql/doc/src/sgml/func.sgml,v 1.54 2001/02/21 23:15:24 tgl Exp $ -->
2
2
3
3
<chapter id="functions">
4
4
<title>Functions and Operators</title>
@@ -2197,6 +2197,17 @@ reasonable at all for that matter.
2197
2197
<entry></entry>
2198
2198
</row>
2199
2199
2200
+ <row>
2201
+ <entry>timeofday()</entry>
2202
+ <entry>text</entry>
2203
+ <entry>
2204
+ returns high-precision date and time; see also <link
2205
+ linkend="functions-datetime-current">below</link>
2206
+ </entry>
2207
+ <entry>timeofday()</entry>
2208
+ <entry>Wed Feb 21 17:01:13.000126 2001 EST</entry>
2209
+ </row>
2210
+
2200
2211
<row>
2201
2212
<entry>timestamp(date)</entry>
2202
2213
<entry>timestamp</entry>
@@ -2627,7 +2638,8 @@ SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');
2627
2638
<title>Current Date/Time</title>
2628
2639
2629
2640
<para>
2630
- The following functions are available to select the current date and/or time:
2641
+ The following functions are available to obtain the current date and/or
2642
+ time:
2631
2643
<synopsis>
2632
2644
CURRENT_TIME
2633
2645
CURRENT_DATE
@@ -2641,23 +2653,56 @@ CURRENT_TIMESTAMP
2641
2653
<informalexample>
2642
2654
<screen>
2643
2655
SELECT CURRENT_TIME;
2644
- <computeroutput>19:07:13 </computeroutput>
2656
+ <computeroutput>19:07:32 </computeroutput>
2645
2657
2646
2658
SELECT CURRENT_DATE;
2647
2659
<computeroutput>2001-02-17</computeroutput>
2648
2660
2649
2661
SELECT CURRENT_TIMESTAMP;
2650
- <computeroutput>2001-02-17 19:07:32+00 </computeroutput>
2662
+ <computeroutput>2001-02-17 19:07:32-05 </computeroutput>
2651
2663
</screen>
2652
2664
</informalexample>
2653
2665
2654
2666
<para>
2655
2667
The function <function>now()</function> is the traditional
2656
2668
<productname>Postgres</productname> equivalent to
2657
2669
<function>CURRENT_TIMESTAMP</function>.
2658
- <productname>Postgres</productname> furthermore has special
2659
- date/time <quote>constants</quote> that can be used to specify the
2660
- current time. The following three all return the same result:
2670
+ </para>
2671
+
2672
+ <para>
2673
+ There is also <function>timeofday()</function>, which returns current
2674
+ time to higher precision than the <function>CURRENT_TIMESTAMP</function>
2675
+ family does:
2676
+ </para>
2677
+
2678
+ <informalexample>
2679
+ <screen>
2680
+ SELECT timeofday();
2681
+ Sat Feb 17 19:07:32.000126 2001 EST
2682
+ </screen>
2683
+ </informalexample>
2684
+
2685
+ <para>
2686
+ <function>timeofday()</function> uses the operating system call
2687
+ <function>gettimeofday(2)</function>, which may have resolution as
2688
+ good as microseconds (depending on your platform); the other functions
2689
+ rely on <function>time(2)</function> which is restricted to one-second
2690
+ resolution. For historical reasons, <function>timeofday()</function>
2691
+ returns its result as a text string rather than a timestamp value.
2692
+ </para>
2693
+
2694
+ <para>
2695
+ It is quite important to realize that
2696
+ <function>CURRENT_TIMESTAMP</function> and related functions all return
2697
+ the time as of the start of the current transaction; their values do not
2698
+ increment while a transaction is running. But
2699
+ <function>timeofday()</function> returns the actual current time.
2700
+ </para>
2701
+
2702
+ <para>
2703
+ All the date/time datatypes also accept the special literal value
2704
+ <literal>now</> to specify the current date and time. Thus,
2705
+ the following three all return the same result:
2661
2706
<programlisting>
2662
2707
SELECT CURRENT_TIMESTAMP;
2663
2708
SELECT now();
@@ -2666,11 +2711,13 @@ SELECT TIMESTAMP 'now';
2666
2711
<note>
2667
2712
<para>
2668
2713
You do not want to use the third form when specifying a DEFAULT
2669
- value when creating a table. The system will immediately
2670
- evaluate the constant, thus when the default value is needed,
2714
+ value while creating a table. The system will convert <literal>now</>
2715
+ to a timestamp as soon as the constant is parsed, so that when
2716
+ the default value is needed,
2671
2717
the time of the table creation would be used! The first two
2672
2718
forms will not be evaluated until the default value is used,
2673
- because they are function calls.
2719
+ because they are function calls. Thus they will give the desired
2720
+ behavior of defaulting to the time of row insertion.
2674
2721
</para>
2675
2722
</note>
2676
2723
</para>
0 commit comments