Browse Source

* Support for exact YearsBetween calculation (bug ID 31233)

git-svn-id: trunk@35423 -
michael 8 years ago
parent
commit
dc500f7276
1 changed files with 14 additions and 3 deletions
  1. 14 3
      packages/rtl-objpas/src/inc/dateutil.inc

+ 14 - 3
packages/rtl-objpas/src/inc/dateutil.inc

@@ -253,7 +253,7 @@ Function WithinPastMilliSeconds(const ANow, AThen: TDateTime; const AMilliSecond
     Period functions.
     Period functions.
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
 
 
-Function YearsBetween(const ANow, AThen: TDateTime): Integer;
+Function YearsBetween(const ANow, AThen: TDateTime; AExact : Boolean = False): Integer;
 Function MonthsBetween(const ANow, AThen: TDateTime; AExact : Boolean = False): Integer;
 Function MonthsBetween(const ANow, AThen: TDateTime; AExact : Boolean = False): Integer;
 Function WeeksBetween(const ANow, AThen: TDateTime): Integer;
 Function WeeksBetween(const ANow, AThen: TDateTime): Integer;
 Function DaysBetween(const ANow, AThen: TDateTime): Integer;
 Function DaysBetween(const ANow, AThen: TDateTime): Integer;
@@ -1281,9 +1281,20 @@ begin
 end;
 end;
 
 
 
 
-Function YearsBetween(const ANow, AThen: TDateTime): Integer;
+Function YearsBetween(const ANow, AThen: TDateTime; AExact : Boolean = False): Integer;
+
+var
+  yy, mm, dd: Word;
+  
 begin
 begin
-  Result:=Trunc((Abs(DateTimeDiff(ANow,AThen))+HalfMilliSecond)/ApproxDaysPerYear);
+  if AExact and (ANow >= -DateDelta) and (AThen >= -DateDelta) and
+     (ANow <= MaxDateTime) and (AThen <= MaxDateTime) then
+    begin
+    PeriodBetween(ANow, AThen, yy , mm, dd);
+    Result := yy;
+    end
+  else
+    Result:=Trunc((Abs(DateTimeDiff(ANow,AThen))+HalfMilliSecond)/ApproxDaysPerYear);
 end;
 end;