Per a brief conversation with Tom, I've created a patch for adding

support for 'week' within the date_trunc function.

Within the patch I added a couple of test cases and associated target
output, and changed the documentation to add 'week' appropriately.

Robert Creager
This commit is contained in:
Bruce Momjian 2004-03-05 02:41:14 +00:00
parent 44611f6e6d
commit 1973971821
6 changed files with 35 additions and 4 deletions

View File

@ -1,5 +1,5 @@
<!--
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.190 2004/03/04 20:09:29 dennis Exp $
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.191 2004/03/05 02:41:14 momjian Exp $
PostgreSQL documentation
-->
@ -5309,6 +5309,7 @@ date_trunc('<replaceable>field</replaceable>', <replaceable>source</replaceable>
<member><literal>minute</literal></member>
<member><literal>hour</literal></member>
<member><literal>day</literal></member>
<member><literal>week</literal></member>
<member><literal>month</literal></member>
<member><literal>year</literal></member>
<member><literal>decade</literal></member>

View File

@ -8,7 +8,7 @@
*
*
* IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.99 2004/02/14 20:16:17 tgl Exp $
* $PostgreSQL: pgsql/src/backend/utils/adt/timestamp.c,v 1.100 2004/03/05 02:41:14 momjian Exp $
*
*-------------------------------------------------------------------------
*/
@ -2564,6 +2564,13 @@ timestamp_trunc(PG_FUNCTION_ARGS)
switch (val)
{
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;
case DTK_MILLENNIUM:
tm->tm_year = (tm->tm_year / 1000) * 1000;
case DTK_CENTURY:
@ -2672,6 +2679,13 @@ timestamptz_trunc(PG_FUNCTION_ARGS)
switch (val)
{
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;
case DTK_MILLENNIUM:
tm->tm_year = (tm->tm_year / 1000) * 1000;
case DTK_CENTURY:

View File

@ -499,6 +499,12 @@ SELECT '' AS "54", d1 - timestamp without time zone '1997-01-02' AS diff
| @ 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
SELECT '' AS "54", d1 - timestamp without time zone '1997-01-02' AS diff
FROM TIMESTAMP_TBL

View File

@ -494,6 +494,12 @@ SELECT '' AS "54", d1 - timestamp with time zone '1997-01-02' AS diff
| @ 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
SELECT '' AS "54", d1 - timestamp with time zone '1997-01-02' AS diff
FROM TIMESTAMPTZ_TBL
@ -1317,9 +1323,9 @@ SELECT '' AS to_char_9, to_char(d1, 'YYYY A.D. YYYY a.d. YYYY bc HH:MI:SS P.M. H
| 2001 A.D. 2001 a.d. 2001 ad 05:32:01 P.M. 05:32:01 p.m. 05:32:01 pm
(64 rows)
SELECT '' AS to_char_10, to_char(d1, 'YYYY WW IYYY IYY IY I IW')
SELECT '' AS to_char_10, to_char(d1, 'YYYY WW IYYY IYY IY I IW')
FROM TIMESTAMPTZ_TBL;
to_char_10 | to_char
to_char_10 | to_char
------------+--------------------------
|
|

View File

@ -151,6 +151,8 @@ SELECT '' AS "49", d1 FROM TIMESTAMP_TBL
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
FROM TIMESTAMP_TBL

View File

@ -145,6 +145,8 @@ SELECT '' AS "49", d1 FROM TIMESTAMPTZ_TBL
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
FROM TIMESTAMPTZ_TBL