Re: BUG #1563: wrong week returnded by date_trunc('week', - Mailing list pgsql-bugs

From Tom Lane
Subject Re: BUG #1563: wrong week returnded by date_trunc('week',
Date
Msg-id 3279.1111908362@sss.pgh.pa.us
Whole thread Raw
In response to Re: BUG #1563: wrong week returnded by date_trunc('week',  (Robert Creager <Robert_Creager@LogicalChaos.org>)
Responses Re: BUG #1563: wrong week returnded by date_trunc('week',  (Robert Creager <Robert_Creager@LogicalChaos.org>)
List pgsql-bugs
Robert Creager <Robert_Creager@LogicalChaos.org> writes:
> "Dirk Raetzel" <d00273@spaetzle.de> confessed:
>> date_trunc('week', ...) returns the wrong week for first days in January if
>> their calendar week belongs to the previous week.

> I brought this up a couple of weeks ago in Hackers since I created this error
> last year :-(

I don't recall seeing that ... anyway, the problem seems to be that
timestamp_trunc implements this as

        case DTK_WEEK:
            isoweek2date(date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday),
                         &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
            tm->tm_hour = 0;
            tm->tm_min = 0;
            tm->tm_sec = 0;
            fsec = 0;
            break;

which looks plausible on its face ... but given 2005-01-01, date2isoweek
returns 53 --- which represents the 53rd week of 2004, which is correct
--- and then isoweek2date thinks it is supposed to compute the 53rd week
of 2005, which is not what's wanted.

We need to change the function APIs so that date2isoweek passes back
some indication of which year it thought the week belongs to, and then
isoweek2date must use that instead of the original year number.

Each of these functions is used in several places, so the change is not
quite trivial, but still not a big deal.  Who wants to fix it?

            regards, tom lane

pgsql-bugs by date:

Previous
From: Stephan Szabo
Date:
Subject: Re: BUG #1555: bug in GROUP BY?
Next
From: Tom Lane
Date:
Subject: Re: foreign key constraint not working when index tablespace is not default.