]> git.ipfire.org Git - thirdparty/postgresql.git/commitdiff
Fix to_char for 1 BC. Previously it returned 1 AD.
authorBruce Momjian <bruce@momjian.us>
Tue, 30 Mar 2004 15:53:18 +0000 (15:53 +0000)
committerBruce Momjian <bruce@momjian.us>
Tue, 30 Mar 2004 15:53:18 +0000 (15:53 +0000)
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.

doc/src/sgml/func.sgml
src/backend/utils/adt/datetime.c
src/backend/utils/adt/formatting.c
src/backend/utils/adt/timestamp.c

index ccd430a97ff38aa8801360fb379c6a6f3b924257..19d357651786157e5b26e1f2f4c057704df0a7ba 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$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
 -->
 
@@ -5216,8 +5216,7 @@ SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');
       <term><literal>week</literal></term>
       <listitem>
        <para>
-        The number of
-        the week of the year that the day is in.  By definition
+        The number of the week of the year that the day is in.  By definition
         (<acronym>ISO</acronym> 8601), the first week of a year
         contains January 4 of that year.  (The <acronym>ISO</acronym>-8601
         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');
       <term><literal>year</literal></term>
       <listitem>
        <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>
 
 <screen>
index 5f340dd2a13838572843836254f142b9e1caabe0..be764ce45e287368734ca90e5119415eb86ea37c 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * 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[] = {
        {"acsst", DTZ, POS(42)},        /* Cent. Australia */
        {"acst", DTZ, NEG(16)},         /* Atlantic/Porto Acre Summer 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 */
        {"aesst", DTZ, POS(44)},        /* E. Australia */
        {"aest", TZ, POS(40)},          /* Australia Eastern Std Time */
@@ -139,7 +139,7 @@ static datetkn datetktbl[] = {
        {"azot", TZ, NEG(4)},           /* Azores Time */
        {"azst", DTZ, POS(20)},         /* Azerbaijan Summer 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 */
        {"bdt", TZ, POS(24)},           /* Dacca */
        {"bnt", TZ, POS(32)},           /* Brunei Darussalam Time */
index 25db1b0f5d3fb5dd55e0d4b5f3dc515fc5602ff5..3393a0ac4ce9682b945e15e2dfdde14b16c0f641 100644 (file)
@@ -1,7 +1,7 @@
 /* -----------------------------------------------------------------------
  * 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
@@ -169,7 +169,7 @@ static char *months_full[] = {
  * 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 A_D_STR                "A.D."
@@ -2119,7 +2119,7 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
                case DCH_B_C:
                        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;
 
                        }
@@ -2134,7 +2134,7 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
                case DCH_BC:
                        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;
 
                        }
@@ -2149,7 +2149,7 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
                case DCH_b_c:
                        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;
 
                        }
@@ -2164,7 +2164,7 @@ dch_date(int arg, char *inout, int suf, int flag, FormatNode *node, void *data)
                case DCH_bc:
                        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;
 
                        }
index 11a75cc2bd56885b1f9a9e8c52e989b6c629bdc7..dafc8ae5bedace622f141063ac7d7a4b17b28f8f 100644 (file)
@@ -8,7 +8,7 @@
  *
  *
  * 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)
                                break;
 
                        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;
 
                        case DTK_DECADE: