Re: BUG #17366: Error result returned in timestamp2timestamptz, expected to be off by one hour - Mailing list pgsql-bugs

From Francisco Olarte
Subject Re: BUG #17366: Error result returned in timestamp2timestamptz, expected to be off by one hour
Date
Msg-id CA+bJJbxLHKPOd7RUFckQ+X14+Ob+Swnqonc2m65garJRHsqdEw@mail.gmail.com
Whole thread Raw
In response to BUG #17366: Error result returned in timestamp2timestamptz, expected to be off by one hour  (PG Bug reporting form <noreply@postgresql.org>)
Responses Re: BUG #17366: Error result returned in timestamp2timestamptz, expected to be off by one hour  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-bugs
On Fri, 14 Jan 2022 at 11:15, PG Bug reporting form
<noreply@postgresql.org> wrote:
> Logged by:          Error result returned in timestamp2timestamptz, expected to be off by one
...
> postgres=# set timezone to "PST8PDT";
..
> postgres=# select (timestamp '2021-03-14 02:00:00')::timestamptz;
>  2021-03-14 03:00:00-07
> postgres=# select (timestamp '2021-03-14 03:00:00')::timestamptz;
>  2021-03-14 03:00:00-07

You are playing around the DST changes, and timestamp must have unique
representations, for informal descriptions "time jumps from 2 to 3" is
fine, when dealing with real data you must decide if it jumps just
before or just after, it seems your expectations are wrong:

=> show time zone;
 TimeZone
----------
 PST8PDT
(1 row)

=> with a(hms) as (values
('01:59:59'),('02:00:00'),('02:00:01'),('02:59:59'),('03:00:00'),('03:00:01'))
, b as (select '2021-03-14 '||hms as f from a)
select f, f::timestamp, (f::timestamp)::timestamptz from b;
          f          |          f          |           f
---------------------+---------------------+------------------------
 2021-03-14 01:59:59 | 2021-03-14 01:59:59 | 2021-03-14 01:59:59-08
 2021-03-14 02:00:00 | 2021-03-14 02:00:00 | 2021-03-14 03:00:00-07
 2021-03-14 02:00:01 | 2021-03-14 02:00:01 | 2021-03-14 03:00:01-07
 2021-03-14 02:59:59 | 2021-03-14 02:59:59 | 2021-03-14 03:59:59-07
 2021-03-14 03:00:00 | 2021-03-14 03:00:00 | 2021-03-14 03:00:00-07
 2021-03-14 03:00:01 | 2021-03-14 03:00:01 | 2021-03-14 03:00:01-07
(6 rows)

Local values do not exist in the (2-3) interval, so they are shifted,
but to cover the timestamp line without overlapping you need half-open
intervals, pg is choosing to use rigth-open, so 2:00 does not exist
but 3:00 does, they cannot both exist ( as normalized output, as input
both are valid, but internally they are stored the same way, time
jumps, pg has to choose a coherent offset to print them ).

Francisco Olarte.



pgsql-bugs by date:

Previous
From: PG Bug reporting form
Date:
Subject: BUG #17366: Error result returned in timestamp2timestamptz, expected to be off by one hour
Next
From: Tom Lane
Date:
Subject: Re: BUG #17366: Error result returned in timestamp2timestamptz, expected to be off by one hour