1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-03 20:02:46 +03:00

Allow to_date/to_timestamp to recognize non-English month/day names.

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
This commit is contained in:
Tom Lane
2020-03-03 11:06:47 -05:00
parent 1810ca18bf
commit d677550493
9 changed files with 221 additions and 54 deletions

View File

@ -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);