Browse Source

* Support for exact MonthsBetween calculation (bug ID 31354)

git-svn-id: trunk@35422 -
michael 8 years ago
parent
commit
268693f0d5
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

@@ -254,7 +254,7 @@ Function WithinPastMilliSeconds(const ANow, AThen: TDateTime; const AMilliSecond
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
 
 
 Function YearsBetween(const ANow, AThen: TDateTime): Integer;
 Function YearsBetween(const ANow, AThen: TDateTime): Integer;
-Function MonthsBetween(const ANow, AThen: TDateTime): 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;
 Function HoursBetween(const ANow, AThen: TDateTime): Int64;
 Function HoursBetween(const ANow, AThen: TDateTime): Int64;
@@ -1287,9 +1287,20 @@ begin
 end;
 end;
 
 
 
 
-Function MonthsBetween(const ANow, AThen: TDateTime): Integer;
+Function MonthsBetween(const ANow, AThen: TDateTime; AExact : Boolean = False): Integer;
+
+var
+  y, m, d: Word;
+
 begin
 begin
-  Result:=Trunc((Abs(DateTimeDiff(ANow,AThen))+HalfMilliSecond)/ApproxDaysPerMonth);
+  if AExact and (ANow >= -DateDelta) and (AThen >= -DateDelta) and
+     (ANow <= MaxDateTime) and (AThen <= MaxDateTime) then
+    begin
+    PeriodBetween(ANow, AThen, y, m, d);
+    Result := y*12 + m;
+    end
+  else
+    Result:=Trunc((Abs(DateTimeDiff(ANow,AThen))+HalfMilliSecond)/ApproxDaysPerMonth);
 end;
 end;