Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 404756f

Browse files
committed
Fix roundoff problems in float8_timestamptz() and make_interval().
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 e511d87 (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
1 parent ae8a602 commit 404756f

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/backend/utils/adt/timestamp.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -779,7 +779,7 @@ float8_timestamptz(PG_FUNCTION_ARGS)
779779
seconds -= ((POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * SECS_PER_DAY);
780780

781781
#ifdef HAVE_INT64_TIMESTAMP
782-
result = seconds * USECS_PER_SEC;
782+
result = rint(seconds * USECS_PER_SEC);
783783
#else
784784
result = seconds;
785785
#endif
@@ -1614,12 +1614,14 @@ make_interval(PG_FUNCTION_ARGS)
16141614
result->month = years * MONTHS_PER_YEAR + months;
16151615
result->day = weeks * 7 + days;
16161616

1617-
secs += hours * (double) SECS_PER_HOUR + mins * (double) SECS_PER_MINUTE;
1618-
16191617
#ifdef HAVE_INT64_TIMESTAMP
1620-
result->time = (int64) (secs * USECS_PER_SEC);
1618+
result->time = hours * ((int64) SECS_PER_HOUR * USECS_PER_SEC) +
1619+
mins * ((int64) SECS_PER_MINUTE * USECS_PER_SEC) +
1620+
(int64) rint(secs * USECS_PER_SEC);
16211621
#else
1622-
result->time = secs;
1622+
result->time = hours * (double) SECS_PER_HOUR +
1623+
mins * (double) SECS_PER_MINUTE +
1624+
secs;
16231625
#endif
16241626

16251627
PG_RETURN_INTERVAL_P(result);

0 commit comments

Comments
 (0)