Sophie

Sophie

distrib > Mandriva > 2006.0 > x86_64 > by-pkgid > b8f4049de69feba5041d49ed4382e582 > files > 81

postgresql-docs-8.0.11-0.1.20060mdk.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Date/Time Types</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PostgreSQL 8.0.11 Documentation"
HREF="index.html"><LINK
REL="UP"
TITLE="Data Types"
HREF="datatype.html"><LINK
REL="PREVIOUS"
TITLE="Binary Data Types"
HREF="datatype-binary.html"><LINK
REL="NEXT"
TITLE="Boolean Type"
HREF="datatype-boolean.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2007-02-02T03:57:22"></HEAD
><BODY
CLASS="SECT1"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
>PostgreSQL 8.0.11 Documentation</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="datatype-binary.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
HREF="datatype.html"
>Fast Backward</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
>Chapter 8. Data Types</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="datatype.html"
>Fast Forward</A
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
HREF="datatype-boolean.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="SECT1"
><H1
CLASS="SECT1"
><A
NAME="DATATYPE-DATETIME"
>8.5. Date/Time Types</A
></H1
><A
NAME="AEN4192"
></A
><A
NAME="AEN4194"
></A
><A
NAME="AEN4196"
></A
><A
NAME="AEN4198"
></A
><A
NAME="AEN4200"
></A
><A
NAME="AEN4202"
></A
><A
NAME="AEN4204"
></A
><A
NAME="AEN4206"
></A
><A
NAME="AEN4208"
></A
><P
>    <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> supports the full set of
    <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> date and time types, shown in <A
HREF="datatype-datetime.html#DATATYPE-DATETIME-TABLE"
>Table 8-9</A
>.  The operations available
    on these data types are described in
    <A
HREF="functions-datetime.html"
>Section 9.9</A
>.
   </P
><DIV
CLASS="TABLE"
><A
NAME="DATATYPE-DATETIME-TABLE"
></A
><P
><B
>Table 8-9. Date/Time Types</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><COL><COL><COL><COL><THEAD
><TR
><TH
>Name</TH
><TH
>Storage Size</TH
><TH
>Description</TH
><TH
>Low Value</TH
><TH
>High Value</TH
><TH
>Resolution</TH
></TR
></THEAD
><TBODY
><TR
><TD
><TT
CLASS="TYPE"
>timestamp [ (<TT
CLASS="REPLACEABLE"
><I
>p</I
></TT
>) ] [ without time zone ]</TT
></TD
><TD
>8 bytes</TD
><TD
>both date and time</TD
><TD
>4713 BC</TD
><TD
>5874897 AD</TD
><TD
>1 microsecond / 14 digits</TD
></TR
><TR
><TD
><TT
CLASS="TYPE"
>timestamp [ (<TT
CLASS="REPLACEABLE"
><I
>p</I
></TT
>) ] with time zone</TT
></TD
><TD
>8 bytes</TD
><TD
>both date and time, with time zone</TD
><TD
>4713 BC</TD
><TD
>5874897 AD</TD
><TD
>1 microsecond / 14 digits</TD
></TR
><TR
><TD
><TT
CLASS="TYPE"
>interval [ (<TT
CLASS="REPLACEABLE"
><I
>p</I
></TT
>) ]</TT
></TD
><TD
>12 bytes</TD
><TD
>time intervals</TD
><TD
>-178000000 years</TD
><TD
>178000000 years</TD
><TD
>1 microsecond / 14 digits</TD
></TR
><TR
><TD
><TT
CLASS="TYPE"
>date</TT
></TD
><TD
>4 bytes</TD
><TD
>dates only</TD
><TD
>4713 BC</TD
><TD
>5874897 AD</TD
><TD
>1 day</TD
></TR
><TR
><TD
><TT
CLASS="TYPE"
>time [ (<TT
CLASS="REPLACEABLE"
><I
>p</I
></TT
>) ] [ without time zone ]</TT
></TD
><TD
>8 bytes</TD
><TD
>times of day only</TD
><TD
>00:00:00.00</TD
><TD
>23:59:59.99</TD
><TD
>1 microsecond / 14 digits</TD
></TR
><TR
><TD
><TT
CLASS="TYPE"
>time [ (<TT
CLASS="REPLACEABLE"
><I
>p</I
></TT
>) ] with time zone</TT
></TD
><TD
>12 bytes</TD
><TD
>times of day only, with time zone</TD
><TD
>00:00:00.00+12</TD
><TD
>23:59:59.99-12</TD
><TD
>1 microsecond / 14 digits</TD
></TR
></TBODY
></TABLE
></DIV
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>     Prior to <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> 7.3, writing just
     <TT
CLASS="TYPE"
>timestamp</TT
> was equivalent to <TT
CLASS="TYPE"
>timestamp with
     time zone</TT
>.  This was changed for SQL compliance.
    </P
></BLOCKQUOTE
></DIV
><P
>    <TT
CLASS="TYPE"
>time</TT
>, <TT
CLASS="TYPE"
>timestamp</TT
>, and
    <TT
CLASS="TYPE"
>interval</TT
> accept an optional precision value
    <TT
CLASS="REPLACEABLE"
><I
>p</I
></TT
> which specifies the number of
    fractional digits retained in the seconds field. By default, there
    is no explicit bound on precision.  The allowed range of
    <TT
CLASS="REPLACEABLE"
><I
>p</I
></TT
> is from 0 to 6 for the
    <TT
CLASS="TYPE"
>timestamp</TT
> and <TT
CLASS="TYPE"
>interval</TT
> types.
   </P
><DIV
CLASS="NOTE"
><BLOCKQUOTE
CLASS="NOTE"
><P
><B
>Note: </B
>    When <TT
CLASS="TYPE"
>timestamp</TT
> values are stored as double precision floating-point
    numbers (currently the default), the effective limit of precision
    may be less than 6. <TT
CLASS="TYPE"
>timestamp</TT
> values are stored as seconds
    before or after midnight 2000-01-01.  Microsecond precision is achieved for
    dates within a few years of 2000-01-01, but the precision degrades for
    dates further away.  When <TT
CLASS="TYPE"
>timestamp</TT
> values are stored as
    eight-byte integers (a compile-time
    option), microsecond precision is available over the full range of
    values. However eight-byte integer timestamps have a more limited range of
    dates than shown above: from 4713 BC up to 294276 AD.  The same
    compile-time option also determines whether <TT
CLASS="TYPE"
>time</TT
> and
    <TT
CLASS="TYPE"
>interval</TT
> values are stored as floating-point or eight-byte
    integers.  In the floating-point case, large <TT
CLASS="TYPE"
>interval</TT
> values
    degrade in precision as the size of the interval increases.
   </P
></BLOCKQUOTE
></DIV
><P
>    For the <TT
CLASS="TYPE"
>time</TT
> types, the allowed range of
    <TT
CLASS="REPLACEABLE"
><I
>p</I
></TT
> is from 0 to 6 when eight-byte integer
    storage is used, or from 0 to 10 when floating-point storage is used.
   </P
><P
>    The type <TT
CLASS="TYPE"
>time with time zone</TT
> is defined by the SQL
    standard, but the definition exhibits properties which lead to
    questionable usefulness. In most cases, a combination of
    <TT
CLASS="TYPE"
>date</TT
>, <TT
CLASS="TYPE"
>time</TT
>, <TT
CLASS="TYPE"
>timestamp without time
    zone</TT
>, and <TT
CLASS="TYPE"
>timestamp with time zone</TT
> should
    provide a complete range of date/time functionality required by
    any application.
   </P
><P
>    The types <TT
CLASS="TYPE"
>abstime</TT
>
    and <TT
CLASS="TYPE"
>reltime</TT
> are lower precision types which are used internally.
    You are discouraged from using these types in new
    applications and are encouraged to move any old
    ones over when appropriate. Any or all of these internal types
    might disappear in a future release.
   </P
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="DATATYPE-DATETIME-INPUT"
>8.5.1. Date/Time Input</A
></H2
><P
>     Date and time input is accepted in almost any reasonable format, including
     ISO 8601, <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
>-compatible, 
     traditional <SPAN
CLASS="PRODUCTNAME"
>POSTGRES</SPAN
>, and others.
     For some formats, ordering of month, day, and year in date input is
     ambiguous and there is support for specifying the expected
     ordering of these fields.  Set the <A
HREF="runtime-config.html#GUC-DATESTYLE"
>DateStyle</A
> parameter
     to <TT
CLASS="LITERAL"
>MDY</TT
> to select month-day-year interpretation,
     <TT
CLASS="LITERAL"
>DMY</TT
> to select day-month-year interpretation, or
     <TT
CLASS="LITERAL"
>YMD</TT
> to select year-month-day interpretation.
    </P
><P
>     <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> is more flexible in
     handling date/time input than the
     <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> standard requires.
     See <A
HREF="datetime-appendix.html"
>Appendix B</A
>
     for the exact parsing rules of date/time input and for the
     recognized text fields including months, days of the week, and
     time zones. 
    </P
><P
>     Remember that any date or time literal input needs to be enclosed
     in single quotes, like text strings.  Refer to 
     <A
HREF="sql-syntax.html#SQL-SYNTAX-CONSTANTS-GENERIC"
>Section 4.1.2.5</A
> for more
     information.
     <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> requires the following syntax
</P><PRE
CLASS="SYNOPSIS"
><TT
CLASS="REPLACEABLE"
><I
>type</I
></TT
> [ (<TT
CLASS="REPLACEABLE"
><I
>p</I
></TT
>) ] '<TT
CLASS="REPLACEABLE"
><I
>value</I
></TT
>'</PRE
><P>
     where <TT
CLASS="REPLACEABLE"
><I
>p</I
></TT
> in the optional precision
     specification is an integer corresponding to the number of
     fractional digits in the seconds field. Precision can be
     specified for <TT
CLASS="TYPE"
>time</TT
>, <TT
CLASS="TYPE"
>timestamp</TT
>, and
     <TT
CLASS="TYPE"
>interval</TT
> types.  The allowed values are mentioned
     above.  If no precision is specified in a constant specification,
     it defaults to the precision of the literal value.
    </P
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN4337"
>8.5.1.1. Dates</A
></H3
><A
NAME="AEN4339"
></A
><P
>     <A
HREF="datatype-datetime.html#DATATYPE-DATETIME-DATE-TABLE"
>Table 8-10</A
> shows some possible
     inputs for the <TT
CLASS="TYPE"
>date</TT
> type.
    </P
><DIV
CLASS="TABLE"
><A
NAME="DATATYPE-DATETIME-DATE-TABLE"
></A
><P
><B
>Table 8-10. Date Input</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><THEAD
><TR
><TH
>Example</TH
><TH
>Description</TH
></TR
></THEAD
><TBODY
><TR
><TD
>January 8, 1999</TD
><TD
>unambiguous in any <TT
CLASS="VARNAME"
>datestyle</TT
> input mode</TD
></TR
><TR
><TD
>1999-01-08</TD
><TD
>ISO 8601; January 8 in any mode
         (recommended format)</TD
></TR
><TR
><TD
>1/8/1999</TD
><TD
>January 8 in <TT
CLASS="LITERAL"
>MDY</TT
> mode;
          August 1 in <TT
CLASS="LITERAL"
>DMY</TT
> mode</TD
></TR
><TR
><TD
>1/18/1999</TD
><TD
>January 18 in <TT
CLASS="LITERAL"
>MDY</TT
> mode;
          rejected in other modes</TD
></TR
><TR
><TD
>01/02/03</TD
><TD
>January 2, 2003 in <TT
CLASS="LITERAL"
>MDY</TT
> mode;
          February 1, 2003 in <TT
CLASS="LITERAL"
>DMY</TT
> mode;
          February 3, 2001 in <TT
CLASS="LITERAL"
>YMD</TT
> mode
         </TD
></TR
><TR
><TD
>1999-Jan-08</TD
><TD
>January 8 in any mode</TD
></TR
><TR
><TD
>Jan-08-1999</TD
><TD
>January 8 in any mode</TD
></TR
><TR
><TD
>08-Jan-1999</TD
><TD
>January 8 in any mode</TD
></TR
><TR
><TD
>99-Jan-08</TD
><TD
>January 8 in <TT
CLASS="LITERAL"
>YMD</TT
> mode, else error</TD
></TR
><TR
><TD
>08-Jan-99</TD
><TD
>January 8, except error in <TT
CLASS="LITERAL"
>YMD</TT
> mode</TD
></TR
><TR
><TD
>Jan-08-99</TD
><TD
>January 8, except error in <TT
CLASS="LITERAL"
>YMD</TT
> mode</TD
></TR
><TR
><TD
>19990108</TD
><TD
>ISO 8601; January 8, 1999 in any mode</TD
></TR
><TR
><TD
>990108</TD
><TD
>ISO 8601; January 8, 1999 in any mode</TD
></TR
><TR
><TD
>1999.008</TD
><TD
>year and day of year</TD
></TR
><TR
><TD
>J2451187</TD
><TD
>Julian day</TD
></TR
><TR
><TD
>January 8, 99 BC</TD
><TD
>year 99 before the Common Era</TD
></TR
></TBODY
></TABLE
></DIV
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN4410"
>8.5.1.2. Times</A
></H3
><A
NAME="AEN4412"
></A
><A
NAME="AEN4414"
></A
><A
NAME="AEN4416"
></A
><P
>      The time-of-day types are <TT
CLASS="TYPE"
>time [
      (<TT
CLASS="REPLACEABLE"
><I
>p</I
></TT
>) ] without time zone</TT
> and
      <TT
CLASS="TYPE"
>time [ (<TT
CLASS="REPLACEABLE"
><I
>p</I
></TT
>) ] with time
      zone</TT
>.  Writing just <TT
CLASS="TYPE"
>time</TT
> is equivalent to
      <TT
CLASS="TYPE"
>time without time zone</TT
>.
     </P
><P
>      Valid input for these types consists of a time of day followed
      by an optional time zone. (See <A
HREF="datatype-datetime.html#DATATYPE-DATETIME-TIME-TABLE"
>Table 8-11</A
> 
      and <A
HREF="datatype-datetime.html#DATATYPE-TIMEZONE-TABLE"
>Table 8-12</A
>.)  If a time zone is
      specified in the input for <TT
CLASS="TYPE"
>time without time zone</TT
>,
      it is silently ignored.
     </P
><DIV
CLASS="TABLE"
><A
NAME="DATATYPE-DATETIME-TIME-TABLE"
></A
><P
><B
>Table 8-11. Time Input</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><THEAD
><TR
><TH
>Example</TH
><TH
>Description</TH
></TR
></THEAD
><TBODY
><TR
><TD
><TT
CLASS="LITERAL"
>04:05:06.789</TT
></TD
><TD
>ISO 8601</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>04:05:06</TT
></TD
><TD
>ISO 8601</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>04:05</TT
></TD
><TD
>ISO 8601</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>040506</TT
></TD
><TD
>ISO 8601</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>04:05 AM</TT
></TD
><TD
>same as 04:05; AM does not affect value</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>04:05 PM</TT
></TD
><TD
>same as 16:05; input hour must be &lt;= 12</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>04:05:06.789-8</TT
></TD
><TD
>ISO 8601</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>04:05:06-08:00</TT
></TD
><TD
>ISO 8601</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>04:05-08:00</TT
></TD
><TD
>ISO 8601</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>040506-08</TT
></TD
><TD
>ISO 8601</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>04:05:06 PST</TT
></TD
><TD
>time zone specified by name</TD
></TR
></TBODY
></TABLE
></DIV
><DIV
CLASS="TABLE"
><A
NAME="DATATYPE-TIMEZONE-TABLE"
></A
><P
><B
>Table 8-12. Time Zone Input</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><THEAD
><TR
><TH
>Example</TH
><TH
>Description</TH
></TR
></THEAD
><TBODY
><TR
><TD
><TT
CLASS="LITERAL"
>PST</TT
></TD
><TD
>Pacific Standard Time</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>-8:00</TT
></TD
><TD
>ISO-8601 offset for PST</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>-800</TT
></TD
><TD
>ISO-8601 offset for PST</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>-8</TT
></TD
><TD
>ISO-8601 offset for PST</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>zulu</TT
></TD
><TD
>Military abbreviation for UTC</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>z</TT
></TD
><TD
>Short form of <TT
CLASS="LITERAL"
>zulu</TT
></TD
></TR
></TBODY
></TABLE
></DIV
><P
>     Refer to <A
HREF="datetime-appendix.html"
>Appendix B</A
> for a list of
     time zone names that are recognized for input.
    </P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN4516"
>8.5.1.3. Time Stamps</A
></H3
><A
NAME="AEN4518"
></A
><A
NAME="AEN4520"
></A
><A
NAME="AEN4522"
></A
><P
>      Valid input for the time stamp types consists of a concatenation
      of a date and a time, followed by an optional time zone,
      followed by an optional <TT
CLASS="LITERAL"
>AD</TT
> or <TT
CLASS="LITERAL"
>BC</TT
>.
      (Alternatively, <TT
CLASS="LITERAL"
>AD</TT
>/<TT
CLASS="LITERAL"
>BC</TT
> can appear
      before the time zone, but this is not the preferred ordering.)
      Thus

</P><PRE
CLASS="PROGRAMLISTING"
>1999-01-08 04:05:06</PRE
><P>
      and
</P><PRE
CLASS="PROGRAMLISTING"
>1999-01-08 04:05:06 -8:00</PRE
><P>

      are valid values, which follow the <ACRONYM
CLASS="ACRONYM"
>ISO</ACRONYM
> 8601
      standard.  In addition, the wide-spread format

</P><PRE
CLASS="PROGRAMLISTING"
>January 8 04:05:06 1999 PST</PRE
><P>
      is supported.
     </P
><P
>      The <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> standard differentiates <TT
CLASS="TYPE"
>timestamp without time zone</TT
> 
      and <TT
CLASS="TYPE"
>timestamp with time zone</TT
> literals by the existence of a 
      <SPAN
CLASS="QUOTE"
>"+"</SPAN
>; or <SPAN
CLASS="QUOTE"
>"-"</SPAN
>. Hence, according to the standard, 
      </P><PRE
CLASS="PROGRAMLISTING"
>TIMESTAMP '2004-10-19 10:23:54'</PRE
><P>
      is a <TT
CLASS="TYPE"
>timestamp without time zone</TT
>, while
      </P><PRE
CLASS="PROGRAMLISTING"
>TIMESTAMP '2004-10-19 10:23:54+02'</PRE
><P>
      is a <TT
CLASS="TYPE"
>timestamp with time zone</TT
>.
      <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> 
      differs from the standard by requiring that <TT
CLASS="TYPE"
>timestamp with time zone</TT
> 
      literals be explicitly typed:
      </P><PRE
CLASS="PROGRAMLISTING"
>TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'</PRE
><P>
      If a literal is not explicitly indicated as being of <TT
CLASS="TYPE"
>timestamp with time zone</TT
>,
      PostgreSQL will silently ignore any time zone indication in the literal.
      That is, the resulting date/time value is derived from the date/time
      fields in the input value, and is not adjusted for time zone.
     </P
><P
>      For <TT
CLASS="TYPE"
>timestamp with time zone</TT
>, the internally stored
      value is always in UTC (Universal
      Coordinated Time, traditionally known as Greenwich Mean Time,
      <ACRONYM
CLASS="ACRONYM"
>GMT</ACRONYM
>).  An input value that has an explicit
      time zone specified is converted to UTC using the appropriate offset
      for that time zone.  If no time zone is stated in the input string,
      then it is assumed to be in the time zone indicated by the system's
      <A
HREF="runtime-config.html#GUC-TIMEZONE"
>timezone</A
> parameter, and is converted to UTC using the
      offset for the <TT
CLASS="VARNAME"
>timezone</TT
> zone.
     </P
><P
>      When a <TT
CLASS="TYPE"
>timestamp with time
      zone</TT
> value is output, it is always converted from UTC to the
      current <TT
CLASS="VARNAME"
>timezone</TT
> zone, and displayed as local time in that
      zone.  To see the time in another time zone, either change
      <TT
CLASS="VARNAME"
>timezone</TT
> or use the <TT
CLASS="LITERAL"
>AT TIME ZONE</TT
> construct
      (see <A
HREF="functions-datetime.html#FUNCTIONS-DATETIME-ZONECONVERT"
>Section 9.9.3</A
>).
     </P
><P
>      Conversions between <TT
CLASS="TYPE"
>timestamp without time zone</TT
> and
      <TT
CLASS="TYPE"
>timestamp with time zone</TT
> normally assume that the
      <TT
CLASS="TYPE"
>timestamp without time zone</TT
> value should be taken or given
      as <TT
CLASS="VARNAME"
>timezone</TT
> local time.  A different zone reference can
      be specified for the conversion using <TT
CLASS="LITERAL"
>AT TIME ZONE</TT
>.
     </P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN4564"
>8.5.1.4. Intervals</A
></H3
><A
NAME="AEN4566"
></A
><P
>       <TT
CLASS="TYPE"
>interval</TT
> values can be written with the following syntax:

</P><PRE
CLASS="PROGRAMLISTING"
>[<SPAN
CLASS="OPTIONAL"
>@</SPAN
>] <TT
CLASS="REPLACEABLE"
><I
>quantity</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>unit</I
></TT
> [<SPAN
CLASS="OPTIONAL"
><TT
CLASS="REPLACEABLE"
><I
>quantity</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>unit</I
></TT
>...</SPAN
>] [<SPAN
CLASS="OPTIONAL"
><TT
CLASS="REPLACEABLE"
><I
>direction</I
></TT
></SPAN
>]</PRE
><P>

      Where: <TT
CLASS="REPLACEABLE"
><I
>quantity</I
></TT
> is a number (possibly signed);
      <TT
CLASS="REPLACEABLE"
><I
>unit</I
></TT
> is <TT
CLASS="LITERAL"
>second</TT
>,
      <TT
CLASS="LITERAL"
>minute</TT
>, <TT
CLASS="LITERAL"
>hour</TT
>, <TT
CLASS="LITERAL"
>day</TT
>,
      <TT
CLASS="LITERAL"
>week</TT
>, <TT
CLASS="LITERAL"
>month</TT
>, <TT
CLASS="LITERAL"
>year</TT
>,
      <TT
CLASS="LITERAL"
>decade</TT
>, <TT
CLASS="LITERAL"
>century</TT
>, <TT
CLASS="LITERAL"
>millennium</TT
>,
      or abbreviations or plurals of these units;
      <TT
CLASS="REPLACEABLE"
><I
>direction</I
></TT
> can be <TT
CLASS="LITERAL"
>ago</TT
> or
      empty.  The at sign (<TT
CLASS="LITERAL"
>@</TT
>) is optional noise.  The amounts
      of different units are implicitly added up with appropriate
      sign accounting.
     </P
><P
>      Quantities of days, hours, minutes, and seconds can be specified without
      explicit unit markings.  For example, <TT
CLASS="LITERAL"
>'1 12:59:10'</TT
> is read
      the same as <TT
CLASS="LITERAL"
>'1 day 12 hours 59 min 10 sec'</TT
>.
     </P
><P
>      The optional precision
      <TT
CLASS="REPLACEABLE"
><I
>p</I
></TT
> should be between 0 and 6, and
      defaults to the precision of the input literal.
     </P
></DIV
><DIV
CLASS="SECT3"
><H3
CLASS="SECT3"
><A
NAME="AEN4599"
>8.5.1.5. Special Values</A
></H3
><A
NAME="AEN4601"
></A
><A
NAME="AEN4604"
></A
><P
>      <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> supports several
      special date/time input values for convenience, as shown in <A
HREF="datatype-datetime.html#DATATYPE-DATETIME-SPECIAL-TABLE"
>Table 8-13</A
>.  The values
      <TT
CLASS="LITERAL"
>infinity</TT
> and <TT
CLASS="LITERAL"
>-infinity</TT
>
      are specially represented inside the system and will be displayed
      the same way; but the others are simply notational shorthands
      that will be converted to ordinary date/time values when read.
      (In particular, <TT
CLASS="LITERAL"
>now</TT
> and related strings are converted
      to a specific time value as soon as they are read.)
      All of these values need to be written in single quotes when used
      as constants in SQL commands.
     </P
><DIV
CLASS="TABLE"
><A
NAME="DATATYPE-DATETIME-SPECIAL-TABLE"
></A
><P
><B
>Table 8-13. Special Date/Time Inputs</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><THEAD
><TR
><TH
>Input String</TH
><TH
>Valid Types</TH
><TH
>Description</TH
></TR
></THEAD
><TBODY
><TR
><TD
><TT
CLASS="LITERAL"
>epoch</TT
></TD
><TD
><TT
CLASS="TYPE"
>date</TT
>, <TT
CLASS="TYPE"
>timestamp</TT
></TD
><TD
>1970-01-01 00:00:00+00 (Unix system time zero)</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>infinity</TT
></TD
><TD
><TT
CLASS="TYPE"
>timestamp</TT
></TD
><TD
>later than all other time stamps</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>-infinity</TT
></TD
><TD
><TT
CLASS="TYPE"
>timestamp</TT
></TD
><TD
>earlier than all other time stamps</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>now</TT
></TD
><TD
><TT
CLASS="TYPE"
>date</TT
>, <TT
CLASS="TYPE"
>time</TT
>, <TT
CLASS="TYPE"
>timestamp</TT
></TD
><TD
>current transaction's start time</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>today</TT
></TD
><TD
><TT
CLASS="TYPE"
>date</TT
>, <TT
CLASS="TYPE"
>timestamp</TT
></TD
><TD
>midnight today</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>tomorrow</TT
></TD
><TD
><TT
CLASS="TYPE"
>date</TT
>, <TT
CLASS="TYPE"
>timestamp</TT
></TD
><TD
>midnight tomorrow</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>yesterday</TT
></TD
><TD
><TT
CLASS="TYPE"
>date</TT
>, <TT
CLASS="TYPE"
>timestamp</TT
></TD
><TD
>midnight yesterday</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>allballs</TT
></TD
><TD
><TT
CLASS="TYPE"
>time</TT
></TD
><TD
>00:00:00.00 UTC</TD
></TR
></TBODY
></TABLE
></DIV
><P
>      The following <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
>-compatible functions can also
      be used to obtain the current time value for the corresponding data
      type:
      <TT
CLASS="LITERAL"
>CURRENT_DATE</TT
>, <TT
CLASS="LITERAL"
>CURRENT_TIME</TT
>, 
      <TT
CLASS="LITERAL"
>CURRENT_TIMESTAMP</TT
>, <TT
CLASS="LITERAL"
>LOCALTIME</TT
>, 
      <TT
CLASS="LITERAL"
>LOCALTIMESTAMP</TT
>.  The latter four accept an 
      optional precision specification.  (See <A
HREF="functions-datetime.html#FUNCTIONS-DATETIME-CURRENT"
>Section 9.9.4</A
>.)  Note however that these are
      SQL functions and are <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not</I
></SPAN
> recognized as data input strings.
     </P
></DIV
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="DATATYPE-DATETIME-OUTPUT"
>8.5.2. Date/Time Output</A
></H2
><A
NAME="AEN4687"
></A
><A
NAME="AEN4691"
></A
><P
>     The output format of the date/time types can be set to one of the four
     styles ISO 8601,
     <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> (Ingres), traditional POSTGRES, and
     German, using the command <TT
CLASS="LITERAL"
>SET datestyle</TT
>.  The default
     is the <ACRONYM
CLASS="ACRONYM"
>ISO</ACRONYM
> format.  (The
     <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> standard requires the use of the ISO 8601
     format.  The name of the <SPAN
CLASS="QUOTE"
>"SQL"</SPAN
> output format is a
     historical accident.)  <A
HREF="datatype-datetime.html#DATATYPE-DATETIME-OUTPUT-TABLE"
>Table 8-14</A
> shows examples of each
     output style.  The output of the <TT
CLASS="TYPE"
>date</TT
> and
     <TT
CLASS="TYPE"
>time</TT
> types is of course only the date or time part
     in accordance with the given examples.
    </P
><DIV
CLASS="TABLE"
><A
NAME="DATATYPE-DATETIME-OUTPUT-TABLE"
></A
><P
><B
>Table 8-14. Date/Time Output Styles</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><COL><THEAD
><TR
><TH
>Style Specification</TH
><TH
>Description</TH
><TH
>Example</TH
></TR
></THEAD
><TBODY
><TR
><TD
>ISO</TD
><TD
>ISO 8601/SQL standard</TD
><TD
>1997-12-17 07:37:16-08</TD
></TR
><TR
><TD
>SQL</TD
><TD
>traditional style</TD
><TD
>12/17/1997 07:37:16.00 PST</TD
></TR
><TR
><TD
>POSTGRES</TD
><TD
>original style</TD
><TD
>Wed Dec 17 07:37:16 1997 PST</TD
></TR
><TR
><TD
>German</TD
><TD
>regional style</TD
><TD
>17.12.1997 07:37:16.00 PST</TD
></TR
></TBODY
></TABLE
></DIV
><P
>     In the <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> and POSTGRES styles, day appears before
     month if DMY field ordering has been specified, otherwise month appears
     before day.
     (See <A
HREF="datatype-datetime.html#DATATYPE-DATETIME-INPUT"
>Section 8.5.1</A
>
     for how this setting also affects interpretation of input values.)
     <A
HREF="datatype-datetime.html#DATATYPE-DATETIME-OUTPUT2-TABLE"
>Table 8-15</A
> shows an
     example.
    </P
><DIV
CLASS="TABLE"
><A
NAME="DATATYPE-DATETIME-OUTPUT2-TABLE"
></A
><P
><B
>Table 8-15. Date Order Conventions</B
></P
><TABLE
BORDER="1"
CLASS="CALSTABLE"
><COL><COL><COL><THEAD
><TR
><TH
><TT
CLASS="VARNAME"
>datestyle</TT
> Setting</TH
><TH
>Input Ordering</TH
><TH
>Example Output</TH
></TR
></THEAD
><TBODY
><TR
><TD
><TT
CLASS="LITERAL"
>SQL, DMY</TT
></TD
><TD
><TT
CLASS="REPLACEABLE"
><I
>day</I
></TT
>/<TT
CLASS="REPLACEABLE"
><I
>month</I
></TT
>/<TT
CLASS="REPLACEABLE"
><I
>year</I
></TT
></TD
><TD
>17/12/1997 15:37:16.00 CET</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>SQL, MDY</TT
></TD
><TD
><TT
CLASS="REPLACEABLE"
><I
>month</I
></TT
>/<TT
CLASS="REPLACEABLE"
><I
>day</I
></TT
>/<TT
CLASS="REPLACEABLE"
><I
>year</I
></TT
></TD
><TD
>12/17/1997 07:37:16.00 PST</TD
></TR
><TR
><TD
><TT
CLASS="LITERAL"
>Postgres, DMY</TT
></TD
><TD
><TT
CLASS="REPLACEABLE"
><I
>day</I
></TT
>/<TT
CLASS="REPLACEABLE"
><I
>month</I
></TT
>/<TT
CLASS="REPLACEABLE"
><I
>year</I
></TT
></TD
><TD
>Wed 17 Dec 07:37:16 1997 PST</TD
></TR
></TBODY
></TABLE
></DIV
><P
>     <TT
CLASS="TYPE"
>interval</TT
> output looks like the input format, except
     that units like <TT
CLASS="LITERAL"
>century</TT
> or
     <TT
CLASS="LITERAL"
>week</TT
> are converted to years and days and
     <TT
CLASS="LITERAL"
>ago</TT
> is converted to an appropriate sign.  In
     ISO mode the output looks like

</P><PRE
CLASS="PROGRAMLISTING"
>[<SPAN
CLASS="OPTIONAL"
> <TT
CLASS="REPLACEABLE"
><I
>quantity</I
></TT
> <TT
CLASS="REPLACEABLE"
><I
>unit</I
></TT
> [<SPAN
CLASS="OPTIONAL"
> ... </SPAN
>] </SPAN
>] [<SPAN
CLASS="OPTIONAL"
> <TT
CLASS="REPLACEABLE"
><I
>days</I
></TT
> </SPAN
>] [<SPAN
CLASS="OPTIONAL"
> <TT
CLASS="REPLACEABLE"
><I
>hours</I
></TT
>:<TT
CLASS="REPLACEABLE"
><I
>minutes</I
></TT
>:<TT
CLASS="REPLACEABLE"
><I
>seconds</I
></TT
> </SPAN
>]</PRE
><P>
    </P
><P
>     The date/time styles can be selected by the user using the
     <TT
CLASS="COMMAND"
>SET datestyle</TT
> command, the <A
HREF="runtime-config.html#GUC-DATESTYLE"
>DateStyle</A
> parameter in the
     <TT
CLASS="FILENAME"
>postgresql.conf</TT
> configuration file, or the
     <TT
CLASS="ENVAR"
>PGDATESTYLE</TT
> environment variable on the server or
     client.  The formatting function <CODE
CLASS="FUNCTION"
>to_char</CODE
>
     (see <A
HREF="functions-formatting.html"
>Section 9.8</A
>) is also available as
     a more flexible way to format the date/time output.
    </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="DATATYPE-TIMEZONES"
>8.5.3. Time Zones</A
></H2
><A
NAME="AEN4792"
></A
><P
>    Time zones, and time-zone conventions, are influenced by
    political decisions, not just earth geometry. Time zones around the
    world became somewhat standardized during the 1900's,
    but continue to be prone to arbitrary changes, particularly with
    respect to daylight-savings rules.
    <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> currently supports daylight-savings
    rules over the time period 1902 through 2038 (corresponding to the full
    range of conventional Unix system time).  Times outside that range are
    taken to be in <SPAN
CLASS="QUOTE"
>"standard time"</SPAN
> for the selected time zone, no
    matter what part of the year they fall in.
   </P
><P
>     <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> endeavors to be compatible with
     the <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> standard definitions for typical usage.
     However, the <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> standard has an odd mix of date and
     time types and capabilities. Two obvious problems are:

     <P
></P
></P><UL
><LI
><P
>        Although the <TT
CLASS="TYPE"
>date</TT
> type 
        does not have an associated time zone, the
        <TT
CLASS="TYPE"
>time</TT
> type can.
        Time zones in the real world have little meaning unless 
        associated with a date as well as a time,
        since the offset may vary through the year with daylight-saving
        time boundaries.
       </P
></LI
><LI
><P
>        The default time zone is specified as a constant numeric offset 
        from <ACRONYM
CLASS="ACRONYM"
>UTC</ACRONYM
>. It is therefore not possible to adapt to
        daylight-saving time when doing date/time arithmetic across
        <ACRONYM
CLASS="ACRONYM"
>DST</ACRONYM
> boundaries.
       </P
></LI
></UL
><P>
    </P
><P
>     To address these difficulties, we recommend using date/time types
     that contain both date and time when using time zones. We
     recommend <SPAN
CLASS="emphasis"
><I
CLASS="EMPHASIS"
>not</I
></SPAN
> using the type <TT
CLASS="TYPE"
>time with
     time zone</TT
> (though it is supported by
     <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> for legacy applications and
     for compliance with the <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> standard).
     <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> assumes
     your local time zone for any type containing only date or time.
    </P
><P
>     All timezone-aware dates and times are stored internally in
     <ACRONYM
CLASS="ACRONYM"
>UTC</ACRONYM
>.  They are converted to local time
     in the zone specified by the <A
HREF="runtime-config.html#GUC-TIMEZONE"
>timezone</A
> configuration
     parameter before being displayed to the client.
    </P
><P
>     The <A
HREF="runtime-config.html#GUC-TIMEZONE"
>timezone</A
> configuration parameter can
     be set in the file <TT
CLASS="FILENAME"
>postgresql.conf</TT
>, or in any of the
     other standard ways described in <A
HREF="runtime-config.html"
>Section 16.4</A
>.
     There are also several special ways to set it:

     <P
></P
></P><UL
><LI
><P
>        If <TT
CLASS="VARNAME"
>timezone</TT
> is not specified in
        <TT
CLASS="FILENAME"
>postgresql.conf</TT
> nor as a postmaster command-line switch,
        the server attempts to use the value of the <TT
CLASS="ENVAR"
>TZ</TT
>
        environment variable as the default time zone.  If <TT
CLASS="ENVAR"
>TZ</TT
>
        is not defined or is not any of the time zone names known to
        <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>, the server attempts to
        determine the operating system's default time zone by checking the
        behavior of the C library function <TT
CLASS="LITERAL"
>localtime()</TT
>.  The
        default time zone is selected as the closest match among
        <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
>'s known time zones.
       </P
></LI
><LI
><P
>        The <ACRONYM
CLASS="ACRONYM"
>SQL</ACRONYM
> command <TT
CLASS="COMMAND"
>SET TIME ZONE</TT
>
        sets the time zone for the session.  This is an alternative spelling
        of <TT
CLASS="COMMAND"
>SET TIMEZONE TO</TT
> with a more SQL-spec-compatible syntax.
       </P
></LI
><LI
><P
>        The <TT
CLASS="ENVAR"
>PGTZ</TT
> environment variable, if set at the
        client, is used by <SPAN
CLASS="APPLICATION"
>libpq</SPAN
>
        applications to send a <TT
CLASS="COMMAND"
>SET TIME ZONE</TT
>
        command to the server upon connection.
       </P
></LI
></UL
><P>
    </P
><P
>    Refer to <A
HREF="datetime-appendix.html"
>Appendix B</A
> for a list of
    available time zones.
   </P
></DIV
><DIV
CLASS="SECT2"
><H2
CLASS="SECT2"
><A
NAME="DATATYPE-DATETIME-INTERNALS"
>8.5.4. Internals</A
></H2
><P
>     <SPAN
CLASS="PRODUCTNAME"
>PostgreSQL</SPAN
> uses Julian dates
     for all date/time calculations. They have the nice property of correctly
     predicting/calculating any date more recent than 4713 BC
     to far into the future, using the assumption that the length of the
     year is 365.2425 days.
    </P
><P
>     Date conventions before the 19th century make for interesting reading,
     but are not consistent enough to warrant coding into a date/time handler.
    </P
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="datatype-binary.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="datatype-boolean.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Binary Data Types</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="datatype.html"
ACCESSKEY="U"
>Up</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Boolean Type</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>