SCM

[#1010746] NpgsqlDate.AddMonths(m): IndexOutOfRangeException, if new date in following year

View Trackers | Bugs | Download .csv | Monitor

Date:
2009-12-31 10:19
Priority:
3
State:
Open
Submitted by:
Sibylle Koczian (skoczian)
Assigned to:
Nobody (None)
Npgsql Version:
2.0.7
Category:
Group:
Resolution:
None
Summary:
NpgsqlDate.AddMonths(m): IndexOutOfRangeException, if new date in following year

Detailed description
If the expected result of NpgsqlDate.AddMonths(int months) lies in the following year, an IndexOutOfRangeException is thrown.

Example attached. Result, if I run it from the command line:

...>pg_interval
1: 01.04.2009 00:00:00
1: 2009-04-01
2: 01.07.2009 00:00:00
2: 2009-07-01
3: 01.10.2009 00:00:00
3: 2009-10-01
4: 01.01.2010 00:00:00

Unbehandelte Ausnahme: System.IndexOutOfRangeException: Der Index war au├čerhalb
des Arraybereichs.
bei NpgsqlTypes.NpgsqlDate.AddMonths(Int32 months)
bei pg_interval.Program.NpgsqlMonthTest() in D:\home\Sibylle\Dokumente\Progra
mme\Cis\pg_interval\pg_interval\Program.cs:Zeile 69.
bei pg_interval.Program.Main(String[] args) in D:\home\Sibylle\Dokumente\Prog
ramme\Cis\pg_interval\pg_interval\Program.cs:Zeile 91.

Using Npgsql 2.0.7 with Visual C# 2008 Express, Windows 7 (64 bit). The same with Npgsql 2.0.8.

Followup

Message
Date: 2010-11-25 09:43
Sender: Gildas

It works for me in 2.0.11, i've closed the related Patch entry
Date: 2010-01-08 02:33
Sender: Francisco Figueiredo jr.


Patch applied. Please, give it a try and let me know if everything is ok.
Date: 2010-01-07 23:19
Sender: Gildas

The patch is available, it works with both negative an positive values, and tests units case are written for testing it.

Moreover the function is a little faster than before, due to a change in method of calculation (which gave me a headache searching for the best way to do it...)

See http://pgfoundry.org/tracker/index.php?func=detail&aid=1010747&group_id=1000140&atid=592
Date: 2010-01-07 21:20
Sender: Josh Cooley

It doesn't have to be proper patch format if you want to just attach the function. We could use some nunit based unit tests to go along with the fix if anyone has time.
Date: 2010-01-07 20:14
Sender: Sibylle Koczian

Your new patch works correctly, if the number of months to
add isn't negative - but it may be, and the new date may
fall in an earlier year. In that case we're looking into the
list of month lengths with a negative index and get a
System.IndexOutOfRangeException again. Moreover the new year
is one too late. All this is a consequence of the way
integer division and the modulo operator are implemented in C#.

I haven't got CVS, so I can't attach a proper patch, it's
just a text file with a new version of the function.
Date: 2010-01-06 14:30
Sender: Gildas

You're right !! I don't see the mistake... Shame on me :)

I've posted the corrected patch, in the patch section http://pgfoundry.org/tracker/index.php?func=detail&aid=1010747&group_id=1000140&atid=592
Date: 2010-01-06 12:41
Sender: Sibylle Koczian

That doesn't work correctly: the year only changes if at
least 12 months are added at once. If you look at Nr. 3, 7,
11 of your example output you see the difference. And it's
evident from the source:
int newYear = Year + months / 12;
If months < 12 then months / 12 == 0 (this is integer division).
Perhaps I can get it myself, now you've shown me the right
place.
Date: 2010-01-05 08:20
Sender: Gildas

One more thing...

You can think that results are not rights because it begins in July instead of April, that's happen because for test I have modified your "Npgsql_Bug_example.cs" source to begin on april instead of january...
Date: 2010-01-04 23:26
Sender: Gildas

This bug is due to the wrong management of the years, when reaching another year, we're searching the number of days in the 13th month instead of the 1st of the next year...

Here is what i have after correcting it.

1: 01/07/2009 00:00:00
1: 2009-07-01
2: 01/10/2009 00:00:00
2: 2009-10-01
3: 01/01/2010 00:00:00
3: 2009-01-01
4: 01/04/2010 00:00:00
4: 2010-04-01
5: 01/07/2010 00:00:00
5: 2010-07-01
6: 01/10/2010 00:00:00
6: 2010-10-01
7: 01/01/2011 00:00:00
7: 2010-01-01
8: 01/04/2011 00:00:00
8: 2011-04-01
9: 01/07/2011 00:00:00
9: 2011-07-01
10: 01/10/2011 00:00:00
10: 2011-10-01
11: 01/01/2012 00:00:00
11: 2011-01-01
12: 01/04/2012 00:00:00
12: 2012-04-01
Weiter mit jeder Taste ...


I have put a patch (#1010747) here -> http://pgfoundry.org/tracker/index.php?func=detail&aid=1010747&group_id=1000140&atid=592

let me know if it works for you. :)

Attached Files:

Attachments:
Npgsql_Bug_example.cs
DateDatatypes.txt

Changes:

Field Old Value Date By
File Added448: DateDatatypes.txt2010-01-07 20:14skoczian
File Added445: Npgsql_Bug_example.cs2009-12-31 10:19skoczian
Powered By FusionForge