pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedi := t1 - t0 but t0 + i <> t1 when t1 and t2 timestamptz values and i is an interval value
3+ messages / 3 participants
[nested] [flat]
* i := t1 - t0 but t0 + i <> t1 when t1 and t2 timestamptz values and i is an interval value
@ 2021-03-26 03:28 Bryn Llewellyn <bryn@yugabyte.com>
0 siblings, 2 replies; 3+ messages in thread
From: Bryn Llewellyn @ 2021-03-26 03:28 UTC (permalink / raw)
To: pgsql-general list <pgsql-general@lists.postgresql.org>
Given these, where the timestamptz values span the start of US/Pacific DST:
t0 timestamptz := ...;
t1 timestamptz := ...;
i_by_subtraction interval second := t1 - t0;
then this expression tests FALSE:
t0 + i_by_subtraction i = t1
There's more. We see that while the two "interval second” values '1 day 01:00:00' and '25 hours' test as equal, the results of adding each to the same timestamptz value are different.
PLEASE STATE THE RULES THAT ALLOW THE BEHAVIOR OF THE BLOCK TO BE PREDICTED.
(I searched the doc but found nothing.)
------------------------------------------------------------------------------------------
— Self-contained test case. Finishes silently in PG 13.2 showing that all assertions hold.
do $body$
declare
t0_text constant text := '2021-03-13 20:00:00';
t_dst constant text := '2021-03-14 02:00:00';
t1_text constant text := '2021-03-14 22:00:00';
t_surprise constant text := '2021-03-14 21:00:00';
-- i_expected because we spring forward to US DST between t0_text and t1_text.
i_expected constant interval second := '1 day 01:00:00';
i_25_hours constant interval second := '25 hours';
t0 timestamptz;
t1 timestamptz;
i_by_subtraction interval second;
begin
assert t0_text < t_dst and t_dst < t1_text, 'assert #1 failed';
assert t_surprise <> t1_text, 'assert #2 failed';
------------------------------------------------------------
-- Look!
assert i_expected = i_25_hours, 'assert #3 failed';
------------------------------------------------------------
set time zone 'US/Pacific';
t0 := t0_text::timestamptz;
t1 := t1_text::timestamptz;
assert t0::text = t0_text||'-08', 'assert #4 failed';
assert t1::text = t1_text||'-07', 'assert #5 failed'; --<< compare
i_by_subtraction := t1 - t0;
assert i_by_subtraction = i_expected, 'assert #6 failed';
------------------------------------------------------------
-- HERE IS THE PARADOX.
t1 := t0 + i_expected;
assert t1::text = t_surprise||'-07', 'assert #7 failed';
-- Meanwhile...
t1 := t0 + i_25_hours;
assert t1::text = t1_text||'-07', 'assert #8 failed';--<< compare (same)
------------------------------------------------------------
end;
$body$;
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: i := t1 - t0 but t0 + i <> t1 when t1 and t2 timestamptz values and i is an interval value
@ 2021-03-26 13:36 Tom Lane <tgl@sss.pgh.pa.us>
parent: Bryn Llewellyn <bryn@yugabyte.com>
1 sibling, 0 replies; 3+ messages in thread
From: Tom Lane @ 2021-03-26 13:36 UTC (permalink / raw)
To: Bryn Llewellyn <bryn@yugabyte.com>; +Cc: pgsql-general list <pgsql-general@lists.postgresql.org>
Bryn Llewellyn <bryn@yugabyte.com> writes:
> There's more. We see that while the two "interval second” values '1 day 01:00:00' and '25 hours' test as equal, the results of adding each to the same timestamptz value are different.
You're misunderstanding how it works. The whole point of these
operations is that "+ '1 day'" should give the same civil time
on the next day, even if a DST boundary intervened. Similarly,
"+ '1 month'" tries to give the same day number in the next
month, and "+ '1 year'" tries to give the same month/day in the
next year, despite varying month and year lengths. (Obviously,
there are edge cases where there is no such date, and you get some
nearby date instead.) Smaller units are taken literally though,
so you could add some large number of hours or seconds if you
don't want these behaviors.
> PLEASE STATE THE RULES THAT ALLOW THE BEHAVIOR OF THE BLOCK TO BE PREDICTED.
If you are looking for simple predictable rules, I am afraid
there is absolutely nothing about either the Gregorian calendar
or daylight-savings time that you will like.
> (I searched the doc but found nothing.)
There's a specific discussion of the DST-boundary issue on
this page:
https://www.postgresql.org/docs/current/functions-datetime.html
(search for the first mention of America/Denver). Not sure about
how well the months and years cases are documented.
regards, tom lane
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: i := t1 - t0 but t0 + i <> t1 when t1 and t2 timestamptz values and i is an interval value
@ 2021-03-26 15:01 Francisco Olarte <folarte@peoplecall.com>
parent: Bryn Llewellyn <bryn@yugabyte.com>
1 sibling, 0 replies; 3+ messages in thread
From: Francisco Olarte @ 2021-03-26 15:01 UTC (permalink / raw)
To: Bryn Llewellyn <bryn@yugabyte.com>; +Cc: pgsql-general list <pgsql-general@lists.postgresql.org>
On Fri, Mar 26, 2021 at 1:21 PM Bryn Llewellyn <bryn@yugabyte.com> wrote:
> Given these, where the timestamptz values span the start of US/Pacific DST:
> t0 timestamptz := ...;
> t1 timestamptz := ...;
> i_by_subtraction interval second := t1 - t0;
> then this expression tests FALSE:
> t0 + i_by_subtraction i = t1
> There's more. We see that while the two "interval second” values '1 day 01:00:00' and '25 hours' test as equal, the results of adding each to the same timestamptz value are different.
This is many times by dessign. Intervals have, IIRC, three parts,
seconds, days and months to make things like "3 month 2 days 1hour
from now" work as expected in "normal" context ( i.e, in which you
would expect to convert 2021-03-26 16:00:00 cia 2021-06-26 ( 3 month )
and 06-28 ( two days) to 2021-06-28 17:00, disregarding the fact that
months and days are different duration ( in Spain ) while doing it ).
It seems you want to use them as simple seconds count, like "I'm
starting a batch job which will take 25 hours to be done, when will it
end?". Like unix timestamps. If you want that, just avoid interval,
use extract epoch to convert timestamptz to a simple number of
seconds, do arithmetic there (where intervals are just plain numbers )
and convert back using to_timestamp().
It seems you expect a different behaviour from intervals to what they
are dessigned for.
Francisco Olarte.
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2021-03-26 15:01 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-03-26 03:28 i := t1 - t0 but t0 + i <> t1 when t1 and t2 timestamptz values and i is an interval value Bryn Llewellyn <bryn@yugabyte.com>
2021-03-26 13:36 ` Tom Lane <tgl@sss.pgh.pa.us>
2021-03-26 15:01 ` Francisco Olarte <folarte@peoplecall.com>
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