Commit ab14a73a authored by Andrew Dunstan's avatar Andrew Dunstan

Use EncodeDateTime instead of to_char to render JSON timestamps.

Per gripe from Peter Eisentraut and Tom Lane.

The output is slightly different, but still ISO 8601 compliant: to_char
doesn't output the minutes when time zone offset is an integer number of
hours, while EncodeDateTime outputs ":00".

The code is slightly adapted from code in xml.c
parent 0ad1a816
...@@ -21,10 +21,11 @@ ...@@ -21,10 +21,11 @@
#include "lib/stringinfo.h" #include "lib/stringinfo.h"
#include "libpq/pqformat.h" #include "libpq/pqformat.h"
#include "mb/pg_wchar.h" #include "mb/pg_wchar.h"
#include "miscadmin.h"
#include "parser/parse_coerce.h" #include "parser/parse_coerce.h"
#include "utils/array.h" #include "utils/array.h"
#include "utils/builtins.h" #include "utils/builtins.h"
#include "utils/formatting.h" #include "utils/datetime.h"
#include "utils/lsyscache.h" #include "utils/lsyscache.h"
#include "utils/json.h" #include "utils/json.h"
#include "utils/jsonapi.h" #include "utils/jsonapi.h"
...@@ -63,13 +64,6 @@ typedef enum /* type categories for datum_to_json */ ...@@ -63,13 +64,6 @@ typedef enum /* type categories for datum_to_json */
JSONTYPE_OTHER /* all else */ JSONTYPE_OTHER /* all else */
} JsonTypeCategory; } JsonTypeCategory;
/*
* to_char formats to turn timestamps and timpstamptzs into json strings
* that are ISO 8601 compliant
*/
#define TS_ISO8601_FMT "\\\"YYYY-MM-DD\"T\"HH24:MI:SS.US\\\""
#define TSTZ_ISO8601_FMT "\\\"YYYY-MM-DD\"T\"HH24:MI:SS.USOF\\\""
static inline void json_lex(JsonLexContext *lex); static inline void json_lex(JsonLexContext *lex);
static inline void json_lex_string(JsonLexContext *lex); static inline void json_lex_string(JsonLexContext *lex);
static inline void json_lex_number(JsonLexContext *lex, char *s, bool *num_err); static inline void json_lex_number(JsonLexContext *lex, char *s, bool *num_err);
...@@ -1394,27 +1388,56 @@ datum_to_json(Datum val, bool is_null, StringInfo result, ...@@ -1394,27 +1388,56 @@ datum_to_json(Datum val, bool is_null, StringInfo result,
pfree(outputstr); pfree(outputstr);
break; break;
case JSONTYPE_TIMESTAMP: case JSONTYPE_TIMESTAMP:
/* {
* The timestamp format used here provides for quoting the string, Timestamp timestamp;
* so no escaping is required. struct pg_tm tm;
*/ fsec_t fsec;
jsontext = DatumGetTextP( char buf[MAXDATELEN + 1];
DirectFunctionCall2(timestamp_to_char, val,
CStringGetTextDatum(TS_ISO8601_FMT))); timestamp = DatumGetTimestamp(val);
outputstr = text_to_cstring(jsontext);
appendStringInfoString(result, outputstr); /* XSD doesn't support infinite values */
pfree(outputstr); if (TIMESTAMP_NOT_FINITE(timestamp))
pfree(jsontext); ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range"),
errdetail("JSON does not support infinite timestamp values.")));
else if (timestamp2tm(timestamp, NULL, &tm, &fsec, NULL, NULL) == 0)
EncodeDateTime(&tm, fsec, false, 0, NULL, USE_XSD_DATES, buf);
else
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
appendStringInfo(result,"\"%s\"",buf);
}
break; break;
case JSONTYPE_TIMESTAMPTZ: case JSONTYPE_TIMESTAMPTZ:
/* same comment as for timestamp above */ {
jsontext = DatumGetTextP( TimestampTz timestamp;
DirectFunctionCall2(timestamptz_to_char, val, struct pg_tm tm;
CStringGetTextDatum(TSTZ_ISO8601_FMT))); int tz;
outputstr = text_to_cstring(jsontext); fsec_t fsec;
appendStringInfoString(result, outputstr); const char *tzn = NULL;
pfree(outputstr); char buf[MAXDATELEN + 1];
pfree(jsontext);
timestamp = DatumGetTimestamp(val);
/* XSD doesn't support infinite values */
if (TIMESTAMP_NOT_FINITE(timestamp))
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range"),
errdetail("JSON does not support infinite timestamp values.")));
else if (timestamp2tm(timestamp, &tz, &tm, &fsec, &tzn, NULL) == 0)
EncodeDateTime(&tm, fsec, true, tz, tzn, USE_XSD_DATES, buf);
else
ereport(ERROR,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
appendStringInfo(result,"\"%s\"",buf);
}
break; break;
case JSONTYPE_JSON: case JSONTYPE_JSON:
/* JSON and JSONB output will already be escaped */ /* JSON and JSONB output will already be escaped */
......
...@@ -420,9 +420,9 @@ select to_json(timestamptz '2014-05-28 12:22:35.614298-04'); ...@@ -420,9 +420,9 @@ select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
SET LOCAL TIME ZONE -8; SET LOCAL TIME ZONE -8;
select to_json(timestamptz '2014-05-28 12:22:35.614298-04'); select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
to_json to_json
--------------------------------- ------------------------------------
"2014-05-28T08:22:35.614298-08" "2014-05-28T08:22:35.614298-08:00"
(1 row) (1 row)
COMMIT; COMMIT;
......
...@@ -420,9 +420,9 @@ select to_json(timestamptz '2014-05-28 12:22:35.614298-04'); ...@@ -420,9 +420,9 @@ select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
SET LOCAL TIME ZONE -8; SET LOCAL TIME ZONE -8;
select to_json(timestamptz '2014-05-28 12:22:35.614298-04'); select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
to_json to_json
--------------------------------- ------------------------------------
"2014-05-28T08:22:35.614298-08" "2014-05-28T08:22:35.614298-08:00"
(1 row) (1 row)
COMMIT; COMMIT;
......
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