PgDateText
Translates the year between the way ISO-8601 writes it and the way PostgreSQL does.
The two disagree outside 0001..9999, and they disagree in a way no single string satisfies:
| Year | ISO-8601 / kotlinx | PostgreSQL |
|---|---|---|
| 2024 | 2024-01-02 | the same |
| 10000 | +10000-01-02 | 10000-01-02 |
| 1 BC | 0000-01-02 | 0001-01-02 BC |
| 2 BC | -0001-01-02 | 0002-01-02 BC |
ISO requires the sign on any year past four digits and PostgreSQL refuses it - it reads the sign as the start of a timezone offset, so +10000-01-02 and -0001-01-02 are both rejected outright, and so is 4713-01-02 BC's ISO spelling even though that is PostgreSQL's own minimum. ISO also has a year zero where PostgreSQL has none, which is where the off-by-one comes from: 1 BC directly precedes 1 AD.
So a payload written in ISO cannot be cast back to a date at all past year 9999 or before year 1. This writes PostgreSQL's form instead, and reads either back - a payload built in SQL, or one written before this existed, still decodes.
None of this widens what a column holds: date reaches 4713 BC to 5874897 AD and timestamp 294276 AD, and a value outside that is out of range whichever way it is spelled.