Re: Returning timestamp with timezone at specified timezone irrespective of client timezone

From: Laurenz Albe <laurenz(dot)albe(at)cybertec(dot)at>
To: "aNullValue (Drew Stemen)" <drew(at)anullvalue(dot)net>, pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: Returning timestamp with timezone at specified timezone irrespective of client timezone
Date: 2020-09-28 09:17:39
Message-ID: 634911a0f87add08d1ba44b5ceadc49bc10fe010.camel@cybertec.at
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

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

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Paul Förster 2020-09-28 09:45:17 Re: Question about using ICU
Previous Message Laurenz Albe 2020-09-28 09:04:23 Re: Question about using ICU