Re: Time zone offset in to_char() - Mailing list pgsql-general

From Adrian Klaver
Subject Re: Time zone offset in to_char()
Date
Msg-id 9b76331e-0fa9-4ede-99f6-159bd9bf3da3@aklaver.com
Whole thread Raw
In response to Re: Time zone offset in to_char()  (Alban Hertroijs <a.hertroijs@nieuwestroom.nl>)
List pgsql-general
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.klaver@aklaver.com




pgsql-general by date:

Previous
From: Dominique Devienne
Date:
Subject: Re: Support for arm64 windows is absent - Would be nice to have!
Next
From: Kiran K V
Date:
Subject: Re: Issue with loading unicode characters with copy command