Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Use EncodeDateTime instead of to_char to render JSON timestamps.
authorAndrew Dunstan <andrew@dunslane.net>
Tue, 3 Jun 2014 22:26:47 +0000 (18:26 -0400)
committerAndrew Dunstan <andrew@dunslane.net>
Tue, 3 Jun 2014 22:26:47 +0000 (18:26 -0400)
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

src/backend/utils/adt/json.c
src/test/regress/expected/json.out
src/test/regress/expected/json_1.out

index 8ca1ede83fb355aefcfa472fcd181168fcfa3a0a..972a22f65e570386cb2a3048179451fae3b5fe89 100644 (file)
 #include "lib/stringinfo.h"
 #include "libpq/pqformat.h"
 #include "mb/pg_wchar.h"
+#include "miscadmin.h"
 #include "parser/parse_coerce.h"
 #include "utils/array.h"
 #include "utils/builtins.h"
-#include "utils/formatting.h"
+#include "utils/datetime.h"
 #include "utils/lsyscache.h"
 #include "utils/json.h"
 #include "utils/jsonapi.h"
@@ -63,13 +64,6 @@ typedef enum                 /* type categories for datum_to_json */
    JSONTYPE_OTHER              /* all else */
 } 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_string(JsonLexContext *lex);
 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,
            pfree(outputstr);
            break;
        case JSONTYPE_TIMESTAMP:
-           /*
-            * The timestamp format used here provides for quoting the string,
-            * so no escaping is required.
-            */
-           jsontext = DatumGetTextP(
-               DirectFunctionCall2(timestamp_to_char, val,
-                                   CStringGetTextDatum(TS_ISO8601_FMT)));
-           outputstr = text_to_cstring(jsontext);
-           appendStringInfoString(result, outputstr);
-           pfree(outputstr);
-           pfree(jsontext);
+           {
+               Timestamp   timestamp;
+               struct pg_tm tm;
+               fsec_t      fsec;
+               char        buf[MAXDATELEN + 1];
+
+               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, 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;
        case JSONTYPE_TIMESTAMPTZ:
-           /* same comment as for timestamp above */
-           jsontext = DatumGetTextP(
-               DirectFunctionCall2(timestamptz_to_char, val,
-                                   CStringGetTextDatum(TSTZ_ISO8601_FMT)));
-           outputstr = text_to_cstring(jsontext);
-           appendStringInfoString(result, outputstr);
-           pfree(outputstr);
-           pfree(jsontext);
+           {
+               TimestampTz timestamp;
+               struct pg_tm tm;
+               int         tz;
+               fsec_t      fsec;
+               const char *tzn = NULL;
+               char        buf[MAXDATELEN + 1];
+
+               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;
        case JSONTYPE_JSON:
            /* JSON and JSONB output will already be escaped */
index 43341aa9bb5b62f5f20821c80acdce91f4cc2970..8b8556b2e04cc89b29c84d3d8a64f0b3d41aa435 100644 (file)
@@ -420,9 +420,9 @@ select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
 
 SET LOCAL TIME ZONE -8;
 select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
-             to_json             
----------------------------------
- "2014-05-28T08:22:35.614298-08"
+              to_json               
+------------------------------------
+ "2014-05-28T08:22:35.614298-08:00"
 (1 row)
 
 COMMIT;
index 953324637d8736f6a8a98fad148b40c6062bd44b..b32c3ed09f37586f5ab005c1032bfd03a4313d3a 100644 (file)
@@ -420,9 +420,9 @@ select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
 
 SET LOCAL TIME ZONE -8;
 select to_json(timestamptz '2014-05-28 12:22:35.614298-04');
-             to_json             
----------------------------------
- "2014-05-28T08:22:35.614298-08"
+              to_json               
+------------------------------------
+ "2014-05-28T08:22:35.614298-08:00"
 (1 row)
 
 COMMIT;