Re: How to get timezone offset in timestamp with time zone AT TIME ZONE output.

From: Andrew Gierth <andrew(at)tao11(dot)riddles(dot)org(dot)uk>
To: Paul McGarry <paul(at)paulmcgarry(dot)com>
Cc: pgsql-general(at)lists(dot)postgresql(dot)org
Subject: Re: How to get timezone offset in timestamp with time zone AT TIME ZONE output.
Date: 2019-09-23 08:44:19
Message-ID: 87tv93o19s.fsf@news-spur.riddles.org.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

>>>>> "Paul" == Paul McGarry <paul(at)paulmcgarry(dot)com> writes:

Paul> Hi there,
Paul> Does anyone have a good way of doing:

Paul> =====
Paul> select '2020-04-04 15:00:00+00'::timestamp with time zone AT TIME ZONE
Paul> 'Australia/Sydney';
Paul> timezone
Paul> ---------------------
Paul> 2020-04-05 02:00:00

Paul> select '2020-04-04 16:00:00+00'::timestamp with time zone AT TIME ZONE
Paul> 'Australia/Sydney';
Paul> timezone
Paul> ---------------------
Paul> 2020-04-05 02:00:00
Paul> =====

Paul> but with the output including the offset, eg:
Paul> 2020-04-05 02:00:00+11
Paul> 2020-04-05 02:00:00+10

This is ugly in some respects but minimizes the possible hazards (since
using a function-local SET clause ensures that the timezone is reset on
exit):

create function ts_to_char(t timestamptz, z text) returns text
language plpgsql immutable
set timezone = 'GMT'
as $$
begin
perform set_config('timezone', z, true);
return t::text;
end;
$$;

select ts_to_char(timestamptz '2020-04-04 16:00:00+00', 'Australia/Sydney');
ts_to_char
------------------------
2020-04-05 02:00:00+10

--
Andrew (irc:RhodiumToad)

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Francisco Olarte 2019-09-23 08:54:48 Re: How to represent a bi-directional list in db?
Previous Message Paul McGarry 2019-09-23 08:32:44 How to get timezone offset in timestamp with time zone AT TIME ZONE output.