Skip to content

Commit 6aa1263

Browse files
committed
Fix EXTRACT(ISOYEAR FROM timestamp) for years BC.
The test cases added by commit 26ae3aa 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
1 parent 8b0fd6d commit 6aa1263

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

src/backend/utils/adt/timestamp.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4348,6 +4348,7 @@ date2isoweek(int year, int mon, int mday)
43484348
/* date2isoyear()
43494349
*
43504350
* Returns ISO 8601 year number.
4351+
* Note: zero or negative results follow the year-zero-exists convention.
43514352
*/
43524353
int
43534354
date2isoyear(int year, int mon, int mday)
@@ -4556,6 +4557,9 @@ timestamp_part(PG_FUNCTION_ARGS)
45564557

45574558
case DTK_ISOYEAR:
45584559
result = date2isoyear(tm->tm_year, tm->tm_mon, tm->tm_mday);
4560+
/* Adjust BC years */
4561+
if (result <= 0)
4562+
result -= 1;
45594563
break;
45604564

45614565
case DTK_DOW:
@@ -4762,6 +4766,9 @@ timestamptz_part(PG_FUNCTION_ARGS)
47624766

47634767
case DTK_ISOYEAR:
47644768
result = date2isoyear(tm->tm_year, tm->tm_mon, tm->tm_mday);
4769+
/* Adjust BC years */
4770+
if (result <= 0)
4771+
result -= 1;
47654772
break;
47664773

47674774
case DTK_DOW:

src/test/regress/expected/timestamp.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -810,7 +810,7 @@ SELECT d1 as "timestamp",
810810
Fri Feb 14 17:32:01 1997 | 1997 | 7 | 5 | 5 | 45
811811
Sat Feb 15 17:32:01 1997 | 1997 | 7 | 6 | 6 | 46
812812
Sun Feb 16 17:32:01 1997 | 1997 | 7 | 7 | 0 | 47
813-
Tue Feb 16 17:32:01 0097 BC | -96 | 7 | 2 | 2 | 47
813+
Tue Feb 16 17:32:01 0097 BC | -97 | 7 | 2 | 2 | 47
814814
Sat Feb 16 17:32:01 0097 | 97 | 7 | 6 | 6 | 47
815815
Thu Feb 16 17:32:01 0597 | 597 | 7 | 4 | 4 | 47
816816
Tue Feb 16 17:32:01 1097 | 1097 | 7 | 2 | 2 | 47

src/test/regress/expected/timestamptz.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -889,7 +889,7 @@ SELECT d1 as timestamptz,
889889
Fri Feb 14 17:32:01 1997 PST | 1997 | 7 | 5 | 5 | 45
890890
Sat Feb 15 17:32:01 1997 PST | 1997 | 7 | 6 | 6 | 46
891891
Sun Feb 16 17:32:01 1997 PST | 1997 | 7 | 7 | 0 | 47
892-
Tue Feb 16 17:32:01 0097 PST BC | -96 | 7 | 2 | 2 | 47
892+
Tue Feb 16 17:32:01 0097 PST BC | -97 | 7 | 2 | 2 | 47
893893
Sat Feb 16 17:32:01 0097 PST | 97 | 7 | 6 | 6 | 47
894894
Thu Feb 16 17:32:01 0597 PST | 597 | 7 | 4 | 4 | 47
895895
Tue Feb 16 17:32:01 1097 PST | 1097 | 7 | 2 | 2 | 47

0 commit comments

Comments
 (0)