Re: BUG #16419: wrong parsing BC year in to_date() function

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: دار الآثار للنشر والتوزيع-صنعاء Dar Alathar-Yemen <dar_alathar(at)hotmail(dot)com>
Cc: "pgsql-bugs(at)lists(dot)postgresql(dot)org" <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Subject: Re: BUG #16419: wrong parsing BC year in to_date() function
Date: 2020-05-07 05:05:10
Message-ID: CAKFQuwba2CO2GVKiR351pwCwP0jk82+0GaG_sMcdYUWUbON3UQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs pgsql-hackers

On Wed, May 6, 2020 at 8:12 PM David G. Johnston <david(dot)g(dot)johnston(at)gmail(dot)com>
wrote:

> It does this seemingly by subtracting one from the year, making it
> positive, then (I infer) appending "BC" to the result. Thus for the year
> "-1" it yields "0002-01-01 BC"
>
>
Specifically:

https://github.com/postgres/postgres/blob/fb544735f11480a697fcab791c058adc166be1fa/src/backend/utils/adt/formatting.c#L236

/*
* There is no 0 AD. Years go from 1 BC to 1 AD, so we make it
* positive and map year == -1 to year zero, and shift all negative
* years up one. For interval years, we just return the year.
*/
#define ADJUST_YEAR(year, is_interval) ((is_interval) ? (year) : ((year) <=
0 ? -((year) - 1) : (year)))

The code comment took me a bit to process - seems like the following would
be better (if its right - I don't know why interval is a pure no-op while
non-interval normalizes to a positive integer).

Years go from 1 BC to 1 AD, so we adjust the year zero, and all negative
years, by shifting them away one year, We then return the positive value
of the result because the caller tracks the BC/AD aspect of the year
separately and only deals with positive year values coming out of this
macro. Intervals denote the distance away from 0 a year is so we can
simply take the supplied value and return it. Interval processing code
expects a negative result for intervals going into BC.

David J.

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Fujii Masao 2020-05-07 06:43:40 Re: Back-patch is necessary? Re: Don't try fetching future segment of a TLI.
Previous Message David G. Johnston 2020-05-07 03:12:20 Re: BUG #16419: wrong parsing BC year in to_date() function

Browse pgsql-hackers by date

  From Date Subject
Next Message Victor Wagner 2020-05-07 05:10:18 Re: Postgres Windows build system doesn't work with python installed in Program Files
Previous Message Victor Wagner 2020-05-07 05:04:53 Re: Postgres Windows build system doesn't work with python installed in Program Files