doc/src/sgml/func.sgml | 26 ++++++- src/backend/utils/adt/timestamp.c | 123 ++++++++++++++++++++++++++++++++ src/include/catalog/pg_proc.dat | 3 + src/test/regress/expected/timestamp.out | 27 +++++++ src/test/regress/sql/timestamp.sql | 16 +++++ 5 files changed, 194 insertions(+), 1 deletion(-) diff --git a/doc/src/sgml/func.sgml b/doc/src/sgml/func.sgml index ceda48e0fc..3863c222a2 100644 --- a/doc/src/sgml/func.sgml +++ b/doc/src/sgml/func.sgml @@ -6949,6 +6949,15 @@ SELECT regexp_match('abc01234xyz', '(?:(.*?)(\d+)(.*)){1,1}'); 2 days 03:00:00 + + date_trunc_interval(interval, timestamp) + timestamp + Truncate to specified precision; see + + date_trunc_interval('15 minutes', timestamp '2001-02-16 20:38:40') + 2001-02-16 20:30:00 + + @@ -7818,7 +7827,7 @@ SELECT date_part('hour', INTERVAL '4 hours 3 minutes'); - <function>date_trunc</function> + <function>date_trunc</function>, <function>date_trunc_interval</function> date_trunc @@ -7902,6 +7911,21 @@ SELECT date_trunc('hour', INTERVAL '3 days 02:47:33'); Result: 3 days 02:00:00 + + + The function date_trunc_interval is + similar to the date_trunc, except that it + truncates to an arbitrary interval. + + + + Example: + +SELECT date_trunc_interval('5 minutes', TIMESTAMP '2001-02-16 20:38:40'); +Result: 2001-02-16 20:35:00 + + + diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index 0b6c9d5ea8..f2dd4f1995 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -3804,6 +3804,129 @@ timestamptz_age(PG_FUNCTION_ARGS) *---------------------------------------------------------*/ +/* timestamp_trunc_interval() + * Truncate timestamp to specified interval. + */ +Datum +timestamp_trunc_interval(PG_FUNCTION_ARGS) +{ + Interval *interval = PG_GETARG_INTERVAL_P(0); + Timestamp timestamp = PG_GETARG_TIMESTAMP(1); + Timestamp result; + fsec_t ifsec, + tfsec; + int unit; + + struct pg_tm it; + struct pg_tm tt; + + if (TIMESTAMP_NOT_FINITE(timestamp)) + PG_RETURN_TIMESTAMP(timestamp); + + if (interval2tm(*interval, &it, &ifsec) != 0) + elog(ERROR, "could not convert interval to tm"); + + if (timestamp2tm(timestamp, NULL, &tt, &tfsec, NULL, NULL) != 0) + ereport(ERROR, + (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), + errmsg("timestamp out of range"))); + + if (it.tm_year != 0) + { + tt.tm_year = it.tm_year * (tt.tm_year / it.tm_year); + unit = DTK_YEAR; + } + else if (it.tm_mon != 0) + { + tt.tm_mon = it.tm_mon * (tt.tm_mon / it.tm_mon); + unit = DTK_MONTH; + } + else if (it.tm_mday != 0) + { + tt.tm_mday = it.tm_mday * (tt.tm_mday / it.tm_mday); + unit = DTK_DAY; + } + else if (it.tm_hour != 0) + { + tt.tm_hour = it.tm_hour * (tt.tm_hour / it.tm_hour); + unit = DTK_HOUR; + } + else if (it.tm_min != 0) + { + tt.tm_min = it.tm_min * (tt.tm_min / it.tm_min); + unit = DTK_MINUTE; + } + else if (it.tm_sec != 0) + { + tt.tm_sec = it.tm_sec * (tt.tm_sec / it.tm_sec); + unit = DTK_SECOND; + } + else if (ifsec != 0) + { + tfsec = ifsec * (tfsec / ifsec); + + if (ifsec >= 1000) + unit = DTK_MILLISEC; + else + unit = DTK_MICROSEC; + } + else + ereport(ERROR, + (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), + errmsg("interval not initialized"))); + + /* + * Justify all lower timestamp units and throw an error if any + * of the lower interval unitsĀ are non-zero. + */ + switch (unit) + { + case DTK_YEAR: + tt.tm_mon = 1; + if (it.tm_mon != 0) + goto error; + case DTK_MONTH: + tt.tm_mday = 1; + if (it.tm_mday != 0) + goto error; + case DTK_DAY: + tt.tm_hour = 0; + if (it.tm_hour != 0) + goto error; + case DTK_HOUR: + tt.tm_min = 0; + if (it.tm_min != 0) + goto error; + case DTK_MINUTE: + tt.tm_sec = 0; + if (it.tm_sec != 0) + goto error; + case DTK_SECOND: + tfsec = 0; + case DTK_MILLISEC: + case DTK_MICROSEC: + break; + default: + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("interval unit not supported"))); + + } + + if (tm2timestamp(&tt, tfsec, NULL, &result) != 0) + ereport(ERROR, + (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), + errmsg("timestamp out of range"))); + + PG_RETURN_TIMESTAMP(result); + +error: + ereport(ERROR, + // WIP is there a better errcode? + (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), + errmsg("only one interval unit allowed for truncation"))); +} + /* timestamp_trunc() * Truncate timestamp to specified units. */ diff --git a/src/include/catalog/pg_proc.dat b/src/include/catalog/pg_proc.dat index 0345118cdb..ddb1b84b52 100644 --- a/src/include/catalog/pg_proc.dat +++ b/src/include/catalog/pg_proc.dat @@ -5660,6 +5660,9 @@ { oid => '2020', descr => 'truncate timestamp to specified units', proname => 'date_trunc', prorettype => 'timestamp', proargtypes => 'text timestamp', prosrc => 'timestamp_trunc' }, +{ oid => '8989', descr => 'truncate timestamp to specified interval', + proname => 'date_trunc_interval', prorettype => 'timestamp', + proargtypes => 'interval timestamp', prosrc => 'timestamp_trunc_interval' }, { oid => '2021', descr => 'extract field from timestamp', proname => 'date_part', prorettype => 'float8', proargtypes => 'text timestamp', prosrc => 'timestamp_part' }, diff --git a/src/test/regress/expected/timestamp.out b/src/test/regress/expected/timestamp.out index 5f97505a30..b887468a73 100644 --- a/src/test/regress/expected/timestamp.out +++ b/src/test/regress/expected/timestamp.out @@ -545,6 +545,33 @@ SELECT '' AS date_trunc_week, date_trunc( 'week', timestamp '2004-02-29 15:44:17 | Mon Feb 23 00:00:00 2004 (1 row) +SELECT + interval, + date_trunc_interval(interval::interval, ts) +FROM ( + VALUES + ('5 years'), + ('1 month'), + ('7 days'), + ('2 hours'), + ('15 minutes'), + ('10 seconds'), + ('100 millisecond'), + ('250 microseconds') +) intervals (interval), +(SELECT TIMESTAMP '2004-02-29 15:44:17.71393') ts (ts); + interval | date_trunc_interval +------------------+-------------------------------- + 5 years | Sat Jan 01 00:00:00 2000 + 1 month | Sun Feb 01 00:00:00 2004 + 7 days | Sat Feb 28 00:00:00 2004 + 2 hours | Sun Feb 29 14:00:00 2004 + 15 minutes | Sun Feb 29 15:30:00 2004 + 10 seconds | Sun Feb 29 15:44:10 2004 + 100 millisecond | Sun Feb 29 15:44:17.7 2004 + 250 microseconds | Sun Feb 29 15:44:17.71375 2004 +(8 rows) + -- Test casting within a BETWEEN qualifier SELECT '' AS "54", d1 - timestamp without time zone '1997-01-02' AS diff FROM TIMESTAMP_TBL diff --git a/src/test/regress/sql/timestamp.sql b/src/test/regress/sql/timestamp.sql index 7b58c3cfa5..ffb22bc3e0 100644 --- a/src/test/regress/sql/timestamp.sql +++ b/src/test/regress/sql/timestamp.sql @@ -166,6 +166,22 @@ SELECT '' AS "54", d1 - timestamp without time zone '1997-01-02' AS diff SELECT '' AS date_trunc_week, date_trunc( 'week', timestamp '2004-02-29 15:44:17.71393' ) AS week_trunc; +SELECT + interval, + date_trunc_interval(interval::interval, ts) +FROM ( + VALUES + ('5 years'), + ('1 month'), + ('7 days'), + ('2 hours'), + ('15 minutes'), + ('10 seconds'), + ('100 millisecond'), + ('250 microseconds') +) intervals (interval), +(SELECT TIMESTAMP '2004-02-29 15:44:17.71393') ts (ts); + -- Test casting within a BETWEEN qualifier SELECT '' AS "54", d1 - timestamp without time zone '1997-01-02' AS diff FROM TIMESTAMP_TBL