Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iCJxK-0003FP-Hm for pgsql-general@arkaria.postgresql.org; Mon, 23 Sep 2019 08:44:26 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iCJxI-0002TU-LH for pgsql-general@arkaria.postgresql.org; Mon, 23 Sep 2019 08:44:24 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iCJxI-0002TN-C3 for pgsql-general@lists.postgresql.org; Mon, 23 Sep 2019 08:44:24 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iCJxG-0000dF-4X for pgsql-general@lists.postgresql.org; Mon, 23 Sep 2019 08:44:24 +0000 Received: from [192.168.127.1] (port=56496 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtp (Exim 4.92.2 (FreeBSD)) (envelope-from ) id 1iCJxD-0000Xb-Nh; Mon, 23 Sep 2019 08:44:19 +0000 Received: from localhost ([127.0.0.1]:55820 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.92.1 (FreeBSD)) (envelope-from ) id 1iCJxD-0006KY-FR; Mon, 23 Sep 2019 08:44:19 +0000 From: Andrew Gierth To: Paul McGarry Cc: pgsql-general@lists.postgresql.org Subject: Re: How to get timezone offset in timestamp with time zone AT TIME ZONE output. In-Reply-To: (Paul McGarry's message of "Mon, 23 Sep 2019 18:32:44 +1000") Message-ID: <87tv93o19s.fsf@news-spur.riddles.org.uk> References: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (berkeley-unix) Date: Mon, 23 Sep 2019 09:44:19 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Paul" == Paul McGarry 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)