pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #16216: the result of to_date function with negative year number not same as BC year number
5+ messages / 3 participants
[nested] [flat]

* BUG #16216: the result of to_date function with negative year number not same as BC year number
@ 2020-01-17 12:42  PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 1 reply; 5+ messages in thread

From: PG Bug reporting form @ 2020-01-17 12:42 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: szx9231@gmail.com

The following bug has been logged on the website:

Bug reference:      16216
Logged by:          zhongxuan sun
Email address:      szx9231@gmail.com
PostgreSQL version: 12.0
Operating system:   centos7
Description:        

example:
postgres=# select to_date('-120', 'yyyy');
    to_date    
---------------
 0121-01-01 BC
(1 row)

postgres=# select to_date('120bc', 'yyyybc'); 
    to_date    
---------------
 0120-01-01 BC
(1 row)

I think -120 means 120bc, however, the output is 121bc not 120bc.

Thanks~



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #16216: the result of to_date function with negative year number not same as BC year number
@ 2020-01-17 14:17  Fabien COELHO <coelho@cri.ensmp.fr>
  parent: PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 1 reply; 5+ messages in thread

From: Fabien COELHO @ 2020-01-17 14:17 UTC (permalink / raw)
  To: szx9231@gmail.com, PostgreSQL Bugs <pgsql-bugs@lists.postgresql.org>


Hello,

> example:
> postgres=# select to_date('-120', 'yyyy');
>    to_date
> ---------------
> 0121-01-01 BC
> (1 row)
>
> postgres=# select to_date('120bc', 'yyyybc');
>    to_date
> ---------------
> 0120-01-01 BC
> (1 row)
>
> I think -120 means 120bc, however, the output is 121bc not 120bc.

ISTM that the documentation does not say that -120 is supported as meaning 
BC.

Given that there are no year 0 (zero was invented much later), history 
skips form year -1 to year 1. If -120 means in the implementation "year as 
an integer", then skipping zero would mean that it indeed represents year 
121 BC, i.e. 120 years before "Jan 1, 1 AD". So there is a logic.

BTW I found another oddity while trying strange date patterns:

   sql> SELECT DATE 'Jan 1, 0001 AD';
   # 0001-01-01

But:

   sql> SELECT DATE 'Jan 1, 1 AD';
   # 2001-01-01 # WT*?

I'll try to have a look over the week-end.

-- 
Fabien.





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #16216: the result of to_date function with negative year number not same as BC year number
@ 2020-01-17 14:59  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Fabien COELHO <coelho@cri.ensmp.fr>
  0 siblings, 1 reply; 5+ messages in thread

From: Tom Lane @ 2020-01-17 14:59 UTC (permalink / raw)
  To: Fabien COELHO <coelho@cri.ensmp.fr>; +Cc: szx9231@gmail.com, PostgreSQL Bugs <pgsql-bugs@lists.postgresql.org>

Fabien COELHO <coelho@cri.ensmp.fr> writes:
>> postgres=# select to_date('-120', 'yyyy');
>> to_date
>> ---------------
>> 0121-01-01 BC
>> (1 row)

> ISTM that the documentation does not say that -120 is supported as meaning 
> BC.

Indeed it does not.  The behavior is "correct" in terms of our internals,
as you say, but I'm a bit distressed to find that we're exposing the
internals this much.  If we do anything about this, my vote would be
to throw an error for zero or negative year field.  OTOH, the point of
to_date is mostly not to throw an error, so maybe we should leave it be.

> BTW I found another oddity while trying strange date patterns:
>    sql> SELECT DATE 'Jan 1, 0001 AD';
>    # 0001-01-01
> But:
>    sql> SELECT DATE 'Jan 1, 1 AD';
>    # 2001-01-01 # WT*?

I'm pretty sure that's intentional; if you specify two or fewer
year digits, a year between 1970 and 2069 is presumed to be meant.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #16216: the result of to_date function with negative year number not same as BC year number
@ 2020-01-17 15:15  Fabien COELHO <coelho@cri.ensmp.fr>
  parent: Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 5+ messages in thread

From: Fabien COELHO @ 2020-01-17 15:15 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: szx9231@gmail.com, PostgreSQL Bugs <pgsql-bugs@lists.postgresql.org>


>> ISTM that the documentation does not say that -120 is supported as meaning
>> BC.
>
> Indeed it does not.  The behavior is "correct" in terms of our internals,
> as you say, but I'm a bit distressed to find that we're exposing the
> internals this much.

I agree.

> If we do anything about this, my vote would be to throw an error for 
> zero or negative year field.

Yep, being stricter.

> OTOH, the point of to_date is mostly not to throw an error, so maybe we 
> should leave it be.

I'll think about it.

>> BTW I found another oddity while trying strange date patterns:
>>    sql> SELECT DATE 'Jan 1, 0001 AD';
>>    # 0001-01-01
>> But:
>>    sql> SELECT DATE 'Jan 1, 1 AD';
>>    # 2001-01-01 # WT*?
>
> I'm pretty sure that's intentional; if you specify two or fewer
> year digits, a year between 1970 and 2069 is presumed to be meant.

Hmmm. Ok, I see.

If I write a fuzzy "31/12/01", I'm pretty okay with 2001-12-31. but if I 
write '1 AD', I would expect '1 AD'.

-- 
Fabien.

^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: BUG #16216: the result of to_date function with negative year number not same as BC year number
@ 2020-01-17 15:29  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Fabien COELHO <coelho@cri.ensmp.fr>
  0 siblings, 0 replies; 5+ messages in thread

From: Tom Lane @ 2020-01-17 15:29 UTC (permalink / raw)
  To: Fabien COELHO <coelho@cri.ensmp.fr>; +Cc: szx9231@gmail.com, PostgreSQL Bugs <pgsql-bugs@lists.postgresql.org>

Fabien COELHO <coelho@cri.ensmp.fr> writes:
>> I'm pretty sure that's intentional; if you specify two or fewer
>> year digits, a year between 1970 and 2069 is presumed to be meant.

> Hmmm. Ok, I see.

> If I write a fuzzy "31/12/01", I'm pretty okay with 2001-12-31. but if I 
> write '1 AD', I would expect '1 AD'.

Yeah, a case could be made for applying that rule only when there's
exactly two year digits.  OTOH, it's been like this for a very long
time with few complaints.  Besides, it'd still be weird when you
write "11 AD", so really people who are dealing with such dates
need to learn to write four year digits.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2020-01-17 15:29 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-01-17 12:42 BUG #16216: the result of to_date function with negative year number not same as BC year number PG Bug reporting form <noreply@postgresql.org>
2020-01-17 14:17 ` Fabien COELHO <coelho@cri.ensmp.fr>
2020-01-17 14:59   ` Tom Lane <tgl@sss.pgh.pa.us>
2020-01-17 15:15     ` Fabien COELHO <coelho@cri.ensmp.fr>
2020-01-17 15:29       ` Tom Lane <tgl@sss.pgh.pa.us>

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