Unix Technical Forum

BUG #2443: 1 hour error at date calculation

This is a discussion on BUG #2443: 1 hour error at date calculation within the pgsql Bugs forums, part of the PostgreSQL category; --> The following bug has been logged online: Bug reference: 2443 Logged by: nikolaus klepp Email address: dr.klepp@gmx.at PostgreSQL version: ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > pgsql Bugs

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-10-2008, 11:00 AM
nikolaus klepp
 
Posts: n/a
Default BUG #2443: 1 hour error at date calculation


The following bug has been logged online:

Bug reference: 2443
Logged by: nikolaus klepp
Email address: dr.klepp@gmx.at
PostgreSQL version: 8.0.3, 7.4.8
Operating system: suse linux 10.0
Description: 1 hour error at date calculation
Details:

i want to calculate the number of days in a month. when I do it this way:

select date_trunc('month', date_trunc('month', date('2006-3-1'))+interval
'1month')-date_trunc('month', date('2006-3-1'));

the result is: 30 days 23:00:00
that is obviously wrong.

---------------------------(end of broadcast)---------------------------
TIP 5: don't forget to increase your free space map settings

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-10-2008, 11:00 AM
Tom Lane
 
Posts: n/a
Default Re: BUG #2443: 1 hour error at date calculation

"nikolaus klepp" <dr.klepp@gmx.at> writes:
> i want to calculate the number of days in a month. when I do it this way:


> select date_trunc('month', date_trunc('month', date('2006-3-1'))+interval
> '1month')-date_trunc('month', date('2006-3-1'));


> the result is: 30 days 23:00:00
> that is obviously wrong.


No, it's not wrong (I assume you live in a timezone where DST switches
during March). Subtraction of two timestamps gives the number of hours
between them, and that's what you've got.

If you want the number of days without regard to minor details like DST
changes, cast the two values back to plain date before subtracting.

For me, DST changes in April, and I get:

regression=# select date_trunc('month', date_trunc('month', date('2006-4-1'))+i
nterval '1month')-date_trunc('month', date('2006-4-1'));
?column?
------------------
29 days 23:00:00
(1 row)

regression=# select date_trunc('month', date_trunc('month', date('2006-4-1'))+i
nterval '1month')::date - date_trunc('month', date('2006-4-1'))::date;
?column?
----------
30
(1 row)

regression=#

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: In versions below 8.0, the planner will ignore your desire to
choose an index scan if your joining column's datatypes do not
match

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 03:56 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com