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

Started by Mark Douglasover 16 years ago5 messagesbugs
Jump to latest
#1Mark Douglas
mark@steelhousemedia.com

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));

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Mark Douglas (#1)
Re: BUG #5031: DATE_TRUNC returns the wrong value when specifying MONTH

"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

#3Mark Douglas
mark@steelhousemedia.com
In reply to: Tom Lane (#2)
Re: BUG #5031: DATE_TRUNC returns the wrong value when specifying MONTH

I have my timezone set to GMT so there really shouldn't be any time zone adjustments.

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=# 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

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: Mark Douglas (#3)
Re: BUG #5031: DATE_TRUNC returns the wrong value when specifying MONTH

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

#5Mark Douglas
mark@steelhousemedia.com
In reply to: Tom Lane (#4)
Re: BUG #5031: DATE_TRUNC returns the wrong value when specifying MONTH

You're correct. When I run this from psql it returns the correct result. When I run it from DBVisualizer, which I normally use, it adjust the result to my local time zone. Thanks for looking into it. Sorry about bugging you with 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=# 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