From: Laurenz Albe <laurenz.albe@cybertec.at>
To: aNullValue (Drew Stemen) <drew@anullvalue.net>
To: pgsql-general@lists.postgresql.org
Subject: Re: Returning timestamp with timezone at specified timezone irrespective of client timezone
Date: Mon, 28 Sep 2020 11:17:39 +0200
Message-ID: <634911a0f87add08d1ba44b5ceadc49bc10fe010.camel@cybertec.at> (raw)
In-Reply-To: <3af9fdd2-b622-426a-8e1e-aeab1bee8493@www.fastmail.com>
References: <3af9fdd2-b622-426a-8e1e-aeab1bee8493@www.fastmail.com>
On Sun, 2020-09-27 at 17:16 -0400, aNullValue (Drew Stemen) wrote:
> I've attempted to obtain help with this problem from several other places, but numerous
> individuals recommended I ask this mailing list.
>
> What I need is for the ability to return a timestamp with timezone, using the UTC
> offset that corresponds to a column-defined timezone, irrespective of the client/session configured timezone.
Try a function like this:
CREATE FUNCTION format_timestamp(
ts timestamp with time zone,
time_zone text
) RETURNS text
LANGUAGE plpgsql IMMUTABLE STRICT AS
$$DECLARE
tz text;
result text;
BEGIN
tz := current_setting('timezone');
PERFORM set_config('timezone', time_zone, TRUE);
result := ts AT TIME ZONE 'UTC' AT TIME ZONE 'UTC';
PERFORM set_config('timezone', tz, TRUE);
RETURN result;
END;$$;
SELECT format_timestamp(current_timestamp, '+08');
format_timestamp
-------------------------------
2020-09-28 17:15:25.083677+08
(1 row)
Yours,
Laurenz Albe
--
Cybertec | https://www.cybertec-postgresql.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: laurenz.albe@cybertec.at, drew@anullvalue.net
Subject: Re: Returning timestamp with timezone at specified timezone irrespective of client timezone
In-Reply-To: <634911a0f87add08d1ba44b5ceadc49bc10fe010.camel@cybertec.at>
References: <3af9fdd2-b622-426a-8e1e-aeab1bee8493@www.fastmail.com> <634911a0f87add08d1ba44b5ceadc49bc10fe010.camel@cybertec.at>
* 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