Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d3U9g-0008IL-Pk for pgsql-bugs@arkaria.postgresql.org; Wed, 26 Apr 2017 21:07:20 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1d3U9g-0006dH-46 for pgsql-bugs@arkaria.postgresql.org; Wed, 26 Apr 2017 21:07:20 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1d3U9f-0006WK-0E for pgsql-bugs@postgresql.org; Wed, 26 Apr 2017 21:07:19 +0000 Received: from hogranch.com ([75.101.82.47]) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d3U9c-0004qX-Az for pgsql-bugs@postgresql.org; Wed, 26 Apr 2017 21:07:17 +0000 Received: from [192.168.0.2] (porker [192.168.0.2]) by hogranch.com (8.11.6/8.11.6) with ESMTP id v3QL77i25423 for ; Wed, 26 Apr 2017 14:07:08 -0700 Subject: Re: BUG #14632: Plus and minus operators inconsistency with leap years and year intervals. To: pgsql-bugs@postgresql.org References: <20170426182322.24371.95224@wrigleys.postgresql.org> <5752C491-B0EC-4D1E-AF97-5E1C8C793A8A@gmail.com> <5f3b0581-8c2d-8323-73c7-e917da54c96c@hogranch.com> From: John R Pierce Message-ID: Date: Wed, 26 Apr 2017 14:07:03 -0700 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.0.1 MIME-Version: 1.0 In-Reply-To: <5f3b0581-8c2d-8323-73c7-e917da54c96c@hogranch.com> Content-Type: multipart/alternative; boundary="------------69543327E9F9B57C29B14A32" Content-Language: en-US X-HawgScanner-Information: Please contact the ISP for more information X-HawgScanner: Found to be clean X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------69543327E9F9B57C29B14A32 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit On 4/26/2017 1:53 PM, John R Pierce wrote: > On 4/26/2017 1:30 PM, Pietro Pugni wrote: >> Adding 10 years to 1912-02-29 returns 1922-02-29, as expected. >> I would like to apply the *reverse* operation. To do so, I subtract >> 10 years from 1922-02-29 but I obtain 1912-02-28, so *the math is >> actually wrong*. > > > assuming 1922 was a leap year, 1912 is NOT a leap year, so therefore > there is no 1912-02-29, that is an invalid date. > ok, I got that backwards. 1912 is the leap year. date arithmetic is not guaranteed to be associative or commutative due to the irregular units involved. pierce=# SELECT date '1912-02-29'; date ------------ 1912-02-29 (1 row) pierce=# SELECT date '1922-02-29'; ERROR: date/time field value out of range: "1922-02-29" LINE 1: SELECT date '1922-02-29'; ^ pierce=# SELECT date '1912-02-29' - interval '10 years'; ?column? --------------------- 1902-02-28 00:00:00 (1 row) pierce=# SELECT date '1912-02-29' + interval '10 years'; ?column? --------------------- 1922-02-28 00:00:00 (1 row) as an even more extreme case... pierce=# SELECT date '2017-04-30' - interval '2 months'; ?column? --------------------- 2017-02-28 00:00:00 (1 row) -- john r pierce, recycling bits in santa cruz --------------69543327E9F9B57C29B14A32 Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit
On 4/26/2017 1:53 PM, John R Pierce wrote:
On 4/26/2017 1:30 PM, Pietro Pugni wrote:
Adding 10 years to 1912-02-29 returns 1922-02-29, as expected.
I would like to apply the reverse operation. To do so, I subtract 10 years from 1922-02-29 but I obtain 1912-02-28, so the math is actually wrong.


assuming 1922 was a leap year, 1912 is NOT a leap year, so therefore there is no 1912-02-29, that is an invalid date.

ok, I got that backwards. 1912 is the leap year.

date arithmetic is not guaranteed to be associative or commutative due to the irregular units involved.

pierce=# SELECT date '1912-02-29';
    date   
------------
 1912-02-29
(1 row)

pierce=# SELECT date '1922-02-29';
ERROR:  date/time field value out of range: "1922-02-29"
LINE 1: SELECT date '1922-02-29';
               ^
pierce=# SELECT date '1912-02-29' - interval '10 years';
      ?column?      
---------------------
 1902-02-28 00:00:00
(1 row)

pierce=# SELECT date '1912-02-29' + interval '10 years';
      ?column?      
---------------------
 1922-02-28 00:00:00
(1 row)

as an even more extreme case...

pierce=# SELECT date '2017-04-30' - interval '2 months';
      ?column?      
---------------------
 2017-02-28 00:00:00
(1 row)



-- 
john r pierce, recycling bits in santa cruz
--------------69543327E9F9B57C29B14A32--