Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c5232dc

Browse files
committed
Fix handling of BC years in to_date/to_timestamp.
Previously, a conversion such as to_date('-44-02-01','YYYY-MM-DD') would result in '0045-02-01 BC', as the code attempted to interpret the negative year as BC, but failed to apply the correction needed for our internal handling of BC years. Fix the off-by-one problem. Also, arrange for the combination of a negative year and an explicit "BC" marker to cancel out and produce AD. This is how the negative-century case works, so it seems sane to do likewise. Continue to read "year 0000" as 1 BC. Oracle would throw an error, but we've accepted that case for a long time so I'm hesitant to change it in a back-patch. Per bug #16419 from Saeed Hubaishan. Back-patch to all supported branches. Dar Alathar-Yemen and Tom Lane Discussion: https://postgr.es/m/16419-d8d9db0a7553f01b@postgresql.org
1 parent 5c7afb4 commit c5232dc

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

doc/src/sgml/func.sgml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6692,6 +6692,15 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}');
66926692
</para>
66936693
</listitem>
66946694

6695+
<listitem>
6696+
<para>
6697+
In <function>to_timestamp</function> and <function>to_date</function>,
6698+
negative years are treated as signifying BC. If you write both a
6699+
negative year and an explicit <literal>BC</literal> field, you get AD
6700+
again. An input of year zero is treated as 1 BC.
6701+
</para>
6702+
</listitem>
6703+
66956704
<listitem>
66966705
<para>
66976706
In <function>to_timestamp</function> and <function>to_date</function>,

src/backend/utils/adt/formatting.c

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3881,8 +3881,11 @@ do_to_timestamp(text *date_txt, text *fmt,
38813881
{
38823882
/* If a 4-digit year is provided, we use that and ignore CC. */
38833883
tm->tm_year = tmfc.year;
3884-
if (tmfc.bc && tm->tm_year > 0)
3885-
tm->tm_year = -(tm->tm_year - 1);
3884+
if (tmfc.bc)
3885+
tm->tm_year = -tm->tm_year;
3886+
/* correct for our representation of BC years */
3887+
if (tm->tm_year < 0)
3888+
tm->tm_year++;
38863889
}
38873890
fmask |= DTK_M(YEAR);
38883891
}

src/test/regress/expected/horology.out

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2837,6 +2837,45 @@ SELECT to_date('2458872', 'J');
28372837
01-23-2020
28382838
(1 row)
28392839

2840+
--
2841+
-- Check handling of BC dates
2842+
--
2843+
SELECT to_date('44-02-01 BC','YYYY-MM-DD BC');
2844+
to_date
2845+
---------------
2846+
02-01-0044 BC
2847+
(1 row)
2848+
2849+
SELECT to_date('-44-02-01','YYYY-MM-DD');
2850+
to_date
2851+
---------------
2852+
02-01-0044 BC
2853+
(1 row)
2854+
2855+
SELECT to_date('-44-02-01 BC','YYYY-MM-DD BC');
2856+
to_date
2857+
------------
2858+
02-01-0044
2859+
(1 row)
2860+
2861+
SELECT to_timestamp('44-02-01 11:12:13 BC','YYYY-MM-DD HH24:MI:SS BC');
2862+
to_timestamp
2863+
---------------------------------
2864+
Fri Feb 01 11:12:13 0044 PST BC
2865+
(1 row)
2866+
2867+
SELECT to_timestamp('-44-02-01 11:12:13','YYYY-MM-DD HH24:MI:SS');
2868+
to_timestamp
2869+
---------------------------------
2870+
Fri Feb 01 11:12:13 0044 PST BC
2871+
(1 row)
2872+
2873+
SELECT to_timestamp('-44-02-01 11:12:13 BC','YYYY-MM-DD HH24:MI:SS BC');
2874+
to_timestamp
2875+
------------------------------
2876+
Mon Feb 01 11:12:13 0044 PST
2877+
(1 row)
2878+
28402879
--
28412880
-- Check handling of multiple spaces in format and/or input
28422881
--
@@ -3096,6 +3135,12 @@ SELECT to_date('2016 366', 'YYYY DDD'); -- ok
30963135

30973136
SELECT to_date('2016 367', 'YYYY DDD');
30983137
ERROR: date/time field value out of range: "2016 367"
3138+
SELECT to_date('0000-02-01','YYYY-MM-DD'); -- allowed, though it shouldn't be
3139+
to_date
3140+
---------------
3141+
02-01-0001 BC
3142+
(1 row)
3143+
30993144
--
31003145
-- Check behavior with SQL-style fixed-GMT-offset time zone (cf bug #8572)
31013146
--

src/test/regress/sql/horology.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,17 @@ SELECT to_date('1 4 1902', 'Q MM YYYY'); -- Q is ignored
417417
SELECT to_date('3 4 21 01', 'W MM CC YY');
418418
SELECT to_date('2458872', 'J');
419419

420+
--
421+
-- Check handling of BC dates
422+
--
423+
424+
SELECT to_date('44-02-01 BC','YYYY-MM-DD BC');
425+
SELECT to_date('-44-02-01','YYYY-MM-DD');
426+
SELECT to_date('-44-02-01 BC','YYYY-MM-DD BC');
427+
SELECT to_timestamp('44-02-01 11:12:13 BC','YYYY-MM-DD HH24:MI:SS BC');
428+
SELECT to_timestamp('-44-02-01 11:12:13','YYYY-MM-DD HH24:MI:SS');
429+
SELECT to_timestamp('-44-02-01 11:12:13 BC','YYYY-MM-DD HH24:MI:SS BC');
430+
420431
--
421432
-- Check handling of multiple spaces in format and/or input
422433
--
@@ -500,6 +511,7 @@ SELECT to_date('2015 366', 'YYYY DDD');
500511
SELECT to_date('2016 365', 'YYYY DDD'); -- ok
501512
SELECT to_date('2016 366', 'YYYY DDD'); -- ok
502513
SELECT to_date('2016 367', 'YYYY DDD');
514+
SELECT to_date('0000-02-01','YYYY-MM-DD'); -- allowed, though it shouldn't be
503515

504516
--
505517
-- Check behavior with SQL-style fixed-GMT-offset time zone (cf bug #8572)

0 commit comments

Comments
 (0)