aboutsummaryrefslogtreecommitdiff
path: root/src/backend/utils/adt/json.c
diff options
context:
space:
mode:
authorTom Lane <tgl@sss.pgh.pa.us>2015-10-20 11:06:24 -0700
committerTom Lane <tgl@sss.pgh.pa.us>2015-10-20 11:06:24 -0700
commit4f33572ee68b515dc2750e265fc0d0312c0d5d3d (patch)
tree7123a20d24826e90d09708102ee1af463e90ac56 /src/backend/utils/adt/json.c
parent7fc7125e21bbc1a84b8670e3d8ac7f7f4b204a9d (diff)
downloadpostgresql-4f33572ee68b515dc2750e265fc0d0312c0d5d3d.tar.gz
postgresql-4f33572ee68b515dc2750e265fc0d0312c0d5d3d.zip
Fix incorrect translation of minus-infinity datetimes for json/jsonb.
Commit bda76c1c8cfb1d11751ba6be88f0242850481733 caused both plus and minus infinity to be rendered as "infinity", which is not only wrong but inconsistent with the pre-9.4 behavior of to_json(). Fix that by duplicating the coding in date_out/timestamp_out/timestamptz_out more closely. Per bug #13687 from Stepan Perlov. Back-patch to 9.4, like the previous commit. In passing, also re-pgindent json.c, since it had gotten a bit messed up by recent patches (and I was already annoyed by indentation-related problems in back-patching this fix ...)
Diffstat (limited to 'src/backend/utils/adt/json.c')
-rw-r--r--src/backend/utils/adt/json.c36
1 files changed, 10 insertions, 26 deletions
diff --git a/src/backend/utils/adt/json.c b/src/backend/utils/adt/json.c
index 679315b6587..c3df54c018c 100644
--- a/src/backend/utils/adt/json.c
+++ b/src/backend/utils/adt/json.c
@@ -33,9 +33,6 @@
#include "utils/typcache.h"
#include "utils/syscache.h"
-/* String to output for infinite dates and timestamps */
-#define DT_INFINITY "\"infinity\""
-
/*
* The context of the parser is maintained by the recursive descent
* mechanism, but is passed explicitly to the error reporting routine
@@ -1435,19 +1432,16 @@ datum_to_json(Datum val, bool is_null, StringInfo result,
char buf[MAXDATELEN + 1];
date = DatumGetDateADT(val);
-
+ /* Same as date_out(), but forcing DateStyle */
if (DATE_NOT_FINITE(date))
- {
- /* we have to format infinity ourselves */
- appendStringInfoString(result,DT_INFINITY);
- }
+ EncodeSpecialDate(date, buf);
else
{
j2date(date + POSTGRES_EPOCH_JDATE,
&(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
EncodeDateOnly(&tm, USE_XSD_DATES, buf);
- appendStringInfo(result, "\"%s\"", buf);
}
+ appendStringInfo(result, "\"%s\"", buf);
}
break;
case JSONTYPE_TIMESTAMP:
@@ -1458,21 +1452,16 @@ datum_to_json(Datum val, bool is_null, StringInfo result,
char buf[MAXDATELEN + 1];
timestamp = DatumGetTimestamp(val);
-
+ /* Same as timestamp_out(), but forcing DateStyle */
if (TIMESTAMP_NOT_FINITE(timestamp))
- {
- /* we have to format infinity ourselves */
- appendStringInfoString(result,DT_INFINITY);
- }
+ EncodeSpecialTimestamp(timestamp, buf);
else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
- {
EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
- appendStringInfo(result, "\"%s\"", buf);
- }
else
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
+ appendStringInfo(result, "\"%s\"", buf);
}
break;
case JSONTYPE_TIMESTAMPTZ:
@@ -1484,22 +1473,17 @@ datum_to_json(Datum val, bool is_null, StringInfo result,
const char *tzn = NULL;
char buf[MAXDATELEN + 1];
- timestamp = DatumGetTimestamp(val);
-
+ timestamp = DatumGetTimestampTz(val);
+ /* Same as timestamptz_out(), but forcing DateStyle */
if (TIMESTAMP_NOT_FINITE(timestamp))
- {
- /* we have to format infinity ourselves */
- appendStringInfoString(result,DT_INFINITY);
- }
+ EncodeSpecialTimestamp(timestamp, buf);
else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
- {
EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
- appendStringInfo(result, "\"%s\"", buf);
- }
else
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
+ appendStringInfo(result, "\"%s\"", buf);
}
break;
case JSONTYPE_JSON: