Re: possibly a bug?

Lists: pgsql-novice
From: Ewald Geschwinde <webmaster(at)geschwinde(dot)net>
To: pgsql-novice(at)postgresql(dot)org
Subject: possibly a bug?
Date: 2002-01-30 23:05:31
Message-ID: 3C587C3B.6070803@geschwinde.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

beta=# select version();
version
---------------------------------------------------------------
PostgreSQL 7.1.3 on i686-pc-linux-gnu, compiled by GCC 2.95.3
(1 row)

Is this a bug or Am i Wrong?
Or can be done this with another sql statement
I want to know only the next day of a date

beta=# SELECT ('2001-10-26'::date + '1 day'::interval)::date;
?column?
------------
2001-10-27
(1 row)

beta=# SELECT ('2001-10-27'::date + '1 day'::interval)::date;
?column?
------------
2001-10-28
(1 row)

beta=# SELECT ('2001-10-28'::date + '1 day'::interval)::date;
?column?
------------
2001-10-28
(1 row)

Ewald Geschwinde


From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Ewald Geschwinde <webmaster(at)geschwinde(dot)net>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: possibly a bug?
Date: 2002-01-30 23:27:46
Message-ID: 27628.1012433266@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

Ewald Geschwinde <webmaster(at)geschwinde(dot)net> writes:
> beta=# SELECT ('2001-10-28'::date + '1 day'::interval)::date;
> ?column?
> ------------
> 2001-10-28
> (1 row)

Try using date arithmetic, rather than timestamp arithmetic:

regression=# SELECT '2001-10-28'::date + 1;
?column?
------------
2001-10-29
(1 row)

The problem with the other is that type interval considers '1 day'
to mean '24 hours', which is not what you want. Since 10/28 is
a DST transition day (at least here in the USA), there's a difference.
What you're really getting is a timestamp addition:

regression=# SELECT ('2001-10-28'::date + '1 day'::interval);
?column?
------------------------
2001-10-28 23:00:00-05
(1 row)

which doesn't produce the desired result when you coerce the timestamp
back to date.

However, adding a plain integer to a date will do what you want.

regards, tom lane


From: "Josh Berkus" <josh(at)agliodbs(dot)com>
To: Ewald Geschwinde <webmaster(at)geschwinde(dot)net>, pgsql-novice(at)postgresql(dot)org
Subject: Re: possibly a bug?
Date: 2002-01-30 23:29:47
Message-ID: web-680587@davinci.ethosmedia.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Lists: pgsql-novice

Ewald,

> Is this a bug or Am i Wrong?

It's not a bug, it's a feature :-)

Your problem is that you're convertng back and forth between DATE
(which does not track hours) and TIMESTAMP + INTERVAL, which does
track hours. Therefore you arrive at this problem:

> beta=# SELECT ('2001-10-28'::date + '1 day'::interval)::date;
> ?column?
> ------------
> 2001-10-28
> (1 row)

Ah, but you're not seeing the whole story:

beta=# SELECT ('2001-10-28'::date + '1 day'::interval)::TIMESTAMP
?column?
------------
2001-10-28 23:00:00
(1 row)

Aha! And, if we look at a calendar, we see that 10.28.2001 was the end
of daylight savings time in 2001!

For your purposes, it would be better not to muddy the waters by
tinkering with the vagaries of TIMESTAMP. Thus, you should:

SELECT ('2001-10-28'::DATE + 1);
?column?
------------
2001-10-29
(1 row)

FOr more info, see my DATE/TIME FAQ on http://techdocs.postgtresql.org/

-Josh

______AGLIO DATABASE SOLUTIONS___________________________
Josh Berkus
Complete information technology josh(at)agliodbs(dot)com
and data management solutions (415) 565-7293
for law firms, small businesses fax 621-2533
and non-profit organizations. San Francisco