Re: Possible bug in PGInterval class

From: Ľubomír Varga <luvar(at)plaintext(dot)sk>
To: Kris Jurka <books(at)ejurka(dot)com>
Cc: pgsql-jdbc(at)postgresql(dot)org
Subject: Re: Possible bug in PGInterval class
Date: 2009-09-02 19:01:43
Message-ID: 200909022101.43692.luvar@plaintext.sk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-jdbc

On Wednesday 02 September 2009 02:01:11 Kris Jurka wrote:
>...
> > Second, critical for me, is to not using modulo in addition. So if you
> > add 55 minutes to 55 minutes, you get 110 minutes instead of 1 hour and
> > 50 minutes.
>
> For minutes and hours, it would be OK, but when rolling hours into days or
> days into months, context is needed to know if you are near a daylight
> saving time change or how many days are in the month. For this reason we
> don't manipulate the values and leave them as is.
>

What about to add some function which will "normalize" interval and make
minutes lees than 60, hours lees than 24 and seconds lees than 60? I do it
like this:

PGInterval duration = ...;
int add = 0;
if(duration.getSeconds() > 60) {
add = (int)Math.floor(duration.getSeconds() / 60);
duration.setSeconds(duration.getSeconds() - (add * 60));
duration.setMinutes(duration.getMinutes() + add);
}
if(duration.getMinutes() > 60) {
add = duration.getMinutes() / 60;
duration.setMinutes(duration.getMinutes() - (add * 60));
duration.setHours(duration.getHours() + add);
}

if(duration.getHours() > 24) {
add = duration.getHours() / 24;
duration.setHours(duration.getHours() - (add * 24));
duration.setDays(duration.getDays() + add);
}

Just an suggestion for future generations of developers which will use
PGInterval for postprocessing selected data like me...

PS: Thanks for explanation, I fully accept that reason.

--
Odborník na všetko je zlý odborník. Ja sa snažím byť výnimkou potvrdzujúcou
pravidlo.

In response to

Browse pgsql-jdbc by date

  From Date Subject
Next Message Maciek Sakrejda 2009-09-02 19:52:33 Re: PGStream synchronization
Previous Message Kris Jurka 2009-09-02 16:30:03 Re: PATCH: SET ROLE as connection parameter