Commit 19739718 authored by Bruce Momjian's avatar Bruce Momjian

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
parent 44611f6e
<!-- <!--
$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 PostgreSQL documentation
--> -->
...@@ -5309,6 +5309,7 @@ date_trunc('<replaceable>field</replaceable>', <replaceable>source</replaceable> ...@@ -5309,6 +5309,7 @@ date_trunc('<replaceable>field</replaceable>', <replaceable>source</replaceable>
<member><literal>minute</literal></member> <member><literal>minute</literal></member>
<member><literal>hour</literal></member> <member><literal>hour</literal></member>
<member><literal>day</literal></member> <member><literal>day</literal></member>
<member><literal>week</literal></member>
<member><literal>month</literal></member> <member><literal>month</literal></member>
<member><literal>year</literal></member> <member><literal>year</literal></member>
<member><literal>decade</literal></member> <member><literal>decade</literal></member>
......
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * 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) ...@@ -2564,6 +2564,13 @@ timestamp_trunc(PG_FUNCTION_ARGS)
switch (val) 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: case DTK_MILLENNIUM:
tm->tm_year = (tm->tm_year / 1000) * 1000; tm->tm_year = (tm->tm_year / 1000) * 1000;
case DTK_CENTURY: case DTK_CENTURY:
...@@ -2672,6 +2679,13 @@ timestamptz_trunc(PG_FUNCTION_ARGS) ...@@ -2672,6 +2679,13 @@ timestamptz_trunc(PG_FUNCTION_ARGS)
switch (val) 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: case DTK_MILLENNIUM:
tm->tm_year = (tm->tm_year / 1000) * 1000; tm->tm_year = (tm->tm_year / 1000) * 1000;
case DTK_CENTURY: case DTK_CENTURY:
......
...@@ -499,6 +499,12 @@ SELECT '' AS "54", d1 - timestamp without time zone '1997-01-02' AS diff ...@@ -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 | @ 1460 days 17 hours 32 mins 1 sec
(54 rows) (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 -- Test casting within a BETWEEN qualifier
SELECT '' AS "54", d1 - timestamp without time zone '1997-01-02' AS diff SELECT '' AS "54", d1 - timestamp without time zone '1997-01-02' AS diff
FROM TIMESTAMP_TBL FROM TIMESTAMP_TBL
......
...@@ -494,6 +494,12 @@ SELECT '' AS "54", d1 - timestamp with time zone '1997-01-02' AS diff ...@@ -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 | @ 1460 days 17 hours 32 mins 1 sec
(54 rows) (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 -- Test casting within a BETWEEN qualifier
SELECT '' AS "54", d1 - timestamp with time zone '1997-01-02' AS diff SELECT '' AS "54", d1 - timestamp with time zone '1997-01-02' AS diff
FROM TIMESTAMPTZ_TBL FROM TIMESTAMPTZ_TBL
......
...@@ -151,6 +151,8 @@ SELECT '' AS "49", d1 FROM TIMESTAMP_TBL ...@@ -151,6 +151,8 @@ SELECT '' AS "49", d1 FROM TIMESTAMP_TBL
SELECT '' AS "54", d1 - timestamp without time zone '1997-01-02' AS diff 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'; 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 -- Test casting within a BETWEEN qualifier
SELECT '' AS "54", d1 - timestamp without time zone '1997-01-02' AS diff SELECT '' AS "54", d1 - timestamp without time zone '1997-01-02' AS diff
FROM TIMESTAMP_TBL FROM TIMESTAMP_TBL
......
...@@ -145,6 +145,8 @@ SELECT '' AS "49", d1 FROM TIMESTAMPTZ_TBL ...@@ -145,6 +145,8 @@ SELECT '' AS "49", d1 FROM TIMESTAMPTZ_TBL
SELECT '' AS "54", d1 - timestamp with time zone '1997-01-02' AS diff 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'; 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 -- Test casting within a BETWEEN qualifier
SELECT '' AS "54", d1 - timestamp with time zone '1997-01-02' AS diff SELECT '' AS "54", d1 - timestamp with time zone '1997-01-02' AS diff
FROM TIMESTAMPTZ_TBL FROM TIMESTAMPTZ_TBL
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment