From 8f93bd8512466c9b6c4dbc1e5efd0f72b8e2be9a Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Wed, 8 Feb 2017 18:04:59 -0500 Subject: [PATCH] Fix roundoff problems in float8_timestamptz() and make_interval(). MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit When converting a float value to integer microseconds, we should be careful to round the value to the nearest integer, typically with rint(); simply assigning to an int64 variable will truncate, causing apparently off-by-one values in cases that should work. Most places in the datetime code got this right, but not these two. float8_timestamptz() is new as of commit e511d878f (9.6). Previous versions effectively depended on interval_mul() to do roundoff correctly, which it does, so this fixes an accuracy regression in 9.6. The problem in make_interval() dates to its introduction in 9.4. Aside from being careful to round not truncate, let's incorporate the hours and minutes inputs into the result with exact integer arithmetic, rather than risk introducing roundoff error where there need not have been any. float8_timestamptz() problem reported by Erik Nordström, though this is not his proposed patch. make_interval() problem found by me. Discussion: https://postgr.es/m/CAHuQZDS76jTYk3LydPbKpNfw9KbACmD=49dC4BrzHcfPv6yA1A@mail.gmail.com --- src/backend/utils/adt/timestamp.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index f2784da360..81b76314de 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -788,7 +788,7 @@ float8_timestamptz(PG_FUNCTION_ARGS) seconds -= ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY); #ifdef HAVE_INT64_TIMESTAMP - result = seconds * USECS_PER_SEC; + result = rint(seconds * USECS_PER_SEC); #else result = seconds; #endif @@ -1623,12 +1623,14 @@ make_interval(PG_FUNCTION_ARGS) result->month = years * MONTHS_PER_YEAR + months; result->day = weeks * 7 + days; - secs += hours * (double) SECS_PER_HOUR + mins * (double) SECS_PER_MINUTE; - #ifdef HAVE_INT64_TIMESTAMP - result->time = (int64) (secs * USECS_PER_SEC); + result->time = hours * ((int64) SECS_PER_HOUR * USECS_PER_SEC) + + mins * ((int64) SECS_PER_MINUTE * USECS_PER_SEC) + + (int64) rint(secs * USECS_PER_SEC); #else - result->time = secs; + result->time = hours * (double) SECS_PER_HOUR + + mins * (double) SECS_PER_MINUTE + + secs; #endif PG_RETURN_INTERVAL_P(result); -- 2.40.0