Skip to content

Commit 524d490

Browse files
committed
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.
1 parent 06ffce4 commit 524d490

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/backend/utils/adt/jsonpath_exec.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2707,12 +2707,27 @@ executeDateTimeMethod(JsonPathExecContext *cxt, JsonPathItem *jsp,
27072707
break;
27082708
case jpiTimestampTz:
27092709
{
2710+
struct pg_tm tm;
2711+
fsec_t fsec;
2712+
27102713
/* Convert result type to timestamp with time zone */
27112714
switch (typid)
27122715
{
27132716
case DATEOID:
27142717
checkTimezoneIsUsedForCast(cxt->useTz,
27152718
"date", "timestamptz");
2719+
2720+
/*
2721+
* Get the timezone value explicitly since JsonbValue
2722+
* keeps that separate.
2723+
*/
2724+
j2date(DatumGetDateADT(value) + POSTGRES_EPOCH_JDATE,
2725+
&(tm.tm_year), &(tm.tm_mon), &(tm.tm_mday));
2726+
tm.tm_hour = 0;
2727+
tm.tm_min = 0;
2728+
tm.tm_sec = 0;
2729+
tz = DetermineTimeZoneOffset(&tm, session_timezone);
2730+
27162731
value = DirectFunctionCall1(date_timestamptz,
27172732
value);
27182733
break;
@@ -2726,6 +2741,16 @@ executeDateTimeMethod(JsonPathExecContext *cxt, JsonPathItem *jsp,
27262741
case TIMESTAMPOID:
27272742
checkTimezoneIsUsedForCast(cxt->useTz,
27282743
"timestamp", "timestamptz");
2744+
2745+
/*
2746+
* Get the timezone value explicitly since JsonbValue
2747+
* keeps that separate.
2748+
*/
2749+
if (timestamp2tm(DatumGetTimestamp(value), NULL, &tm,
2750+
&fsec, NULL, NULL) == 0)
2751+
tz = DetermineTimeZoneOffset(&tm,
2752+
session_timezone);
2753+
27292754
value = DirectFunctionCall1(timestamp_timestamptz,
27302755
value);
27312756
break;

src/test/regress/expected/jsonb_jsonpath.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2964,7 +2964,7 @@ HINT: Use *_tz() function for time zone support.
29642964
select jsonb_path_query_tz('"2023-08-15"', '$.timestamp_tz()'); -- should work
29652965
jsonb_path_query_tz
29662966
-----------------------------
2967-
"2023-08-15T07:00:00+00:00"
2967+
"2023-08-15T00:00:00-07:00"
29682968
(1 row)
29692969

29702970
select jsonb_path_query('"12:34:56"', '$.timestamp_tz()');
@@ -3151,7 +3151,7 @@ HINT: Use *_tz() function for time zone support.
31513151
select jsonb_path_query_tz('"2023-08-15 12:34:56"', '$.timestamp_tz()'); -- should work
31523152
jsonb_path_query_tz
31533153
-----------------------------
3154-
"2023-08-15T02:34:56+00:00"
3154+
"2023-08-15T12:34:56+10:00"
31553155
(1 row)
31563156

31573157
select jsonb_path_query('"2023-08-15 12:34:56 +05:30"', '$.timestamp_tz()');

0 commit comments

Comments
 (0)