Recent

Author Topic: YearsBetween Problem - Wrong answer!  (Read 16611 times)

wp

  • Hero Member
  • *****
  • Posts: 13192
Re: YearsBetween Problem - Wrong answer!
« Reply #30 on: January 15, 2017, 10:50:58 pm »
Time is ignored in the original YearsBetween as well.

Ty it with Date := -697243.0 (10 years before 01-01-01) and 2017-01-01.
It will return 2017, where it whould well be over 2017 (2026, 2027 or 2028: I still get confused about what it should be).
FPC will return 2025, as does Delphi.

Yes it is, and maybe this is by design, but you brouht up the time issue (or am I mistaken?).

In the YearsBetween it is by design because of the Trunc() used. But it can easily be added by taking the difference betwen the time parts of end and start datetimes for the case that the period begins and ends on the same calendar date (same day, same month, but different years): if it is negative then the last year is not complete, and the result must be decremented by 1.

On the other hand I am not really sure if this of any practical relevance.

bylaardt

  • Sr. Member
  • ****
  • Posts: 310
Re: YearsBetween Problem - Wrong answer!
« Reply #31 on: January 15, 2017, 10:54:14 pm »
58 is the correct answer IMO here.

Bart

Why?

Basic arithmetic operations:

years= 2015-1956
years= 59

months=12-12 =0
months=0

Days=1-1
Days=0

wp

  • Hero Member
  • *****
  • Posts: 13192
Re: YearsBetween Problem - Wrong answer!
« Reply #32 on: January 15, 2017, 11:07:26 pm »
58 is the correct answer IMO here.

Bart

Why?

Basic arithmetic operations:

years= 2015-1956
years= 59

months=12-12 =0
months=0

Days=1-1
Days=0
Because there is a time part, the interval is too short by 1 hour. Of course it's a point of discussion whether time should be considered in counting of years.
« Last Edit: January 15, 2017, 11:09:20 pm by wp »

bylaardt

  • Sr. Member
  • ****
  • Posts: 310
Re: YearsBetween Problem - Wrong answer!
« Reply #33 on: January 16, 2017, 12:30:30 am »
Because there is a time part, the interval is too short by 1 hour. Of course it's a point of discussion whether time should be considered in counting of years.
then i suggest rename the function to
Code: Pascal  [Select][+][-]
  1. function SomethingSimimilarYears_ButNotYears_Between(ANow, AThen: TDateTime): Integer;
  2.  
because "YearsBetween" expected results is in years!

wp

  • Hero Member
  • *****
  • Posts: 13192
Re: YearsBetween Problem - Wrong answer!
« Reply #34 on: January 16, 2017, 12:49:41 am »
?

It *is* in years. This applies to  the old YearsBetween as well. It does not count incomplete years. When months or days are missing then a year is not complete. And it is not complete either when hours or minutes, even seconds, are missing. Look at Silvester: Everybody's waiting for midnight. Until the clock does not show midnight the year which started on previous Jan 1 0:00 is not complete. So, there is no full year between Jan 1 2015 0:00 and Dec 31 2015 23:55, and thus a time-sensitive YearsBetween must report zero years in the same way as the current date-only version must report zero years between Jan 1 2015 and Dec 31 2015.

bylaardt

  • Sr. Member
  • ****
  • Posts: 310
Re: YearsBetween Problem - Wrong answer!
« Reply #35 on: January 16, 2017, 01:02:23 am »
Ok. my mistake.

bzman24

  • Jr. Member
  • **
  • Posts: 71
Re: YearsBetween Problem - Wrong answer!
« Reply #36 on: January 16, 2017, 03:15:00 am »
Thanks guys for all the feed back.  With so much here I'm not quite sure where to begin to resolve my problem.  The clip of code from Bart on Pg 2 looks like a good place to start though.  At least the problem with YearsBetween was brought to light. %) %)

More testing to go!!

BZMan24
==============
OS: Win7 - i5 - win64
Linux Mint 17.3
Lazarus: V1.8.2 / FPC - 3.0.4

 

TinyPortal © 2005-2018