From fd071bd478f489c81208029265e1fef954a9b5fa Mon Sep 17 00:00:00 2001 From: Bruce Momjian Date: Tue, 30 Mar 2004 15:53:18 +0000 Subject: [PATCH] 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. --- doc/src/sgml/func.sgml | 8 ++++---- src/backend/utils/adt/datetime.c | 6 +++--- src/backend/utils/adt/formatting.c | 12 ++++++------ src/backend/utils/adt/timestamp.c | 8 ++++++-- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index ccd430a97f..19d3576517 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -1,5 +1,5 @@ @@ -5216,8 +5216,7 @@ SELECT EXTRACT(SECOND FROM TIME '17:12:28.5'); week - 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 (ISO 8601), the first week of a year contains January 4 of that year. (The ISO-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'); year - The year field + The year field. Keep in mind there is no 0 AD, so subtracting + BC years from AD years should be done with care. diff --git a/src/backend/utils/adt/datetime.c b/src/backend/utils/adt/datetime.c index 5f340dd2a1..be764ce45e 100644 --- a/src/backend/utils/adt/datetime.c +++ b/src/backend/utils/adt/datetime.c @@ -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 */ diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c index 25db1b0f5d..3393a0ac4c 100644 --- a/src/backend/utils/adt/formatting.c +++ b/src/backend/utils/adt/formatting.c @@ -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; } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index 11a75cc2bd..dafc8ae5be 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -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: -- 2.40.0