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 1iCxK7-0000VB-UK for pgsql-general@arkaria.postgresql.org; Wed, 25 Sep 2019 02:46:36 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iCxK5-0003FD-Iv for pgsql-general@arkaria.postgresql.org; Wed, 25 Sep 2019 02:46:33 +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 1iCxK5-0003F6-8o for pgsql-general@lists.postgresql.org; Wed, 25 Sep 2019 02:46:33 +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 1iCxK2-0007K8-DC for pgsql-general@lists.postgresql.org; Wed, 25 Sep 2019 02:46:32 +0000 Received: from [192.168.127.1] (port=44436 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtp (Exim 4.92.2 (FreeBSD)) (envelope-from ) id 1iCxJy-0004cK-Bm; Wed, 25 Sep 2019 02:46:26 +0000 Received: from localhost ([127.0.0.1]:64351 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.92.1 (FreeBSD)) (envelope-from ) id 1iCxJx-0007mw-KD; Wed, 25 Sep 2019 02:46:25 +0000 From: Andrew Gierth To: Paul McGarry Cc: Adrian Klaver , 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 "Wed, 25 Sep 2019 10:12:17 +1000") Message-ID: <875zlhnn5d.fsf@news-spur.riddles.org.uk> References: <87impio1tf.fsf@news-spur.riddles.org.uk> <91469459-36f7-41a4-09e9-c88d693cccef@aklaver.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.2 (berkeley-unix) Date: Wed, 25 Sep 2019 03:46:25 +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: > On Wed, Sep 25, 2019 at 5:44 AM Adrian Klaver > wrote: >> >> "Therefore whatever renders the offset needs to be capable of doing >> it per row, independently of the server/session time zone." The key point here is that storing the timestamp as WITHOUT rather than WITH time zone doesn't help you solve the problem of outputting the data with a different time zone offset shown for different rows. (Since timestamp without time zone values will be output without an offset, whereas timestamp with time zone values will be output with the offset corresponding to the server's timezone, which shouldn't be changing globally between rows - local changes within a function are OK.) Paul> Andrew's function seems plausible and I need to find some time to Paul> test it, but I am slightly surprised there isn't a native way to Paul> get the output, as it seems like something that would be fairly Paul> useful when dealing with dates. The set of functions for dealing with timestamps has, like most of postgres, largely grown in an unplanned fashion and therefore often has deficiencies. Also, the fact that we don't follow the spec's semantics for WITH TIME ZONE (for good reason, the spec can't handle DST boundaries or historical timezone changes _at all_) complicates choices of functions and operators to provide. Possible functions we could add: strftime('format', t [,timezone]) -- like the C function to_char(t, 'format', timezone) -- 3-arg version of existing to_char You can do a getOffset(timestamptz,timezone) function like this: create function getOffset(t timestamptz, zone text) returns interval language sql immutable as $$ select (t at time zone zone) - (t at time zone 'GMT'); $$; but formatting the interval result as text is a little more challenging due to needing explicit + signs: create function getOffsetStr(t timestamptz, zone text) returns text language sql stable as $$ select regexp_replace(to_char(getOffset(t,zone), 'HH24:MI'), '^(?!-)', '+'); $$; -- Andrew (irc:RhodiumToad)