Browse Source

* Refactoring of dateutils by barlone (Bug ID 19000)

git-svn-id: trunk@17218 -
michael 14 years ago
parent
commit
8384a5ad04
1 changed files with 43 additions and 93 deletions
  1. 43 93
      rtl/objpas/dateutil.inc

+ 43 - 93
rtl/objpas/dateutil.inc

@@ -21,7 +21,7 @@ interface
 
 
 {$ifndef FPUNONE}
 {$ifndef FPUNONE}
 uses
 uses
-  SysUtils, Math, Types;
+  SysUtils, Math;
 
 
 { ---------------------------------------------------------------------
 { ---------------------------------------------------------------------
     Various constants
     Various constants
@@ -440,13 +440,13 @@ end;
     Simple trimming functions.
     Simple trimming functions.
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
 
 
-Function DateOf(const AValue: TDateTime): TDateTime;
+Function DateOf(const AValue: TDateTime): TDateTime; inline;
 begin
 begin
   Result:=Trunc(AValue);
   Result:=Trunc(AValue);
 end;
 end;
 
 
 
 
-Function TimeOf(const AValue: TDateTime): TDateTime;
+Function TimeOf(const AValue: TDateTime): TDateTime; inline;
 begin
 begin
   Result:=Frac(Avalue);
   Result:=Frac(Avalue);
 end;
 end;
@@ -458,24 +458,14 @@ end;
 
 
 
 
 Function IsInLeapYear(const AValue: TDateTime): Boolean;
 Function IsInLeapYear(const AValue: TDateTime): Boolean;
-
-Var
-  D,Y,M : Word;
-
 begin
 begin
-  DecodeDate(AValue,Y,M,D);
-  Result:=IsLeapYear(Y);
+  Result:=IsLeapYear(YearOf(AValue));
 end;
 end;
 
 
 
 
-Function IsPM(const AValue: TDateTime): Boolean;
-
-Var
-  H,M,S,MS : Word;
-
+Function IsPM(const AValue: TDateTime): Boolean; inline;
 begin
 begin
-  DecodeTime(AValue,H,M,S,MS);
-  Result:=(H>=12);
+  Result:=(HourOf(AValue)>=12);
 end;
 end;
 
 
 
 
@@ -535,13 +525,8 @@ end;
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
 
 
 Function WeeksInYear(const AValue: TDateTime): Word;
 Function WeeksInYear(const AValue: TDateTime): Word;
-
-Var
-  Y,M,D : Word;
-
 begin
 begin
-  DecodeDate(AValue,Y,M,D);
-  Result:=WeeksInAYear(Y);
+  Result:=WeeksInAYear(YearOf(AValue));
 end;
 end;
 
 
 
 
@@ -559,13 +544,8 @@ end;
 
 
 
 
 Function DaysInYear(const AValue: TDateTime): Word;
 Function DaysInYear(const AValue: TDateTime): Word;
-
-Var
-  Y,M,D : Word;
-
 begin
 begin
-  DecodeDate(AValue,Y,M,D);
-  Result:=DaysPerYear[IsLeapYear(Y)];
+  Result:=DaysPerYear[IsLeapYear(YearOf(AValue))];
 end;
 end;
 
 
 
 
@@ -597,27 +577,27 @@ end;
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
 
 
 
 
-Function Today: TDateTime;
+Function Today: TDateTime; inline;
 begin
 begin
-  Result:=Date;
+  Result:=Date();
 end;
 end;
 
 
 
 
 Function Yesterday: TDateTime;
 Function Yesterday: TDateTime;
 begin
 begin
-  Result:=Date-1;
+  Result:=Date()-1;
 end;
 end;
 
 
 
 
-Function Tomorrow: TDateTime;
+Function Tomorrow: TDateTime; 
 begin
 begin
-  Result:=Date+1;
+  Result:=Date()+1;
 end;
 end;
 
 
 
 
 Function IsToday(const AValue: TDateTime): Boolean;
 Function IsToday(const AValue: TDateTime): Boolean;
 begin
 begin
-  Result:=IsSameDay(AValue,Date);
+  Result:=IsSameDay(AValue,Date());
 end;
 end;
 
 
 
 
@@ -669,7 +649,7 @@ begin
 end;
 end;
 
 
 
 
-Function WeekOf(const AValue: TDateTime): Word;
+Function WeekOf(const AValue: TDateTime): Word; inline;
 begin
 begin
   Result:=WeekOfTheYear(AValue);
   Result:=WeekOfTheYear(AValue);
 end;
 end;
@@ -711,7 +691,7 @@ Var
   H,N,MS : Word;
   H,N,MS : Word;
 
 
 begin
 begin
-  DecodeTime(AVAlue,H,N,Result,MS);
+  DecodeTime(AValue,H,N,Result,MS);
 end;
 end;
 
 
 
 
@@ -731,24 +711,14 @@ end;
 
 
 
 
 Function StartOfTheYear(const AValue: TDateTime): TDateTime;
 Function StartOfTheYear(const AValue: TDateTime): TDateTime;
-
-Var
-  Y,M,D : Word;
-
 begin
 begin
-  DecodeDate(AValue,Y,M,D);
-  Result:=EncodeDate(Y,1,1);
+  Result:=EncodeDate(YearOf(AValue),1,1);
 end;
 end;
 
 
 
 
 Function EndOfTheYear(const AValue: TDateTime): TDateTime;
 Function EndOfTheYear(const AValue: TDateTime): TDateTime;
-
-Var
-  Y,M,D : Word;
-
 begin
 begin
-  DecodeDate(AValue,Y,M,D);
-  Result:=EncodeDateTime(Y,12,31,23,59,59,999);
+  Result:=EncodeDateTime(YearOf(AValue),12,31,23,59,59,999);
 end;
 end;
 
 
 
 
@@ -791,7 +761,7 @@ begin
 end;
 end;
 
 
 
 
-Function StartOfAMonth(const AYear, AMonth: Word): TDateTime;
+Function StartOfAMonth(const AYear, AMonth: Word): TDateTime; inline;
 begin
 begin
   Result:=EncodeDate(AYear,AMonth,1);
   Result:=EncodeDate(AYear,AMonth,1);
 end;
 end;
@@ -827,13 +797,13 @@ begin
 end;
 end;
 
 
 
 
-Function StartOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // ADayOFWeek 1
+Function StartOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; inline; // ADayOFWeek 1
 begin
 begin
   Result:=StartOfAWeek(AYear,AWeekOfYear,1)
   Result:=StartOfAWeek(AYear,AWeekOfYear,1)
 end;
 end;
 
 
 
 
-Function EndOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
+Function EndOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime; inline;
 begin
 begin
   Result := EndOfTheDay(EncodeDateWeek(AYear, AWeekOfYear, ADayOfWeek));
   Result := EndOfTheDay(EncodeDateWeek(AYear, AWeekOfYear, ADayOfWeek));
 end;
 end;
@@ -850,7 +820,7 @@ end;
     Start/End of day functions.
     Start/End of day functions.
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
 
 
-Function StartOfTheDay(const AValue: TDateTime): TDateTime;
+Function StartOfTheDay(const AValue: TDateTime): TDateTime; inline;
 begin
 begin
   StartOfTheDay:=Trunc(Avalue);
   StartOfTheDay:=Trunc(Avalue);
 end;
 end;
@@ -867,7 +837,7 @@ begin
 end;
 end;
 
 
 
 
-Function StartOfADay(const AYear, AMonth, ADay: Word): TDateTime;
+Function StartOfADay(const AYear, AMonth, ADay: Word): TDateTime; inline;
 begin
 begin
   Result:=EncodeDate(AYear,AMonth,ADay);
   Result:=EncodeDate(AYear,AMonth,ADay);
 end;
 end;
@@ -879,7 +849,7 @@ begin
 end;
 end;
 
 
 
 
-Function EndOfADay(const AYear, AMonth, ADay: Word): TDateTime;
+Function EndOfADay(const AYear, AMonth, ADay: Word): TDateTime; inline;
 begin
 begin
   Result:=EndOfTheDay(EncodeDate(AYear,AMonth,ADay));
   Result:=EndOfTheDay(EncodeDate(AYear,AMonth,ADay));
 end;
 end;
@@ -897,13 +867,9 @@ end;
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
 
 
 
 
-Function MonthOfTheYear(const AValue: TDateTime): Word;
-
-Var
-  Y,D : Word;
-
+Function MonthOfTheYear(const AValue: TDateTime): Word; inline;
 begin
 begin
-  DecodeDate(AValue,Y,Result,D);
+  Result:=MonthOf(AValue);
 end;
 end;
 
 
 
 
@@ -1116,13 +1082,9 @@ end;
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
 
 
 
 
-Function HourOfTheDay(const AValue: TDateTime): Word;
-
-Var
-  M,S,MS : Word;
-
+Function HourOfTheDay(const AValue: TDateTime): Word; inline;
 begin
 begin
-  DecodeTime(AValue,Result,M,S,MS);
+  Result:=HourOf(AValue);
 end;
 end;
 
 
 
 
@@ -1163,13 +1125,9 @@ end;
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
 
 
 
 
-Function MinuteOfTheHour(const AValue: TDateTime): Word;
-
-Var
-  H,S,MS : Word;
-
+Function MinuteOfTheHour(const AValue: TDateTime): Word; inline;
 begin
 begin
-  DecodeTime(AValue,H,Result,S,MS);
+  Result:=MinuteOf(AValue);
 end;
 end;
 
 
 
 
@@ -1199,13 +1157,9 @@ end;
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
 
 
 
 
-Function SecondOfTheMinute(const AValue: TDateTime): Word;
-
-Var
-  H,M,MS : Word;
-
+Function SecondOfTheMinute(const AValue: TDateTime): Word; inline;
 begin
 begin
-  DecodeTime(AValue,H,M,Result,MS);
+  Result:=SecondOf(AValue);
 end;
 end;
 
 
 
 
@@ -1223,62 +1177,58 @@ end;
     Part of second functions.
     Part of second functions.
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
 
 
-Function MilliSecondOfTheSecond(const AValue: TDateTime): Word;
-
-Var
-  H,M,S : Word;
-
+Function MilliSecondOfTheSecond(const AValue: TDateTime): Word; inline;
 begin
 begin
-  DecodeTime(AValue,H,M,S,Result);
+  Result:=MilliSecondOf(AValue);
 end;
 end;
 
 
 { ---------------------------------------------------------------------
 { ---------------------------------------------------------------------
     Range checking functions.
     Range checking functions.
   ---------------------------------------------------------------------}
   ---------------------------------------------------------------------}
 
 
-Function WithinPastYears(const ANow, AThen: TDateTime; const AYears: Integer): Boolean;
+Function WithinPastYears(const ANow, AThen: TDateTime; const AYears: Integer): Boolean; inline;
 begin
 begin
   Result:=YearsBetween(ANow,AThen)<=AYears;
   Result:=YearsBetween(ANow,AThen)<=AYears;
 end;
 end;
 
 
 
 
-Function WithinPastMonths(const ANow, AThen: TDateTime; const AMonths: Integer): Boolean;
+Function WithinPastMonths(const ANow, AThen: TDateTime; const AMonths: Integer): Boolean; inline;
 begin
 begin
   Result:=MonthsBetween(ANow,AThen)<=AMonths;
   Result:=MonthsBetween(ANow,AThen)<=AMonths;
 end;
 end;
 
 
 
 
-Function WithinPastWeeks(const ANow, AThen: TDateTime; const AWeeks: Integer): Boolean;
+Function WithinPastWeeks(const ANow, AThen: TDateTime; const AWeeks: Integer): Boolean; inline;
 begin
 begin
   Result:=WeeksBetween(ANow,AThen)<=AWeeks;
   Result:=WeeksBetween(ANow,AThen)<=AWeeks;
 end;
 end;
 
 
 
 
-Function WithinPastDays(const ANow, AThen: TDateTime; const ADays: Integer): Boolean;
+Function WithinPastDays(const ANow, AThen: TDateTime; const ADays: Integer): Boolean; inline;
 begin
 begin
   Result:=DaysBetween(ANow,AThen)<=ADays;
   Result:=DaysBetween(ANow,AThen)<=ADays;
 end;
 end;
 
 
 
 
-Function WithinPastHours(const ANow, AThen: TDateTime; const AHours: Int64): Boolean;
+Function WithinPastHours(const ANow, AThen: TDateTime; const AHours: Int64): Boolean; inline;
 begin
 begin
   Result:=HoursBetween(ANow,AThen)<=AHours;
   Result:=HoursBetween(ANow,AThen)<=AHours;
 end;
 end;
 
 
 
 
-Function WithinPastMinutes(const ANow, AThen: TDateTime; const AMinutes: Int64): Boolean;
+Function WithinPastMinutes(const ANow, AThen: TDateTime; const AMinutes: Int64): Boolean; inline;
 begin
 begin
   Result:=MinutesBetween(ANow,AThen)<=AMinutes;
   Result:=MinutesBetween(ANow,AThen)<=AMinutes;
 end;
 end;
 
 
 
 
-Function WithinPastSeconds(const ANow, AThen: TDateTime; const ASeconds: Int64): Boolean;
+Function WithinPastSeconds(const ANow, AThen: TDateTime; const ASeconds: Int64): Boolean; inline;
 begin
 begin
   Result:=SecondsBetween(ANow,Athen)<=ASeconds;
   Result:=SecondsBetween(ANow,Athen)<=ASeconds;
 end;
 end;
 
 
 
 
-Function WithinPastMilliSeconds(const ANow, AThen: TDateTime; const AMilliSeconds: Int64): Boolean;
+Function WithinPastMilliSeconds(const ANow, AThen: TDateTime; const AMilliSeconds: Int64): Boolean; inline;
 begin
 begin
   Result:=MilliSecondsBetween(ANow,AThen)<=AMilliSeconds;
   Result:=MilliSecondsBetween(ANow,AThen)<=AMilliSeconds;
 end;
 end;
@@ -1888,7 +1838,7 @@ begin
 end;
 end;
 
 
 
 
-Function SameDate(const A, B: TDateTime): Boolean;
+Function SameDate(const A, B: TDateTime): Boolean; inline;
 begin
 begin
   Result:=Trunc(A)=Trunc(B);
   Result:=Trunc(A)=Trunc(B);
 end;
 end;