From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Alban Hertroijs <a.hertroijs@nieuwestroom.nl>
To: Alban Hertroys <haramrae@gmail.com>
Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
Subject: Re: Time zone offset in to_char()
Date: Fri, 12 Jan 2024 09:32:53 -0800
Message-ID: <9b76331e-0fa9-4ede-99f6-159bd9bf3da3@aklaver.com> (raw)
In-Reply-To: <DB8P189MB104685ABEAAB813986343B4CE26F2@DB8P189MB1046.EURP189.PROD.OUTLOOK.COM>
References: <DB8P189MB1046C0D82500690791CED07AE2682@DB8P189MB1046.EURP189.PROD.OUTLOOK.COM>
<11b56239-9f18-4b94-aeac-395648a2bfe3@aklaver.com>
<752055E6-31DA-404F-AB5E-0BAB83836108@gmail.com>
<5bd216b2-4c55-4832-8b9c-bbda9b636bd4@aklaver.com>
<DB8P189MB104685ABEAAB813986343B4CE26F2@DB8P189MB1046.EURP189.PROD.OUTLOOK.COM>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: adrian.klaver@aklaver.com, a.hertroijs@nieuwestroom.nl, haramrae@gmail.com
Subject: Re: Time zone offset in to_char()
In-Reply-To: <9b76331e-0fa9-4ede-99f6-159bd9bf3da3@aklaver.com>
References: <DB8P189MB1046C0D82500690791CED07AE2682@DB8P189MB1046.EURP189.PROD.OUTLOOK.COM> <11b56239-9f18-4b94-aeac-395648a2bfe3@aklaver.com> <752055E6-31DA-404F-AB5E-0BAB83836108@gmail.com> <5bd216b2-4c55-4832-8b9c-bbda9b636bd4@aklaver.com> <DB8P189MB104685ABEAAB813986343B4CE26F2@DB8P189MB1046.EURP189.PROD.OUTLOOK.COM> <9b76331e-0fa9-4ede-99f6-159bd9bf3da3@aklaver.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox