From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Paul McGarry <paul@paulmcgarry.com>
Cc: Adrian Klaver <adrian.klaver@aklaver.com>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: How to get timezone offset in timestamp with time zone AT TIME ZONE output.
Date: Wed, 25 Sep 2019 03:46:25 +0100
Message-ID: <875zlhnn5d.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <CAPrE0SbBcD+adG_YLpphu3CjpVZUp7bXqJ6vxvxySX2vGpGMNw@mail.gmail.com>
References: <CAPrE0SYkp12Gd2Y0NVxTwvp_EpMymDa=i_vVQ3EZD11MOSJTRQ@mail.gmail.com>
<dd0f5c95-08e0-f198-b59a-704377ccdcf7@aklaver.com>
<87impio1tf.fsf@news-spur.riddles.org.uk>
<91469459-36f7-41a4-09e9-c88d693cccef@aklaver.com>
<CAPrE0SbBcD+adG_YLpphu3CjpVZUp7bXqJ6vxvxySX2vGpGMNw@mail.gmail.com>
>>>>> "Paul" == Paul McGarry <paul@paulmcgarry.com> writes:
> On Wed, Sep 25, 2019 at 5:44 AM Adrian Klaver <adrian.klaver@aklaver.com>
> 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)
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, adrian.klaver@aklaver.com
Subject: Re: How to get timezone offset in timestamp with time zone AT TIME ZONE output.
In-Reply-To: <875zlhnn5d.fsf@news-spur.riddles.org.uk>
References: <CAPrE0SYkp12Gd2Y0NVxTwvp_EpMymDa=i_vVQ3EZD11MOSJTRQ@mail.gmail.com> <dd0f5c95-08e0-f198-b59a-704377ccdcf7@aklaver.com> <87impio1tf.fsf@news-spur.riddles.org.uk> <91469459-36f7-41a4-09e9-c88d693cccef@aklaver.com> <CAPrE0SbBcD+adG_YLpphu3CjpVZUp7bXqJ6vxvxySX2vGpGMNw@mail.gmail.com> <875zlhnn5d.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