Index: src/backend/utils/adt/timestamp.c
===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v
retrieving revision 1.117
diff -c -r1.117 timestamp.c
*** src/backend/utils/adt/timestamp.c	31 Dec 2004 22:01:22 -0000	1.117
--- src/backend/utils/adt/timestamp.c	13 Mar 2005 17:58:18 -0000
***************
*** 2729,2735 ****
  	Timestamp timestamp = PG_GETARG_TIMESTAMP(1);
  	Timestamp	result;
  	int			type,
! 				val;
  	char	   *lowunits;
  	fsec_t		fsec;
  	struct pg_tm tt,
--- 2729,2736 ----
  	Timestamp timestamp = PG_GETARG_TIMESTAMP(1);
  	Timestamp	result;
  	int			type,
! 				val,
! 				year;
  	char	   *lowunits;
  	fsec_t		fsec;
  	struct pg_tm tt,
***************
*** 2754,2760 ****
--- 2755,2769 ----
  		switch (val)
  		{
  			case DTK_WEEK:
+ 				year = tm->tm_year;
  				isoweek2date(date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday), &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
+ 				/* the new year cannot be greater than the
+ 				 * original year, so we subtract one if it is
+ 				 */
+ 				if ( tm->tm_year > year )
+ 					{
+ 					--tm->tm_year;
+ 					}
  				tm->tm_hour = 0;
  				tm->tm_min = 0;
  				tm->tm_sec = 0;
***************
*** 2847,2853 ****
  	TimestampTz result;
  	int			tz;
  	int			type,
! 				val;
  	bool		redotz = false;
  	char	   *lowunits;
  	fsec_t		fsec;
--- 2856,2863 ----
  	TimestampTz result;
  	int			tz;
  	int			type,
! 				val,
! 				year;
  	bool		redotz = false;
  	char	   *lowunits;
  	fsec_t		fsec;
***************
*** 2874,2880 ****
--- 2884,2898 ----
  		switch (val)
  		{
  			case DTK_WEEK:
+ 				year = tm->tm_year;
  				isoweek2date(date2isoweek(tm->tm_year, tm->tm_mon, tm->tm_mday), &(tm->tm_year), &(tm->tm_mon), &(tm->tm_mday));
+ 				/* the new year cannot be greater than the
+ 				 * original year, so we subtract one if it is
+ 				 */
+ 				if ( tm->tm_year > year )
+ 					{
+ 					--tm->tm_year;
+ 					}
  				tm->tm_hour = 0;
  				tm->tm_min = 0;
  				tm->tm_sec = 0;
Index: src/test/regress/expected/timestamp.out
===================================================================
RCS file: /projects/cvsroot/pgsql/src/test/regress/expected/timestamp.out,v
retrieving revision 1.27
diff -c -r1.27 timestamp.out
*** src/test/regress/expected/timestamp.out	3 Jun 2004 02:08:06 -0000	1.27
--- src/test/regress/expected/timestamp.out	13 Mar 2005 17:58:18 -0000
***************
*** 499,508 ****
      | @ 1460 days 17 hours 32 mins 1 sec
  (54 rows)
  
! SELECT '' AS date_trunc_week, date_trunc( 'week', timestamp '2004-02-29 15:44:17.71393' ) AS week_trunc;
   date_trunc_week |        week_trunc        
  -----------------+--------------------------
!                  | Mon Feb 23 00:00:00 2004
  (1 row)
  
  -- Test casting within a BETWEEN qualifier
--- 499,508 ----
      | @ 1460 days 17 hours 32 mins 1 sec
  (54 rows)
  
! SELECT '' AS date_trunc_week, date_trunc( 'week', timestamp '2005-01-01 15:44:17.71393' ) AS week_trunc;
   date_trunc_week |        week_trunc        
  -----------------+--------------------------
!                  | Sun Jan 02 00:00:00 2005
  (1 row)
  
  -- Test casting within a BETWEEN qualifier
Index: src/test/regress/expected/timestamptz.out
===================================================================
RCS file: /projects/cvsroot/pgsql/src/test/regress/expected/timestamptz.out,v
retrieving revision 1.17
diff -c -r1.17 timestamptz.out
*** src/test/regress/expected/timestamptz.out	11 Jul 2004 04:57:20 -0000	1.17
--- src/test/regress/expected/timestamptz.out	13 Mar 2005 17:58:19 -0000
***************
*** 494,503 ****
      | @ 1460 days 17 hours 32 mins 1 sec
  (54 rows)
  
! SELECT '' AS date_trunc_week, date_trunc( 'week', timestamp with time zone '2004-02-29 15:44:17.71393' ) AS week_trunc;
   date_trunc_week |          week_trunc          
  -----------------+------------------------------
!                  | Mon Feb 23 00:00:00 2004 PST
  (1 row)
  
  -- Test casting within a BETWEEN qualifier
--- 494,503 ----
      | @ 1460 days 17 hours 32 mins 1 sec
  (54 rows)
  
! SELECT '' AS date_trunc_week, date_trunc( 'week', timestamp with time zone '2005-01-01 15:44:17.71393' ) AS week_trunc;
   date_trunc_week |          week_trunc          
  -----------------+------------------------------
!                  | Sun Jan 02 00:00:00 2005 PST
  (1 row)
  
  -- Test casting within a BETWEEN qualifier
Index: src/test/regress/sql/timestamp.sql
===================================================================
RCS file: /projects/cvsroot/pgsql/src/test/regress/sql/timestamp.sql,v
retrieving revision 1.13
diff -c -r1.13 timestamp.sql
*** src/test/regress/sql/timestamp.sql	5 Mar 2004 02:41:14 -0000	1.13
--- src/test/regress/sql/timestamp.sql	13 Mar 2005 17:58:19 -0000
***************
*** 151,157 ****
  SELECT '' AS "54", d1 - timestamp without time zone '1997-01-02' AS diff
     FROM TIMESTAMP_TBL WHERE d1 BETWEEN '1902-01-01' AND '2038-01-01';
  
! SELECT '' AS date_trunc_week, date_trunc( 'week', timestamp '2004-02-29 15:44:17.71393' ) AS week_trunc;
  
  -- Test casting within a BETWEEN qualifier
  SELECT '' AS "54", d1 - timestamp without time zone '1997-01-02' AS diff
--- 151,157 ----
  SELECT '' AS "54", d1 - timestamp without time zone '1997-01-02' AS diff
     FROM TIMESTAMP_TBL WHERE d1 BETWEEN '1902-01-01' AND '2038-01-01';
  
! SELECT '' AS date_trunc_week, date_trunc( 'week', timestamp '2005-01-01 15:44:17.71393' ) AS week_trunc;
  
  -- Test casting within a BETWEEN qualifier
  SELECT '' AS "54", d1 - timestamp without time zone '1997-01-02' AS diff
Index: src/test/regress/sql/timestamptz.sql
===================================================================
RCS file: /projects/cvsroot/pgsql/src/test/regress/sql/timestamptz.sql,v
retrieving revision 1.6
diff -c -r1.6 timestamptz.sql
*** src/test/regress/sql/timestamptz.sql	5 Mar 2004 02:41:14 -0000	1.6
--- src/test/regress/sql/timestamptz.sql	13 Mar 2005 17:58:19 -0000
***************
*** 145,151 ****
  SELECT '' AS "54", d1 - timestamp with time zone '1997-01-02' AS diff
     FROM TIMESTAMPTZ_TBL WHERE d1 BETWEEN '1902-01-01' AND '2038-01-01';
  
! SELECT '' AS date_trunc_week, date_trunc( 'week', timestamp with time zone '2004-02-29 15:44:17.71393' ) AS week_trunc;
  
  -- Test casting within a BETWEEN qualifier
  SELECT '' AS "54", d1 - timestamp with time zone '1997-01-02' AS diff
--- 145,151 ----
  SELECT '' AS "54", d1 - timestamp with time zone '1997-01-02' AS diff
     FROM TIMESTAMPTZ_TBL WHERE d1 BETWEEN '1902-01-01' AND '2038-01-01';
  
! SELECT '' AS date_trunc_week, date_trunc( 'week', timestamp with time zone '2005-01-01 15:44:17.71393' ) AS week_trunc;
  
  -- Test casting within a BETWEEN qualifier
  SELECT '' AS "54", d1 - timestamp with time zone '1997-01-02' AS diff
