aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/timestamp.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2019-12-12 12:30:44 -0500
committerTom Lane <tgl@sss.pgh.pa.us>2019-12-12 12:30:44 -0500
commit07c4b6ac7d6e467f9db681772b5d5101144fcfe7 (patch)
tree457c55e4dda6a6e7d2229d3d42a2c2cbcf956fc0 /src/backend/utils/adt/timestamp.c
parent73355634959bc011508a24f0e344c6f6fddf419e (diff)
downloadpostgresql-07c4b6ac7d6e467f9db681772b5d5101144fcfe7.tar.gz
postgresql-07c4b6ac7d6e467f9db681772b5d5101144fcfe7.zip
Fix EXTRACT(ISOYEAR FROM timestamp) for years BC.
The test cases added by commit 26ae3aa80 exposed an old oversight in timestamp[tz]_part: they didn't correct the result of date2isoyear() for BC years, so that we produced an off-by-one answer for such years. Fix that, and back-patch to all supported branches. Discussion: https://postgr.es/m/SG2PR06MB37762CAE45DB0F6CA7001EA9B6550@SG2PR06MB3776.apcprd06.prod.outlook.com
Diffstat (limited to 'src/backend/utils/adt/timestamp.c')
-rw-r--r--src/backend/utils/adt/timestamp.c7
1 files changed, 7 insertions, 0 deletions
diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c
index 9151d347ed9..666452d81ef 100644
--- a/src/backend/utils/adt/timestamp.c
+++ b/src/backend/utils/adt/timestamp.c
@@ -4359,6 +4359,7 @@ date2isoweek(int year, int mon, int mday)
/* date2isoyear()
*
* Returns ISO 8601 year number.
+ * Note: zero or negative results follow the year-zero-exists convention.
*/
int
date2isoyear(int year, int mon, int mday)
@@ -4633,6 +4634,9 @@ timestamp_part(PG_FUNCTION_ARGS)
case DTK_ISOYEAR:
result = date2isoyear(tm->tm_year, tm->tm_mon, tm->tm_mday);
+ /* Adjust BC years */
+ if (result <= 0)
+ result -= 1;
break;
case DTK_DOW:
@@ -4829,6 +4833,9 @@ timestamptz_part(PG_FUNCTION_ARGS)
case DTK_ISOYEAR:
result = date2isoyear(tm->tm_year, tm->tm_mon, tm->tm_mday);
+ /* Adjust BC years */
+ if (result <= 0)
+ result -= 1;
break;
case DTK_DOW: