Commit 30bcebbd authored by Alvaro Herrera's avatar Alvaro Herrera

Allow negative years in make_date to represent BC years

There doesn't seem to be any reason not to allow negative years to be
interpreted as BC, so do that.

The documentation is pretty vague on the details of this function, so
nothing needs to change there.

Reported-by: Andy Abelisto, in bug #14446
parent 8b07aee8
...@@ -252,16 +252,20 @@ make_date(PG_FUNCTION_ARGS) ...@@ -252,16 +252,20 @@ make_date(PG_FUNCTION_ARGS)
struct pg_tm tm; struct pg_tm tm;
DateADT date; DateADT date;
int dterr; int dterr;
bool bc = false;
tm.tm_year = PG_GETARG_INT32(0); tm.tm_year = PG_GETARG_INT32(0);
tm.tm_mon = PG_GETARG_INT32(1); tm.tm_mon = PG_GETARG_INT32(1);
tm.tm_mday = PG_GETARG_INT32(2); tm.tm_mday = PG_GETARG_INT32(2);
/* /* Handle negative years as BC */
* Note: we'll reject zero or negative year values. Perhaps negatives if (tm.tm_year < 0)
* should be allowed to represent BC years? {
*/ bc = true;
dterr = ValidateDate(DTK_DATE_M, false, false, false, &tm); tm.tm_year = -tm.tm_year;
}
dterr = ValidateDate(DTK_DATE_M, false, false, bc, &tm);
if (dterr != 0) if (dterr != 0)
ereport(ERROR, ereport(ERROR,
......
...@@ -1454,6 +1454,12 @@ select make_date(2013, 7, 15); ...@@ -1454,6 +1454,12 @@ select make_date(2013, 7, 15);
07-15-2013 07-15-2013
(1 row) (1 row)
select make_date(-44, 3, 15);
make_date
---------------
03-15-0044 BC
(1 row)
select make_time(8, 20, 0.0); select make_time(8, 20, 0.0);
make_time make_time
----------- -----------
...@@ -1467,8 +1473,6 @@ select make_date(2013, 13, 1); ...@@ -1467,8 +1473,6 @@ select make_date(2013, 13, 1);
ERROR: date field value out of range: 2013-13-01 ERROR: date field value out of range: 2013-13-01
select make_date(2013, 11, -1); select make_date(2013, 11, -1);
ERROR: date field value out of range: 2013-11--1 ERROR: date field value out of range: 2013-11--1
select make_date(-44, 3, 15); -- perhaps we should allow this sometime?
ERROR: date field value out of range: -44-03-15
select make_time(10, 55, 100.1); select make_time(10, 55, 100.1);
ERROR: time field value out of range: 10:55:100.1 ERROR: time field value out of range: 10:55:100.1
select make_time(24, 0, 2.1); select make_time(24, 0, 2.1);
......
...@@ -338,11 +338,11 @@ SELECT EXTRACT(UNDEFINED FROM DATE 'infinity'); -- ERROR: timestamp units " ...@@ -338,11 +338,11 @@ SELECT EXTRACT(UNDEFINED FROM DATE 'infinity'); -- ERROR: timestamp units "
-- test constructors -- test constructors
select make_date(2013, 7, 15); select make_date(2013, 7, 15);
select make_date(-44, 3, 15);
select make_time(8, 20, 0.0); select make_time(8, 20, 0.0);
-- should fail -- should fail
select make_date(2013, 2, 30); select make_date(2013, 2, 30);
select make_date(2013, 13, 1); select make_date(2013, 13, 1);
select make_date(2013, 11, -1); select make_date(2013, 11, -1);
select make_date(-44, 3, 15); -- perhaps we should allow this sometime?
select make_time(10, 55, 100.1); select make_time(10, 55, 100.1);
select make_time(24, 0, 2.1); select make_time(24, 0, 2.1);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment