Skip to content

Preserve tz when converting to jsonb timestamptz #7

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/backend/utils/adt/jsonpath_exec.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 it 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;
Expand All @@ -2726,6 +2741,16 @@ executeDateTimeMethod(JsonPathExecContext *cxt, JsonPathItem *jsp,
case TIMESTAMPOID:
checkTimezoneIsUsedForCast(cxt->useTz,
"timestamp", "timestamptz");

/*
* Get the timezone value explicitly since JsonbValue
* keeps it separate.
*/
if (timestamp2tm(DatumGetTimestamp(value), NULL, &tm,
&fsec, NULL, NULL) == 0)
tz = DetermineTimeZoneOffset(&tm,
session_timezone);

value = DirectFunctionCall1(timestamp_timestamptz,
value);
break;
Expand Down
4 changes: 2 additions & 2 deletions src/test/regress/expected/jsonb_jsonpath.out
Original file line number Diff line number Diff line change
Expand Up @@ -2964,7 +2964,7 @@ HINT: Use *_tz() function for time zone support.
select jsonb_path_query_tz('"2023-08-15"', '$.timestamp_tz()'); -- should work
jsonb_path_query_tz
-----------------------------
"2023-08-15T07:00:00+00:00"
"2023-08-15T00:00:00-07:00"
(1 row)

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

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