Thread: BUG #5031: DATE_TRUNC returns the wrong value when specifying MONTH

BUG #5031: DATE_TRUNC returns the wrong value when specifying MONTH

From
"Mark Douglas"
Date:
The following bug has been logged online:

Bug reference:      5031
Logged by:          Mark Douglas
Email address:      mark@steelhousemedia.com
PostgreSQL version: 8.4.0
Operating system:   Ubunto Linux
Description:        DATE_TRUNC returns the wrong value when specifying MONTH
Details:

The following use of DATE_TRUNC returns the wrong value. I called the
function on 2009-09-02. It should return '2009-09-01 00:00:00' for the
following usage:

SELECT DATE_TRUNC('MONTH', CURRENT_DATE);

It instead returns '2009-08-31 17:00:00.

Casting CURRENT_DATE to a TIMESTAMP causes it to return the correct value
but that shouldn't be required. Cast example:

SELECT DATE_TRUNC('MONTH', CAST(CURRENT_DATE AS TIMESTAMP));

Re: BUG #5031: DATE_TRUNC returns the wrong value when specifying MONTH

From
Tom Lane
Date:
"Mark Douglas" <mark@steelhousemedia.com> writes:
> The following use of DATE_TRUNC returns the wrong value. I called the
> function on 2009-09-02. It should return '2009-09-01 00:00:00' for the
> following usage:

> SELECT DATE_TRUNC('MONTH', CURRENT_DATE);

> It instead returns '2009-08-31 17:00:00.

Really?  What timezone setting are you using?  I get

postgres=# SELECT DATE_TRUNC('MONTH', CURRENT_DATE);
       date_trunc
------------------------
 2009-09-01 00:00:00-04
(1 row)

with timezone set to 'America/New_York' or equivalent.
This test might also be instructive:

postgres=# select CURRENT_DATE::timestamptz;
      timestamptz
------------------------
 2009-09-03 00:00:00-04
(1 row)


            regards, tom lane

Re: BUG #5031: DATE_TRUNC returns the wrong value when specifying MONTH

From
Tom Lane
Date:
Mark Douglas <mark@steelhousemedia.com> writes:
> I have my timezone set to GMT so there really shouldn't be any time zone adjustments.

Okay ...

postgres=# set timezone = GMT;
SET
postgres=# SELECT DATE_TRUNC('MONTH', CURRENT_DATE);
       date_trunc
------------------------
 2009-09-01 00:00:00+00
(1 row)

I suspect there's something you're not telling us, like you're
using a client-side library that is doing timezone adjustments
behind your back.

            regards, tom lane

Re: BUG #5031: DATE_TRUNC returns the wrong value when specifying MONTH

From
Mark Douglas
Date:
I have my timezone set to GMT so there really shouldn't be any time zone ad=
justments.

Mark


On 9/2/09 10:01 PM, "Tom Lane" <tgl@sss.pgh.pa.us> wrote:

"Mark Douglas" <mark@steelhousemedia.com> writes:
> The following use of DATE_TRUNC returns the wrong value. I called the
> function on 2009-09-02. It should return '2009-09-01 00:00:00' for the
> following usage:

> SELECT DATE_TRUNC('MONTH', CURRENT_DATE);

> It instead returns '2009-08-31 17:00:00.

Really?  What timezone setting are you using?  I get

postgres=3D# SELECT DATE_TRUNC('MONTH', CURRENT_DATE);
       date_trunc
------------------------
 2009-09-01 00:00:00-04
(1 row)

with timezone set to 'America/New_York' or equivalent.
This test might also be instructive:

postgres=3D# select CURRENT_DATE::timestamptz;
      timestamptz
------------------------
 2009-09-03 00:00:00-04
(1 row)


                        regards, tom lane

Re: BUG #5031: DATE_TRUNC returns the wrong value when specifying MONTH

From
Mark Douglas
Date:
You're correct. When I run this from psql it returns the correct result. Wh=
en I run it from DBVisualizer, which I normally use, it adjust the result t=
o my local time zone. Thanks for looking into it. Sorry about bugging you w=
ith that.

Thanks,

Mark


On 9/2/09 10:24 PM, "Tom Lane" <tgl@sss.pgh.pa.us> wrote:

Mark Douglas <mark@steelhousemedia.com> writes:
> I have my timezone set to GMT so there really shouldn't be any time zone =
adjustments.

Okay ...

postgres=3D# set timezone =3D GMT;
SET
postgres=3D# SELECT DATE_TRUNC('MONTH', CURRENT_DATE);
       date_trunc
------------------------
 2009-09-01 00:00:00+00
(1 row)

I suspect there's something you're not telling us, like you're
using a client-side library that is doing timezone adjustments
behind your back.

                        regards, tom lane