mirror of
https://github.com/postgres/postgres.git
synced 2025-11-06 07:49:08 +03:00
Preserve tz when converting to jsonb timestamptz
This removes an inconsistency in the treatment of different datatypes by the jsonpath timestamp_tz() function. Conversions from data types that are not timestamp-aware, such as date and timestamp, are now treated consistently with conversion from those that are such as timestamptz. Author: David Wheeler Reviewed-by: Junwang Zhao and Jeevan Chalke Discussion: https://postgr.es/m/7DE080CE-6D8C-4794-9BD1-7D9699172FAB%40justatheory.com Backpatch to release 17.
This commit is contained in:
@@ -2707,12 +2707,27 @@ executeDateTimeMethod(JsonPathExecContext *cxt, JsonPathItem *jsp,
|
||||
break;
|
||||
case jpiTimestampTz:
|
||||
{
|
||||
struct pg_tm tm;
|
||||
fsec_t fsec;
|
||||
|
||||
/* Convert result type to timestamp with time zone */
|
||||
switch (typid)
|
||||
{
|
||||
case DATEOID:
|
||||
checkTimezoneIsUsedForCast(cxt->useTz,
|
||||
"date", "timestamptz");
|
||||
|
||||
/*
|
||||
* Get the timezone value explicitly since JsonbValue
|
||||
* keeps that separate.
|
||||
*/
|
||||
j2date(DatumGetDateADT(value) + POSTGRES_EPOCH_JDATE,
|
||||
&(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
|
||||
tm.tm_hour = 0;
|
||||
tm.tm_min = 0;
|
||||
tm.tm_sec = 0;
|
||||
tz = DetermineTimeZoneOffset(&tm, session_timezone);
|
||||
|
||||
value = DirectFunctionCall1(date_timestamptz,
|
||||
value);
|
||||
break;
|
||||
@@ -2726,6 +2741,16 @@ executeDateTimeMethod(JsonPathExecContext *cxt, JsonPathItem *jsp,
|
||||
case TIMESTAMPOID:
|
||||
checkTimezoneIsUsedForCast(cxt->useTz,
|
||||
"timestamp", "timestamptz");
|
||||
|
||||
/*
|
||||
* Get the timezone value explicitly since JsonbValue
|
||||
* keeps that separate.
|
||||
*/
|
||||
if (timestamp2tm(DatumGetTimestamp(value), NULL, &tm,
|
||||
&fsec, NULL, NULL) == 0)
|
||||
tz = DetermineTimeZoneOffset(&tm,
|
||||
session_timezone);
|
||||
|
||||
value = DirectFunctionCall1(timestamp_timestamptz,
|
||||
value);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user