Re: i := t1 - t0 but t0 + i <> t1 when t1 and t2 timestamptz values and i is an interval value

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Bryn Llewellyn <bryn(at)yugabyte(dot)com>
Cc: pgsql-general list <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: i := t1 - t0 but t0 + i <> t1 when t1 and t2 timestamptz values and i is an interval value
Date: 2021-03-26 13:36:06
Message-ID: 1578541.1616765766@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Bryn Llewellyn <bryn(at)yugabyte(dot)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

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Francisco Olarte 2021-03-26 15:01:30 Re: i := t1 - t0 but t0 + i <> t1 when t1 and t2 timestamptz values and i is an interval value
Previous Message Tim Cross 2021-03-26 06:41:32 Re: About CVE-2018-1058