On 1/12/24 02:01, Alban Hertroijs wrote:

ciscache=> select ToDatetimeOffset(current_timestamp, 'Europe/Amsterdam'), to_char(current_timestamp, 'YYYY-MM-DD HH24:MI:SS.US0 TZH:TZM');
ERROR:  invalid value for parameter "TimeZone": "tz_"
CONTEXT:  SQL statement "set local timezone to tz_"
PL/pgSQL function todatetimeoffset(timestamp with time zone,text) line 4 at SQL statement

But set_config() doesn't have that issue.

An example of how to deal with this:

CREATE OR REPLACE FUNCTION public.todatetimeoffset(ts_ timestamp with time zone, tz_ text)
 RETURNS character varying
 LANGUAGE plpgsql
AS $function$
begin
        --perform set_config('timezone', tz_, true /* local */);
        EXECUTE 'set local timezone to ' || quote_literal(tz_);
        return to_char(ts_, 'YYYY-MM-DD HH24:MI:SS.US0 TZH:TZM');
end;
$function$

select ToDatetimeOffset(now(), 'Europe/Amsterdam');
          todatetimeoffset
------------------------------------
 2024-01-12 18:32:02.5486820 +01:00

--
Adrian Klaver
adrian.kla...@aklaver.com



Reply via email to