From: | Alban Hertroys <haramrae(at)gmail(dot)com> |
---|---|
To: | "Harvey, Allan AC" <HarveyA(at)OneSteel(dot)com> |
Cc: | <pgsql-general(at)postgresql(dot)org> |
Subject: | Re: Should casting to integer produce same result as trunc() |
Date: | 2011-10-11 07:05:29 |
Message-ID: | 17A2DEC6-37D4-430C-882C-11800A112098@gmail.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On 11 Oct 2011, at 2:55, Harvey, Allan AC wrote:
> Hi all,
>
> Had to squash timestamps to the nearest 5 minutes and things went wrong.
>
> My simple understanding of trunc() and casting to an integer says that
> there is a bug here.
I think you may be right there, something about the rounding in the cast seems wrong.
> -- should be different but are not.
> select (((extract( epoch from '2011-08-22 08:37:30'::timestamp ) + 10 *
> 3600) / 300 )::integer), (((extract( epoch from '2011-08-22
> 08:42:30'::timestamp
> ) + 10 * 3600) / 300 )::integer);
> int4 | int4
> ---------+---------
> 4380008 | 4380008
> (1 row)
>
Without the cast, that gives (I'm in a different TZ apparently):
select (extract( epoch from '2011-08-22 08:37:30'::timestamp ) + 10 *
3600) / 300, (extract( epoch from '2011-08-22 08:42:30'::timestamp ) + 10 * 3600) / 300;
?column? | ?column?
-----------+-----------
4380103.5 | 4380104.5
(1 row)
Which the type-cast should round to 4380103 and 4380104 respectively.
It doesn't:
select ((extract( epoch from '2011-08-22 08:37:30'::timestamp ) + 10 *
3600) / 300)::integer, floor((extract( epoch from '2011-08-22 08:42:30'::timestamp ) + 10 * 3600) / 300);
int4 | floor
---------+---------
4380104 | 4380104
(1 row)
Floor() works fine though:
select floor((extract( epoch from '2011-08-22 08:37:30'::timestamp ) + 10 *
3600) / 300), floor((extract( epoch from '2011-08-22 08:42:30'::timestamp ) + 10 * 3600) / 300);
floor | floor
---------+---------
4380103 | 4380104
(1 row)
Alban Hertroys
--
If you can't see the forest for the trees,
cut the trees and you'll see there is no forest.
From | Date | Subject | |
---|---|---|---|
Next Message | Jeff Adams | 2011-10-11 11:43:54 | Re: SQL Help - Finding Next Lowest Value of Current Row Value |
Previous Message | Raghavendra | 2011-10-11 05:39:39 | Re: Help on PostgreSQL |