Commit fd071bd4 authored by Bruce Momjian's avatar Bruce Momjian

Fix to_char for 1 BC. Previously it returned 1 AD.

Fix to_char(year) for BC dates.  Previously it returned one less than
the current year.

Add documentation mentioning that there is no 0 AD.
parent f2c064af
<!-- <!--
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.195 2004/03/19 19:13:26 tgl Exp $ $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.196 2004/03/30 15:53:18 momjian Exp $
PostgreSQL documentation PostgreSQL documentation
--> -->
...@@ -5216,8 +5216,7 @@ SELECT EXTRACT(SECOND FROM TIME '17:12:28.5'); ...@@ -5216,8 +5216,7 @@ SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');
<term><literal>week</literal></term> <term><literal>week</literal></term>
<listitem> <listitem>
<para> <para>
The number of The number of the week of the year that the day is in. By definition
the week of the year that the day is in. By definition
(<acronym>ISO</acronym> 8601), the first week of a year (<acronym>ISO</acronym> 8601), the first week of a year
contains January 4 of that year. (The <acronym>ISO</acronym>-8601 contains January 4 of that year. (The <acronym>ISO</acronym>-8601
week starts on Monday.) In other words, the first Thursday of week starts on Monday.) In other words, the first Thursday of
...@@ -5235,7 +5234,8 @@ SELECT EXTRACT(WEEK FROM TIMESTAMP '2001-02-16 20:38:40'); ...@@ -5235,7 +5234,8 @@ SELECT EXTRACT(WEEK FROM TIMESTAMP '2001-02-16 20:38:40');
<term><literal>year</literal></term> <term><literal>year</literal></term>
<listitem> <listitem>
<para> <para>
The year field The year field. Keep in mind there is no <literal>0 AD</>, so subtracting
<literal>BC</> years from <literal>AD</> years should be done with care.
</para> </para>
<screen> <screen>
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.125 2004/02/25 19:41:23 momjian Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.126 2004/03/30 15:53:18 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -93,7 +93,7 @@ static datetkn datetktbl[] = { ...@@ -93,7 +93,7 @@ static datetkn datetktbl[] = {
{"acsst", DTZ, POS(42)}, /* Cent. Australia */ {"acsst", DTZ, POS(42)}, /* Cent. Australia */
{"acst", DTZ, NEG(16)}, /* Atlantic/Porto Acre Summer Time */ {"acst", DTZ, NEG(16)}, /* Atlantic/Porto Acre Summer Time */
{"act", TZ, NEG(20)}, /* Atlantic/Porto Acre Time */ {"act", TZ, NEG(20)}, /* Atlantic/Porto Acre Time */
{DA_D, ADBC, AD}, /* "ad" for years >= 0 */ {DA_D, ADBC, AD}, /* "ad" for years > 0 */
{"adt", DTZ, NEG(12)}, /* Atlantic Daylight Time */ {"adt", DTZ, NEG(12)}, /* Atlantic Daylight Time */
{"aesst", DTZ, POS(44)}, /* E. Australia */ {"aesst", DTZ, POS(44)}, /* E. Australia */
{"aest", TZ, POS(40)}, /* Australia Eastern Std Time */ {"aest", TZ, POS(40)}, /* Australia Eastern Std Time */
...@@ -139,7 +139,7 @@ static datetkn datetktbl[] = { ...@@ -139,7 +139,7 @@ static datetkn datetktbl[] = {
{"azot", TZ, NEG(4)}, /* Azores Time */ {"azot", TZ, NEG(4)}, /* Azores Time */
{"azst", DTZ, POS(20)}, /* Azerbaijan Summer Time */ {"azst", DTZ, POS(20)}, /* Azerbaijan Summer Time */
{"azt", TZ, POS(16)}, /* Azerbaijan Time */ {"azt", TZ, POS(16)}, /* Azerbaijan Time */
{DB_C, ADBC, BC}, /* "bc" for years < 0 */ {DB_C, ADBC, BC}, /* "bc" for years <= 0 */
{"bdst", TZ, POS(8)}, /* British Double Summer Time */ {"bdst", TZ, POS(8)}, /* British Double Summer Time */
{"bdt", TZ, POS(24)}, /* Dacca */ {"bdt", TZ, POS(24)}, /* Dacca */
{"bnt", TZ, POS(32)}, /* Brunei Darussalam Time */ {"bnt", TZ, POS(32)}, /* Brunei Darussalam Time */
......
/* ----------------------------------------------------------------------- /* -----------------------------------------------------------------------
* formatting.c * formatting.c
* *
* $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.72 2004/01/07 18:56:28 neilc Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/formatting.c,v 1.73 2004/03/30 15:53:18 momjian Exp $
* *
* *
* Portions Copyright (c) 1999-2003, PostgreSQL Global Development Group * Portions Copyright (c) 1999-2003, PostgreSQL Global Development Group
...@@ -169,7 +169,7 @@ static char *months_full[] = { ...@@ -169,7 +169,7 @@ static char *months_full[] = {
* AC / DC * AC / DC
* ---------- * ----------
*/ */
#define YEAR_ABS(_y) (_y < 0 ? -(_y -1) : _y) #define YEAR_ABS(_y) (_y <= 0 ? -(_y -1) : _y)
#define BC_STR_ORIG " BC" #define BC_STR_ORIG " BC"
#define A_D_STR "A.D." #define A_D_STR "A.D."
...@@ -2119,7 +2119,7 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data) ...@@ -2119,7 +2119,7 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
case DCH_B_C: case DCH_B_C:
if (flag == TO_CHAR) if (flag == TO_CHAR)
{ {
strcpy(inout, (tm->tm_year < 0 ? B_C_STR : A_D_STR)); strcpy(inout, (tm->tm_year <= 0 ? B_C_STR : A_D_STR));
return 3; return 3;
} }
...@@ -2134,7 +2134,7 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data) ...@@ -2134,7 +2134,7 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
case DCH_BC: case DCH_BC:
if (flag == TO_CHAR) if (flag == TO_CHAR)
{ {
strcpy(inout, (tm->tm_year < 0 ? BC_STR : AD_STR)); strcpy(inout, (tm->tm_year <= 0 ? BC_STR : AD_STR));
return 1; return 1;
} }
...@@ -2149,7 +2149,7 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data) ...@@ -2149,7 +2149,7 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
case DCH_b_c: case DCH_b_c:
if (flag == TO_CHAR) if (flag == TO_CHAR)
{ {
strcpy(inout, (tm->tm_year < 0 ? b_c_STR : a_d_STR)); strcpy(inout, (tm->tm_year <= 0 ? b_c_STR : a_d_STR));
return 3; return 3;
} }
...@@ -2164,7 +2164,7 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data) ...@@ -2164,7 +2164,7 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
case DCH_bc: case DCH_bc:
if (flag == TO_CHAR) if (flag == TO_CHAR)
{ {
strcpy(inout, (tm->tm_year < 0 ? bc_STR : ad_STR)); strcpy(inout, (tm->tm_year <= 0 ? bc_STR : ad_STR));
return 1; return 1;
} }
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.102 2004/03/22 01:38:17 tgl Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.103 2004/03/30 15:53:18 momjian Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -3261,7 +3261,11 @@ timestamp_part(PG_FUNCTION_ARGS) ...@@ -3261,7 +3261,11 @@ timestamp_part(PG_FUNCTION_ARGS)
break; break;
case DTK_YEAR: case DTK_YEAR:
result = tm->tm_year; if (tm->tm_year > 0)
result = tm->tm_year;
else
/* there is no year 0, just 1 BC and 1 AD*/
result = tm->tm_year - 1;
break; break;
case DTK_DECADE: case DTK_DECADE:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment