From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Bryn Llewellyn <bryn@yugabyte.com>
Cc: Tom Lane PostgreSQL <tgl@sss.pgh.pa.us>
Cc: pgsql-general list <pgsql-general@lists.postgresql.org>
Subject: Re: to_date() and to_timestamp() with negative years
Date: Wed, 3 Nov 2021 21:42:01 -0700
Message-ID: <e1c27b01-e7c4-a5dc-ff2c-dcd0fccb1e4f@aklaver.com> (raw)
In-Reply-To: <1EA95C37-F94C-4914-A7B1-E5C7FC65CA95@yugabyte.com>
References: <A7385FDD-CC12-44DD-BEE8-8E8AD5C69EF5@yugabyte.com>
<138332.1635962086@sss.pgh.pa.us>
<08E63AD5-8E6D-4140-AE1C-9DC22324A287@yugabyte.com>
<72204b8f-41b2-0004-b09b-842f8c57477f@aklaver.com>
<0B75398B-91CC-4074-B77A-390AA157685B@yugabyte.com>
<c105f567-aa51-8eee-d272-ef472ca6c9dc@aklaver.com>
<265331.1635978234@sss.pgh.pa.us>
<745E3531-6791-4964-AF68-BC82FC2EC864@yugabyte.com>
<e3f68515-6925-da9a-004c-cd206e730aeb@aklaver.com>
<30ba14d1-31fc-31ac-aaff-ac42589d7a07@aklaver.com>
<1EA95C37-F94C-4914-A7B1-E5C7FC65CA95@yugabyte.com>
On 11/3/21 19:12, Bryn Llewellyn wrote:
>> /adrian.klaver@aklaver.com <mailto:adrian.klaver@aklaver.com> wrote:/
>>
> Back to the point about separators, the "Current" doc has this bullet:
>
> «
> A separator (a space or non-letter/non-digit character) in the template
> string of to_timestamp and to_date matches any single separator in
> the input string or is skipped, unless the FX option is used...
> »
>
> (There's no such explanation in the Version 11 doc—but never mind that.)
> I read this to mean that a space IS a viable separator. And yet Tom's
> "nope, the space doesn't count [as a separator]" claims the opposite.
> The bullet's wording, by the way, says that the minus sign is a
> separator. But yet it can also be taken to confer the meaning "BC" to a
> year. No wonder I'm confused.
>
> Elsewhere the "Current" doc says that runs of two or more spaces have
> the same effect as a single space (in the absence of FX or FM complexity).
>
> No wonder that examples like I showed sometimes produce the wrong
> results, even after more than one round of tinkering with the C
> implementation. Try this test (the to-be-converted text has runs of five
> spaces, and the template has runs of ten spaces):
It comes down to determining what is a separator and what is the
negative sign.
>
> select to_date(' 1950 02 14', 'YYYY MM DD');
> select to_date(' -1950 02 14', 'YYYY MM DD');
The row above returns:
select to_date(' -1950 02 14', 'YYYY MM DD');
to_date
---------------
1950-02-14 BC
As you state below.
Change it to:
select to_date(' -1950 02 14', ' YYYY MM DD');
to_date
------------
1950-02-14
and you don't get the BC as -1950 is taken as separator(-)1950 not
negative(-)1950.
>
> select to_date(' 14 02 1950', 'DD MM YYYY');
> select to_date(' 14 02 -1950', 'DD MM YYYY');
The above returns:
select to_date(' 14 02 -1950', 'DD MM YYYY');
to_date
------------
1950-02-14
Change it to:
select to_date(' 14 02 -1950', 'DD MMYYYY');
to_date
---------------
1950-02-14 BC
and you get BC as -1950 is taken as negative(-)1950 not separator(-)1950.
Not sure how this can be handled in a totally predictable way given the
unpredictable ways in which datetime strings are formatted?
The only thing I can say it is it points out that when working with
datetimes settling on a standard format is your best defense against
unpredictable results.
>
> "-1950" is taken as "BC" in the second to_date() but it is not so taken
> in the fourth to_date().
>
--
Adrian Klaver
adrian.klaver@aklaver.com
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-general@postgresql.org
Cc: adrian.klaver@aklaver.com, bryn@yugabyte.com, tgl@sss.pgh.pa.us
Subject: Re: to_date() and to_timestamp() with negative years
In-Reply-To: <e1c27b01-e7c4-a5dc-ff2c-dcd0fccb1e4f@aklaver.com>
References: <A7385FDD-CC12-44DD-BEE8-8E8AD5C69EF5@yugabyte.com> <138332.1635962086@sss.pgh.pa.us> <08E63AD5-8E6D-4140-AE1C-9DC22324A287@yugabyte.com> <72204b8f-41b2-0004-b09b-842f8c57477f@aklaver.com> <0B75398B-91CC-4074-B77A-390AA157685B@yugabyte.com> <c105f567-aa51-8eee-d272-ef472ca6c9dc@aklaver.com> <265331.1635978234@sss.pgh.pa.us> <745E3531-6791-4964-AF68-BC82FC2EC864@yugabyte.com> <e3f68515-6925-da9a-004c-cd206e730aeb@aklaver.com> <30ba14d1-31fc-31ac-aaff-ac42589d7a07@aklaver.com> <1EA95C37-F94C-4914-A7B1-E5C7FC65CA95@yugabyte.com> <e1c27b01-e7c4-a5dc-ff2c-dcd0fccb1e4f@aklaver.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