Bug 227666 - wrong age calculation for birth year 1961 in 2010, 2011 and 2012
Summary: wrong age calculation for birth year 1961 in 2010, 2011 and 2012
Status: RESOLVED FIXED
Alias: None
Product: kresources
Classification: Miscellaneous
Component: birthdays (show other bugs)
Version: unspecified
Platform: unspecified Linux
: NOR normal
Target Milestone: ---
Assignee: kdepim bugs
URL:
Keywords:
: 228523 228924 232662 235254 236704 (view as bug list)
Depends on:
Blocks:
 
Reported: 2010-02-19 13:57 UTC by Panard
Modified: 2010-05-23 15:51 UTC (History)
16 users (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Panard 2010-02-19 13:57:09 UTC
Version:           unknown (using 4.4.00 (KDE 4.4.0), Gentoo)
Compiler:          x86_64-pc-linux-gnu-gcc
OS:                Linux (x86_64) release 2.6.32-gentoo-r5

What a funny bug!

My mom is born on Feb 1961, the 19th. However, using the birthday kresource, korganizer says she will have 48 this year!
If I look in 2009, the age computation is correct (48) and in 2013 too (52) but, for the years 2010, 2011 and 2012, there is always one year less.
If I change the birth month to April, the bug is still here.
If I change the birth year (I tried 1902 and 1988), the bug disappears.

My mom will be very happy not becoming older this year, but, well, I would be happy that this bug apply to every birth year to have this gift too ;-)
Comment 1 Panard 2010-02-19 14:10:42 UTC
Oh, this bug also occurs for me! I'm born on April 1985, the 11th, and korganizer says I will still have 24 this year.
The calculation remains wrong if I set my birth year to 1984.
The calculation is correct if I set my birth year to 1983 or 1986, 1987.
Comment 2 John Layt 2010-02-22 19:57:55 UTC
The problem lies in the following code:

qint64 KOHelper::yearDiff( const QDate &start, const QDate &end )
{
  return static_cast<qint64>( start.daysTo( end ) / 365.25 );
}

In case 1 above this gives

( 19 Feb 2010 - 19 Feb 1961 ) / 365.25
= ( 2455246 - 2437349 ) / 365.25
= 17897 / 365.25
= 48.9993
= 48

When obviously it should be 49.  you could add a few more days on to the end date to fix the rounding, or do it properly:

if ( end.month() > start.month() ||
    ( end.month() == start.month() &&
      end.day() > start.day() ) {
    end.year() - start.year();
} else {
    end.year() - start.year() - 1;
}

I should probably provide KCalendarSystem methods to do this.

This assumes a standard rule for date maths that if the end month has fewer days than the start month you move to the last day of the same month.  So a birthday on the 29th Feb will be moved to 28th Feb, which is the law in the UK.

I think KOrganizer follows the day of year rule, i.e. always falls on day 60 so would be 1st March in non-leap years, which would be wrong in the UK context at least.

Don't even get me started on rules for moving Hebrew birthdays, those rules blow my mind :-)

Also it doesn't localise the calendar system either, but that seems widespread throughout KOrganizer, there's a lot of inconsistencies as a result if the user has a non-Gregorian system calendar selected. Perhaps KO should always force to run in Gregorian for now, especially as iCalendar only supports Gregorian?
Comment 3 John Layt 2010-02-22 20:40:48 UTC
Whoops, small typo, should be "end.day() >= start.day()".

It also assumes start < end, it needs tweaking for start >= end (i.e. same year or how many years ago something happened).
Comment 4 John Layt 2010-03-01 20:18:45 UTC
*** Bug 228523 has been marked as a duplicate of this bug. ***
Comment 5 John Layt 2010-03-01 20:19:10 UTC
*** Bug 228924 has been marked as a duplicate of this bug. ***
Comment 6 John Layt 2010-03-01 20:20:04 UTC
I should note my alogrithm above is slightly wrong, I'll post a correct version to Reviewboard soon.
Comment 7 John Layt 2010-04-02 23:30:57 UTC
*** Bug 232662 has been marked as a duplicate of this bug. ***
Comment 8 Andreas Kahnt 2010-04-11 11:15:01 UTC
I've watched the same with a birthday of 13-Apr-1940 => 69years!?? to 13-Apr-2010.
I would suggest to round the result of KOHelper::yearDiff(). And I would suggest to display the original date in the preview window of kontact/calendar.
My version: kde/kontact/calendar-4.4.2 (kubuntu)
Comment 9 Stéphane Pontier 2010-04-20 11:50:59 UTC
I also have this problem with a friend born 1980-05-02 and displaying in korganizer going to have 29 years old instead of 30
onother one born on 1985-02-01 displaying having 24 instead of 25
while another born on 1976-02-01 is rightly displayed as having 34 years old
Comment 10 Alex Dănilă 2010-04-25 16:01:55 UTC
*** This bug has been confirmed by popular vote. ***
Comment 11 John Layt 2010-04-27 17:39:01 UTC
*** Bug 235254 has been marked as a duplicate of this bug. ***
Comment 12 Luis Silva 2010-04-27 18:29:27 UTC
(In reply to comment #2)
> The problem lies in the following code:
> 
> qint64 KOHelper::yearDiff( const QDate &start, const QDate &end )
> {
>   return static_cast<qint64>( start.daysTo( end ) / 365.25 );
> }

The correct code should actually be:
qint64 KOHelper::yearDiff( const QDate &start, const QDate &end )
{
  return static_cast<qint64>( (start.daysTo( end ) + 1) / 365.25 );
}

This is a common mistake when doing time/date related maths.
Comment 13 John Layt 2010-05-08 21:17:49 UTC
*** Bug 236704 has been marked as a duplicate of this bug. ***
Comment 14 vuxi 2010-05-08 23:40:34 UTC
is there a chance that this bug is removed soon ?
Comment 15 Ralf Jung 2010-05-12 14:57:19 UTC
My brother, born in 1990, also triggers this issue (KDE 4.4.2). Looks rather silly, to be honest ;-)
Comment 16 Bernd Paysan 2010-05-15 00:49:55 UTC
Oh no, what a mess!  The calendar year's length is 365.2425 since 1582.  The rounding method is chosen wrongly (I'd suggest round to nearest by adding 0.5), affecting everybody in my list of birthdays.  Worst of all, this is even using QDate, which has a year function.  Just use

end.year()-start.year()

for printing out the years for birthday and anniversary, and be done!  No need for helper function for something that trivial, and even then possibly to mess it up.

QDate has its own problems, by (wrongly) stating that there is "no year 0".  Yes, there is, and it's there for 10 years now, it is all very handy for computer-related date calculation.  Read up the ISO 8601 standard about date and time representations, or at lest http://de.wikipedia.org/wiki/ISO_8601.

And please have someone with checking rights to KDE take this bug, it can't be more than a five minutes fix.
Comment 17 Glenn R Williams 2010-05-19 17:11:04 UTC
all the age calculations are wrong by me - Kubuntu 10.04 Lucid Lynx.
Comment 18 Gerry Gavigan 2010-05-22 17:07:30 UTC
It would be helpful. please, to get a progress report.

Are there any remaining tasks that can be handed off to users/bug reporters?

The more qualified among us seem to have provided code suggestions.  Are these inadequate?

Is that all will be revealed in KDE 4.5?

The silence is a bit deafening.
Comment 19 Glenn R Williams 2010-05-22 17:31:51 UTC
This is such a visible bug, and is really harmful to the reputation of kde. Is there a purpose in submitting bug reports if no one seems to read them?
Comment 20 Allen Winter 2010-05-23 15:07:01 UTC
this was fixed a couple days ago and will be included in the next 4.4.x release
Comment 21 Ralf Jung 2010-05-23 15:51:32 UTC
Thank you very much for the good news :)