Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 54c8e82

Browse files
committed
In my mind there were two categories of open issues
a) ones that are 100% backward (such as the comment about outputting this format) and b) ones that aren't (such as deprecating the current postgresql shorthand of '1Y1M'::interval = 1 year 1 minute in favor of the ISO-8601 'P1Y1M'::interval = 1 year 1 month. Attached is a patch that addressed all the discussed issues that did not break backward compatability, including the ability to output ISO-8601 compliant intervals by setting datestyle to iso8601basic. Interval values can now be written as ISO 8601 time intervals, using the "Format with time-unit designators". This format always starts with the character 'P', followed by a string of values followed by single character time-unit designators. A 'T' separates the date and time parts of the interval. Ron Mayer
1 parent 7be614a commit 54c8e82

File tree

7 files changed

+578
-7
lines changed

7 files changed

+578
-7
lines changed

doc/src/sgml/datatype.sgml

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/datatype.sgml,v 1.135 2003/12/01 22:07:55 momjian Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/datatype.sgml,v 1.136 2003/12/20 15:32:54 momjian Exp $
33
-->
44

55
<chapter id="datatype">
@@ -1785,6 +1785,57 @@ January 8 04:05:06 1999 PST
17851785
<replaceable>p</replaceable> should be between 0 and 6, and
17861786
defaults to the precision of the input literal.
17871787
</para>
1788+
1789+
1790+
<para>
1791+
Alternatively, <type>interval</type> values can be written as
1792+
ISO 8601 time intervals, using the "Format with time-unit designators".
1793+
This format always starts with the character <literal>'P'</>, followed
1794+
by a string of values followed by single character time-unit designators.
1795+
A <literal>'T'</> separates the date and time parts of the interval.
1796+
</para>
1797+
1798+
<para>
1799+
Format: PnYnMnDTnHnMnS
1800+
</para>
1801+
<para>
1802+
In this format, <literal>'n'</> gets replaced by a number, and
1803+
<literal>Y</> represents years,
1804+
<literal>M</> (in the date part) months,
1805+
<literal>D</> months,
1806+
<literal>H</> hours,
1807+
<literal>M</> (in the time part) minutes,
1808+
and <literal>S</> seconds.
1809+
</para>
1810+
1811+
1812+
<table id="interval-example-table">
1813+
<title>Interval Example</title>
1814+
<tgroup cols="2">
1815+
<thead>
1816+
<row>
1817+
<entry>Traditional</entry>
1818+
<entry>ISO-8601 time-interval</entry>
1819+
</row>
1820+
</thead>
1821+
<tbody>
1822+
<row>
1823+
<entry>1 month</entry>
1824+
<entry>P1M</entry>
1825+
</row>
1826+
<row>
1827+
<entry>1 hour 30 minutes</entry>
1828+
<entry>PT1H30M</entry>
1829+
</row>
1830+
<row>
1831+
<entry>2 years 10 months 15 days 10 hours 30 minutes 20 seconds</entry>
1832+
<entry>P2Y10M15DT10H30M20S</entry>
1833+
</row>
1834+
</tbody>
1835+
</thead>
1836+
</table>
1837+
1838+
</para>
17881839
</sect3>
17891840

17901841
<sect3>
@@ -1941,6 +1992,11 @@ January 8 04:05:06 1999 PST
19411992
<entry>regional style</entry>
19421993
<entry>17.12.1997 07:37:16.00 PST</entry>
19431994
</row>
1995+
<row>
1996+
<entry>ISO8601basic</entry>
1997+
<entry>ISO 8601 basic format</entry>
1998+
<entry>19971217T073716-08</entry>
1999+
</row>
19442000
</tbody>
19452001
</tgroup>
19462002
</table>
@@ -1997,6 +2053,11 @@ January 8 04:05:06 1999 PST
19972053
</programlisting>
19982054
</para>
19992055

2056+
<para>
2057+
If the <varname>datestyle</> is set to iso8601basic, the interval
2058+
output is a ISO-8601 time interval with time-unit designator (like P1Y6M or PT23H59M59S).
2059+
</para>
2060+
20002061
<para>
20012062
The date/time styles can be selected by the user using the
20022063
<command>SET datestyle</command> command, the

src/backend/commands/variable.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
*
1010
*
1111
* IDENTIFICATION
12-
* $PostgreSQL: pgsql/src/backend/commands/variable.c,v 1.90 2003/11/29 19:51:48 pgsql Exp $
12+
* $PostgreSQL: pgsql/src/backend/commands/variable.c,v 1.91 2003/12/20 15:32:54 momjian Exp $
1313
*
1414
*-------------------------------------------------------------------------
1515
*/
@@ -82,7 +82,12 @@ assign_datestyle(const char *value, bool doit, bool interactive)
8282

8383
/* Ugh. Somebody ought to write a table driven version -- mjl */
8484

85-
if (strcasecmp(tok, "ISO") == 0)
85+
if (strcasecmp(tok, "ISO8601BASIC") == 0)
86+
{
87+
newDateStyle = USE_ISO8601BASIC_DATES;
88+
scnt++;
89+
}
90+
else if (strcasecmp(tok, "ISO") == 0)
8691
{
8792
newDateStyle = USE_ISO_DATES;
8893
scnt++;
@@ -198,6 +203,9 @@ assign_datestyle(const char *value, bool doit, bool interactive)
198203
case USE_ISO_DATES:
199204
strcpy(result, "ISO");
200205
break;
206+
case USE_ISO8601BASIC_DATES:
207+
strcpy(result, "ISO8601BASIC");
208+
break;
201209
case USE_SQL_DATES:
202210
strcpy(result, "SQL");
203211
break;

0 commit comments

Comments
 (0)