Today, I've encountered an unexplainable result in PostgreSQL 9.6 while running this query:
SELECT age('2018-06-30','2018-05-19') AS one,
age('2018-07-01','2018-05-20') AS two;
Expected results for both columns: 1 mon 11 days
. However, only for the interval from 2018-05-19 to 2018-06-30, I get what I expect, while for 2018-05-20 till 2018-07-01 I'll get one day more: 1 mon 12 days
I don't get why this is the case and in my understanding, between 2018-05-20 2018-07-01 is just an interval of 1 mon 11 days
and the Postgres result here is wrong.
I cannot find any in-depth information on how exactly the PostgreSQL-age(timestamp,timestamp)
function works. However, I assumed that function does something like: Go from the start date in month steps forward till you reach the end month. From there, go to the day of the end date. Sum up months and days.
So, in my understanding, this is what should go on under the hood in my case (sorry, for being so verbose here, but I feel it's necessary):
Start at 2018-05-19. Go one month forward. Land at 2018-06-19. Walk N
days forward till you've reached 2018-06-30:
1 day: 20
2 days: 21
3 days: 22
4 days: 23
5 days: 24
6 days: 25
7 days: 26
8 days: 27
9 days: 28
10 days: 29
11 days: 30
= 1 month 11 days.
For the time between 2018-05-20 and 2018-07-01 it should be almost the same:
Start at 2018-05-20. Go one month forward. Land at 2018-06-20. Walk N
days forward till you've reached 2018-07-01:
1 day: 21
2 days: 22
3 days: 23
4 days: 24
5 days: 25
6 days: 26
7 days: 27
8 days: 28
9 days: 29
10 days: 30
11 days: 1
= 1 month 11 days.
Is this my mistake or one of PostgreSQL? Are there alternative functions/algorithms which work the way I described/expect?