Re: [BUGS] BUG #1749: date_trunc('week', ...) is incorrect for some - Mailing list pgsql-patches

From Bruce Momjian
Subject Re: [BUGS] BUG #1749: date_trunc('week', ...) is incorrect for some
Date
Msg-id 200507041413.j64EDhd05231@candle.pha.pa.us
Whole thread Raw
List pgsql-patches
Nick Johnson wrote:
>
> The following bug has been logged online:
>
> Bug reference:      1749
> Logged by:          Nick Johnson
> Email address:      arachnid@notdot.net
> PostgreSQL version: 8.0.3
> Operating system:   FreeBSD
> Description:        date_trunc('week', ...) is incorrect for some dates
> Details:
>
> numa=# SELECT date_trunc('week', '2002-12-31'::date);
>        date_trunc
> ------------------------
>  2001-12-31 00:00:00-05
> (1 row)
>
> Obviously this isn't the expected behaviour...

Thanks, patch attached and applied to CVS HEAD and 8.0.X.  When we fixed
January dates that were part of the previous year, I thought their might
be December dates in the next year, but I couldn't find any so I figured
they didn't exist.

--
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 359-1001
  +  If your life is a hard drive,     |  13 Roberts Road
  +  Christ can be your backup.        |  Newtown Square, Pennsylvania 19073
Index: src/backend/utils/adt/timestamp.c
===================================================================
RCS file: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v
retrieving revision 1.128
diff -c -c -r1.128 timestamp.c
*** src/backend/utils/adt/timestamp.c    30 Jun 2005 03:48:58 -0000    1.128
--- src/backend/utils/adt/timestamp.c    4 Jul 2005 14:02:46 -0000
***************
*** 2793,2801 ****
--- 2793,2804 ----
                  /*
                   *    If it is week 52/53 and the month is January,
                   *    then the week must belong to the previous year.
+                  *    Also, some December dates belong to the next year.
                   */
                  if (woy >= 52 && tm->tm_mon == 1)
                      --tm->tm_year;
+                 if (woy <= 1 && tm->tm_mon == 12)
+                     ++tm->tm_year;
                  isoweek2date(woy, &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
                  tm->tm_hour = 0;
                  tm->tm_min = 0;
***************
*** 2924,2932 ****
--- 2927,2938 ----
                  /*
                   *    If it is week 52/53 and the month is January,
                   *    then the week must belong to the previous year.
+                  *    Also, some December dates belong to the next year.
                   */
                  if (woy >= 52 && tm->tm_mon == 1)
                      --tm->tm_year;
+                 if (woy <= 1 && tm->tm_mon == 12)
+                     ++tm->tm_year;
                  isoweek2date(woy, &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
                  tm->tm_hour = 0;
                  tm->tm_min = 0;

pgsql-patches by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: [HACKERS] Dbsize backend integration
Next
From: Bruce Momjian
Date:
Subject: Re: contrib/pgcrypto patch for OpenSSL 0.9.8