Re: Time zone offset in to_char()

From: Alban Hertroijs <a(dot)hertroijs(at)nieuwestroom(dot)nl>
To: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>, 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 10:01:57
Message-ID: DB8P189MB104685ABEAAB813986343B4CE26F2@DB8P189MB1046.EURP189.PROD.OUTLOOK.COM
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>

> I didn’t succeed in calling SET LOCAL TIMEZONE from within the function. Could be I missed something, then Google (stackoverflow) pointed me to set_config().

CREATE OR REPLACE FUNCTION public.tz_fnc()
RETURNS void
LANGUAGE plpgsql
AS $function$
BEGIN
SET LOCAL TIMEZONE = 'UTC';
RAISE NOTICE '%', to_char(now(), 'OF');
END;
$function$
Turns out that the reason I cannot get this to work is that in my case the time zone value comes from a text parameter:

ciscache=> create or replace function ciscache.ToDatetimeOffset(ts_ timestamptz, tz_ text)
returns varchar(34)
language plpgsql
as $$
begin
--perform set_config('timezone', tz_, true /* local */);
set local timezone to tz_;
return to_char(ts_, 'YYYY-MM-DD HH24:MI:SS.US0 TZH:TZM');
end;
$$;
CREATE FUNCTION

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.

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Sébastien TANIERE 2024-01-12 10:53:32 How much size saved by updating column to NULL ?
Previous Message Alban Hertroijs 2024-01-12 08:51:38 Re: Time zone offset in to_char()