Re: Time zone offset in to_char()

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Alban Hertroijs <a(dot)hertroijs(at)nieuwestroom(dot)nl>, Alban Hertroys <haramrae(at)gmail(dot)com>
Cc: "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: Time zone offset in to_char()
Date: 2024-01-12 17:32:53
Message-ID: 9b76331e-0fa9-4ede-99f6-159bd9bf3da3@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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(dot)klaver(at)aklaver(dot)com

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Kiran K V 2024-01-12 17:47:27 Re: Issue with loading unicode characters with copy command
Previous Message Dominique Devienne 2024-01-12 17:19:03 Re: Support for arm64 windows is absent - Would be nice to have!