pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Paul McGarry <paul@paulmcgarry.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: How to get timezone offset in timestamp with time zone AT TIME ZONE output.
Date: Mon, 23 Sep 2019 09:44:19 +0100
Message-ID: <87tv93o19s.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <CAPrE0SYkp12Gd2Y0NVxTwvp_EpMymDa=i_vVQ3EZD11MOSJTRQ@mail.gmail.com>
References: <CAPrE0SYkp12Gd2Y0NVxTwvp_EpMymDa=i_vVQ3EZD11MOSJTRQ@mail.gmail.com>

>>>>> "Paul" == Paul McGarry <paul@paulmcgarry.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)





view thread (9+ messages)  latest in thread

Message-ID: <87tv93o19s.fsf@news-spur.riddles.org.uk>
Permalink:  ../87tv93o19s.fsf@news-spur.riddles.org.uk/
Also on:    postgresql.org/message-id/87tv93o19s.fsf@news-spur.riddles.org.uk

 · 

reply

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: andrew@tao11.riddles.org.uk, paul@paulmcgarry.com
  Subject: Re: How to get timezone offset in timestamp with time zone AT TIME ZONE output.
  In-Reply-To: <87tv93o19s.fsf@news-spur.riddles.org.uk>
  References: <CAPrE0SYkp12Gd2Y0NVxTwvp_EpMymDa=i_vVQ3EZD11MOSJTRQ@mail.gmail.com> <87tv93o19s.fsf@news-spur.riddles.org.uk>

* 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