diff options
author | Peter Eisentraut <peter@eisentraut.org> | 2021-04-10 19:33:46 +0200 |
---|---|---|
committer | Peter Eisentraut <peter@eisentraut.org> | 2021-04-10 19:33:46 +0200 |
commit | 496e58bb0e5e939e6ed5839c92b05e3ab11b54bb (patch) | |
tree | 7a58b873caea3816bac0579bac291a6629678ab8 /src/backend/utils/adt/timestamp.c | |
parent | 99964c4ade468c35a3f6e248a2380a1ff67d9cd3 (diff) | |
download | postgresql-496e58bb0e5e939e6ed5839c92b05e3ab11b54bb.tar.gz postgresql-496e58bb0e5e939e6ed5839c92b05e3ab11b54bb.zip |
Improve behavior of date_bin with origin in the future
Currently, when the origin is after the input, the result is the
timestamp at the end of the bin, rather than the beginning as
expected. This puts the result consistently at the beginning of the
bin.
Author: John Naylor <john.naylor@enterprisedb.com>
Discussion: https://www.postgresql.org/message-id/CAFBsxsGjLDxQofRfH+d4KSAXxPf3MMevUG7s6EDfdBOvHLDLjw@mail.gmail.com
Diffstat (limited to 'src/backend/utils/adt/timestamp.c')
-rw-r--r-- | src/backend/utils/adt/timestamp.c | 14 |
1 files changed, 14 insertions, 0 deletions
diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index b2bdbcab576..280ee7f92ba 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -3846,6 +3846,13 @@ timestamp_bin(PG_FUNCTION_ARGS) tm_diff = timestamp - origin; tm_delta = tm_diff - tm_diff % stride_usecs; + /* + * Make sure the returned timestamp is at the start of the bin, + * even if the origin is in the future. + */ + if (origin > timestamp && stride_usecs > 1) + tm_delta -= stride_usecs; + result = origin + tm_delta; PG_RETURN_TIMESTAMP(result); @@ -4017,6 +4024,13 @@ timestamptz_bin(PG_FUNCTION_ARGS) tm_diff = timestamp - origin; tm_delta = tm_diff - tm_diff % stride_usecs; + /* + * Make sure the returned timestamp is at the start of the bin, + * even if the origin is in the future. + */ + if (origin > timestamp && stride_usecs > 1) + tm_delta -= stride_usecs; + result = origin + tm_delta; PG_RETURN_TIMESTAMPTZ(result); |