From d67755049388526cd8673aa826dc794b97345eb3 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 3 Mar 2020 11:06:47 -0500 Subject: Allow to_date/to_timestamp to recognize non-English month/day names. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit to_char() has long allowed the TM (translation mode) prefix to specify output of translated month or day names; but that prefix had no effect in input format strings. Now it does. to_date() and to_timestamp() will now recognize the same month or day names that to_char() would output for the same format code. Matching is case-insensitive (per the active collation's notion of what that means), just as it has always been for English month/day names without the TM prefix. (As per the discussion thread, there are lots of cases that this feature will not handle, such as alternate day names. But being able to accept what to_char() will output seems useful enough.) In passing, fix some shaky English and violations of message style guidelines in jsonpath errors for the .datetime() method, which depends on this code. Juan José Santamaría Flecha, reviewed and modified by me, with other commentary from Alvaro Herrera, Tomas Vondra, Arthur Zakirov, Peter Eisentraut, Mark Dilger. Discussion: https://postgr.es/m/CAC+AXB3u1jTngJcoC1nAHBf=M3v-jrEfo86UFtCqCjzbWS9QhA@mail.gmail.com --- src/backend/utils/adt/jsonpath_exec.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) (limited to 'src/backend/utils/adt/jsonpath_exec.c') diff --git a/src/backend/utils/adt/jsonpath_exec.c b/src/backend/utils/adt/jsonpath_exec.c index b6fdd474bec..bc063061cf2 100644 --- a/src/backend/utils/adt/jsonpath_exec.c +++ b/src/backend/utils/adt/jsonpath_exec.c @@ -1781,6 +1781,7 @@ executeDateTimeMethod(JsonPathExecContext *cxt, JsonPathItem *jsp, JsonbValue jbvbuf; Datum value; text *datetime; + Oid collid; Oid typid; int32 typmod = -1; int tz = 0; @@ -1797,6 +1798,13 @@ executeDateTimeMethod(JsonPathExecContext *cxt, JsonPathItem *jsp, datetime = cstring_to_text_with_len(jb->val.string.val, jb->val.string.len); + /* + * At some point we might wish to have callers supply the collation to + * use, but right now it's unclear that they'd be able to do better than + * DEFAULT_COLLATION_OID anyway. + */ + collid = DEFAULT_COLLATION_OID; + if (jsp->content.arg) { text *template; @@ -1814,7 +1822,7 @@ executeDateTimeMethod(JsonPathExecContext *cxt, JsonPathItem *jsp, template = cstring_to_text_with_len(template_str, template_len); - value = parse_datetime(datetime, template, true, + value = parse_datetime(datetime, template, collid, true, &typid, &typmod, &tz, jspThrowErrors(cxt) ? NULL : &have_error); @@ -1858,7 +1866,7 @@ executeDateTimeMethod(JsonPathExecContext *cxt, JsonPathItem *jsp, MemoryContextSwitchTo(oldcxt); } - value = parse_datetime(datetime, fmt_txt[i], true, + value = parse_datetime(datetime, fmt_txt[i], collid, true, &typid, &typmod, &tz, &have_error); @@ -1872,8 +1880,9 @@ executeDateTimeMethod(JsonPathExecContext *cxt, JsonPathItem *jsp, if (res == jperNotFound) RETURN_ERROR(ereport(ERROR, (errcode(ERRCODE_INVALID_ARGUMENT_FOR_JSON_DATETIME_FUNCTION), - errmsg("datetime format is not unrecognized"), - errhint("use datetime template argument for explicit format specification")))); + errmsg("datetime format is not recognized: \"%s\"", + text_to_cstring(datetime)), + errhint("Use a datetime template argument to specify the input data format.")))); } pfree(datetime); -- cgit v1.2.3