From: Pietro Pugni <pietro.pugni@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: David G. Johnston <david.g.johnston@gmail.com>
Cc: pgsql-bugs@postgresql.org <pgsql-bugs@postgresql.org>
Subject: Re: BUG #14632: Plus and minus operators inconsistency with leap years and year intervals.
Date: Thu, 27 Apr 2017 00:44:40 +0200
Message-ID: <F0C67D01-0A6C-4822-89D5-2286DD64C75F@gmail.com> (raw)
In-Reply-To: <9066.1493241918@sss.pgh.pa.us>
References: <20170426182322.24371.95224@wrigleys.postgresql.org>
<CAKFQuwZ+PXKFrQ+LqOOorfva9nMesie0WQpe_hJTdkMn7vCZAw@mail.gmail.com>
<5752C491-B0EC-4D1E-AF97-5E1C8C793A8A@gmail.com>
<9066.1493241918@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
> Pietro Pugni <pietro.pugni@gmail.com> writes:
>> Adding 10 years to 1912-02-29 returns 1922-02-29, as expected.
>
> No, it doesn't, and it would be wrong to do so because there is no
> such date; 1922 wasn't a leap year.
I messed up 28 with 29 in that statement, sorry. Look at the queries instead, they are correct.
I’ll try to shed some light on the inconsistency of age() versus intervals used in conjunction with leap years.
The whole point (for leap years) is:
- inconsistency (adding and subtracting the same quantity to a date should provide the date itself, but it doesn't);
- wrong results (we loose days)
- ambiguity (we know that the meaning of an interval is relative to the referred date, but we have two different meanings if we add an interval to a date and obtain x and then subtract the same interval and obtain y=x-1)
The day loss can be easily seen by recursing the addition of the result returned by age:
SELECT age(age('1922-02-28'::date, '1912-02-29'::date) + '1912-02-29'::date, '1912-02-29'::date) + '1912-02-29'::date
?column?
---------------------
1922-02-24 00:00:00
This is totally wrong from a semantic point of view and it should return '1922-02-28 00:00:00'. Instead we get a total loss of 4 days.
As reported in my previous mail, adding 10 years to a leap date returns the expected value:
SELECT '1912-02-29'::date + '10 years'::interval
?column?
---------------------
1922-02-28 00:00:00
while subtracting 10 years from the returned date has a totally different meaning for Postgres and misses 1 day:
SELECT '1922-02-28'::date - '10 years'::interval
?column?
---------------------
1902-02-28 00:00:00
This is inconsistent (and wrong) against the result provided by this query:
select age('1922-02-28'::date, '1912-02-29'::date)
age
-------------------------
9 years 11 mons 28 days
I expect it to return ’10 years’ instead. It’s inconsistent also with this query:
SELECT '1912-02-29'::date + age('1922-02-28'::date, '1912-02-29'::date)
?column?
---------------------
1922-02-26 00:00:00
because it returns 1922-02-26 instead of 1922-02-28! age() returns the correct value if we look at the docs but it’s wrong from a logical point of view if we consider it for what it should be: a function returning age between dates and not the number of days.
The only workaround I see is subtracting 1 day from the “born” date:
SELECT age('1922-02-28'::date, '1912-02-29'::date - '1 day'::interval)
age
----------
10 years
but I’ll apply this caveat only to leap years, so I’ll need a function or a case statement.
>> The bug basically consists of the vague meaning of “years” applied to
>> leap years. It should be revised in order to be consistent and correct.
>
> As remarked somewhere in our documentation, you'd need to take that up
> with the Pope, not with us database hackers. We didn't invent the
> calendar rules.
Postgres does a great job with dates and this is the first bug I find, but it’s very frustrating because it leads to errors, especially when dealing with a lot of records. The docs aren’t exhaustive on this topic IMHO.
Kind regards
Pietro Pugni
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-bugs@postgresql.org
Cc: pietro.pugni@gmail.com, tgl@sss.pgh.pa.us, david.g.johnston@gmail.com
Subject: Re: BUG #14632: Plus and minus operators inconsistency with leap years and year intervals.
In-Reply-To: <F0C67D01-0A6C-4822-89D5-2286DD64C75F@gmail.com>
References: <20170426182322.24371.95224@wrigleys.postgresql.org> <CAKFQuwZ+PXKFrQ+LqOOorfva9nMesie0WQpe_hJTdkMn7vCZAw@mail.gmail.com> <5752C491-B0EC-4D1E-AF97-5E1C8C793A8A@gmail.com> <9066.1493241918@sss.pgh.pa.us> <F0C67D01-0A6C-4822-89D5-2286DD64C75F@gmail.com>
* 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