فهرست منبع

--- Merging r17218 into '.':
U rtl/objpas/dateutil.inc
--- Merging r17410 into '.':
G rtl/objpas/dateutil.inc

# revisions: 17218,17410
------------------------------------------------------------------------
r17218 | michael | 2011-04-02 11:51:01 +0200 (Sat, 02 Apr 2011) | 1 line
Changed paths:
M /trunk/rtl/objpas/dateutil.inc

* Refactoring of dateutils by barlone (Bug ID 19000)
------------------------------------------------------------------------
------------------------------------------------------------------------
r17410 | joost | 2011-05-08 14:28:41 +0200 (Sun, 08 May 2011) | 1 line
Changed paths:
M /trunk/rtl/objpas/dateutil.inc

* EncodeTimeInterval implemented, allowing time intervals>24 hour
------------------------------------------------------------------------

git-svn-id: branches/fixes_2_4@17577 -

marco 14 سال پیش
والد
کامیت
97bbb9d233
1فایلهای تغییر یافته به همراه67 افزوده شده و 93 حذف شده
  1. 67 93
      rtl/objpas/dateutil.inc

+ 67 - 93
rtl/objpas/dateutil.inc

@@ -21,7 +21,7 @@ interface
 
 {$ifndef FPUNONE}
 uses
-  SysUtils, Math, Types;
+  SysUtils, Math;
 
 { ---------------------------------------------------------------------
     Various constants
@@ -329,6 +329,13 @@ Function EncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word
 Procedure DecodeDateMonthWeek(const AValue: TDateTime; out AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);
 Function TryEncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word; out AValue: TDateTime): Boolean;
 
+{ ---------------------------------------------------------------------
+    Encode time interval, allowing hours>24
+  ---------------------------------------------------------------------}
+
+function TryEncodeTimeInterval(Hour, Min, Sec, MSec:word; Out Time : TDateTime) : boolean;
+function EncodeTimeInterval(Hour, Minute, Second, MilliSecond:word): TDateTime;
+
 { ---------------------------------------------------------------------
     Replace given element with supplied value.
   ---------------------------------------------------------------------}
@@ -440,13 +447,13 @@ end;
     Simple trimming functions.
   ---------------------------------------------------------------------}
 
-Function DateOf(const AValue: TDateTime): TDateTime;
+Function DateOf(const AValue: TDateTime): TDateTime; inline;
 begin
   Result:=Trunc(AValue);
 end;
 
 
-Function TimeOf(const AValue: TDateTime): TDateTime;
+Function TimeOf(const AValue: TDateTime): TDateTime; inline;
 begin
   Result:=Frac(Avalue);
 end;
@@ -458,24 +465,14 @@ end;
 
 
 Function IsInLeapYear(const AValue: TDateTime): Boolean;
-
-Var
-  D,Y,M : Word;
-
 begin
-  DecodeDate(AValue,Y,M,D);
-  Result:=IsLeapYear(Y);
+  Result:=IsLeapYear(YearOf(AValue));
 end;
 
 
-Function IsPM(const AValue: TDateTime): Boolean;
-
-Var
-  H,M,S,MS : Word;
-
+Function IsPM(const AValue: TDateTime): Boolean; inline;
 begin
-  DecodeTime(AValue,H,M,S,MS);
-  Result:=(H>=12);
+  Result:=(HourOf(AValue)>=12);
 end;
 
 
@@ -535,13 +532,8 @@ end;
   ---------------------------------------------------------------------}
 
 Function WeeksInYear(const AValue: TDateTime): Word;
-
-Var
-  Y,M,D : Word;
-
 begin
-  DecodeDate(AValue,Y,M,D);
-  Result:=WeeksInAYear(Y);
+  Result:=WeeksInAYear(YearOf(AValue));
 end;
 
 
@@ -559,13 +551,8 @@ end;
 
 
 Function DaysInYear(const AValue: TDateTime): Word;
-
-Var
-  Y,M,D : Word;
-
 begin
-  DecodeDate(AValue,Y,M,D);
-  Result:=DaysPerYear[IsLeapYear(Y)];
+  Result:=DaysPerYear[IsLeapYear(YearOf(AValue))];
 end;
 
 
@@ -597,27 +584,27 @@ end;
   ---------------------------------------------------------------------}
 
 
-Function Today: TDateTime;
+Function Today: TDateTime; inline;
 begin
-  Result:=Date;
+  Result:=Date();
 end;
 
 
 Function Yesterday: TDateTime;
 begin
-  Result:=Date-1;
+  Result:=Date()-1;
 end;
 
 
-Function Tomorrow: TDateTime;
+Function Tomorrow: TDateTime; 
 begin
-  Result:=Date+1;
+  Result:=Date()+1;
 end;
 
 
 Function IsToday(const AValue: TDateTime): Boolean;
 begin
-  Result:=IsSameDay(AValue,Date);
+  Result:=IsSameDay(AValue,Date());
 end;
 
 
@@ -669,7 +656,7 @@ begin
 end;
 
 
-Function WeekOf(const AValue: TDateTime): Word;
+Function WeekOf(const AValue: TDateTime): Word; inline;
 begin
   Result:=WeekOfTheYear(AValue);
 end;
@@ -711,7 +698,7 @@ Var
   H,N,MS : Word;
 
 begin
-  DecodeTime(AVAlue,H,N,Result,MS);
+  DecodeTime(AValue,H,N,Result,MS);
 end;
 
 
@@ -731,24 +718,14 @@ end;
 
 
 Function StartOfTheYear(const AValue: TDateTime): TDateTime;
-
-Var
-  Y,M,D : Word;
-
 begin
-  DecodeDate(AValue,Y,M,D);
-  Result:=EncodeDate(Y,1,1);
+  Result:=EncodeDate(YearOf(AValue),1,1);
 end;
 
 
 Function EndOfTheYear(const AValue: TDateTime): TDateTime;
-
-Var
-  Y,M,D : Word;
-
 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;
 
 
@@ -791,7 +768,7 @@ begin
 end;
 
 
-Function StartOfAMonth(const AYear, AMonth: Word): TDateTime;
+Function StartOfAMonth(const AYear, AMonth: Word): TDateTime; inline;
 begin
   Result:=EncodeDate(AYear,AMonth,1);
 end;
@@ -827,13 +804,13 @@ begin
 end;
 
 
-Function StartOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; // ADayOFWeek 1
+Function StartOfAWeek(const AYear, AWeekOfYear: Word): TDateTime; inline; // ADayOFWeek 1
 begin
   Result:=StartOfAWeek(AYear,AWeekOfYear,1)
 end;
 
 
-Function EndOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;
+Function EndOfAWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime; inline;
 begin
   Result := EndOfTheDay(EncodeDateWeek(AYear, AWeekOfYear, ADayOfWeek));
 end;
@@ -850,7 +827,7 @@ end;
     Start/End of day functions.
   ---------------------------------------------------------------------}
 
-Function StartOfTheDay(const AValue: TDateTime): TDateTime;
+Function StartOfTheDay(const AValue: TDateTime): TDateTime; inline;
 begin
   StartOfTheDay:=Trunc(Avalue);
 end;
@@ -867,7 +844,7 @@ begin
 end;
 
 
-Function StartOfADay(const AYear, AMonth, ADay: Word): TDateTime;
+Function StartOfADay(const AYear, AMonth, ADay: Word): TDateTime; inline;
 begin
   Result:=EncodeDate(AYear,AMonth,ADay);
 end;
@@ -879,7 +856,7 @@ begin
 end;
 
 
-Function EndOfADay(const AYear, AMonth, ADay: Word): TDateTime;
+Function EndOfADay(const AYear, AMonth, ADay: Word): TDateTime; inline;
 begin
   Result:=EndOfTheDay(EncodeDate(AYear,AMonth,ADay));
 end;
@@ -897,13 +874,9 @@ end;
   ---------------------------------------------------------------------}
 
 
-Function MonthOfTheYear(const AValue: TDateTime): Word;
-
-Var
-  Y,D : Word;
-
+Function MonthOfTheYear(const AValue: TDateTime): Word; inline;
 begin
-  DecodeDate(AValue,Y,Result,D);
+  Result:=MonthOf(AValue);
 end;
 
 
@@ -1116,13 +1089,9 @@ end;
   ---------------------------------------------------------------------}
 
 
-Function HourOfTheDay(const AValue: TDateTime): Word;
-
-Var
-  M,S,MS : Word;
-
+Function HourOfTheDay(const AValue: TDateTime): Word; inline;
 begin
-  DecodeTime(AValue,Result,M,S,MS);
+  Result:=HourOf(AValue);
 end;
 
 
@@ -1163,13 +1132,9 @@ end;
   ---------------------------------------------------------------------}
 
 
-Function MinuteOfTheHour(const AValue: TDateTime): Word;
-
-Var
-  H,S,MS : Word;
-
+Function MinuteOfTheHour(const AValue: TDateTime): Word; inline;
 begin
-  DecodeTime(AValue,H,Result,S,MS);
+  Result:=MinuteOf(AValue);
 end;
 
 
@@ -1199,13 +1164,9 @@ end;
   ---------------------------------------------------------------------}
 
 
-Function SecondOfTheMinute(const AValue: TDateTime): Word;
-
-Var
-  H,M,MS : Word;
-
+Function SecondOfTheMinute(const AValue: TDateTime): Word; inline;
 begin
-  DecodeTime(AValue,H,M,Result,MS);
+  Result:=SecondOf(AValue);
 end;
 
 
@@ -1223,62 +1184,58 @@ end;
     Part of second functions.
   ---------------------------------------------------------------------}
 
-Function MilliSecondOfTheSecond(const AValue: TDateTime): Word;
-
-Var
-  H,M,S : Word;
-
+Function MilliSecondOfTheSecond(const AValue: TDateTime): Word; inline;
 begin
-  DecodeTime(AValue,H,M,S,Result);
+  Result:=MilliSecondOf(AValue);
 end;
 
 { ---------------------------------------------------------------------
     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
   Result:=YearsBetween(ANow,AThen)<=AYears;
 end;
 
 
-Function WithinPastMonths(const ANow, AThen: TDateTime; const AMonths: Integer): Boolean;
+Function WithinPastMonths(const ANow, AThen: TDateTime; const AMonths: Integer): Boolean; inline;
 begin
   Result:=MonthsBetween(ANow,AThen)<=AMonths;
 end;
 
 
-Function WithinPastWeeks(const ANow, AThen: TDateTime; const AWeeks: Integer): Boolean;
+Function WithinPastWeeks(const ANow, AThen: TDateTime; const AWeeks: Integer): Boolean; inline;
 begin
   Result:=WeeksBetween(ANow,AThen)<=AWeeks;
 end;
 
 
-Function WithinPastDays(const ANow, AThen: TDateTime; const ADays: Integer): Boolean;
+Function WithinPastDays(const ANow, AThen: TDateTime; const ADays: Integer): Boolean; inline;
 begin
   Result:=DaysBetween(ANow,AThen)<=ADays;
 end;
 
 
-Function WithinPastHours(const ANow, AThen: TDateTime; const AHours: Int64): Boolean;
+Function WithinPastHours(const ANow, AThen: TDateTime; const AHours: Int64): Boolean; inline;
 begin
   Result:=HoursBetween(ANow,AThen)<=AHours;
 end;
 
 
-Function WithinPastMinutes(const ANow, AThen: TDateTime; const AMinutes: Int64): Boolean;
+Function WithinPastMinutes(const ANow, AThen: TDateTime; const AMinutes: Int64): Boolean; inline;
 begin
   Result:=MinutesBetween(ANow,AThen)<=AMinutes;
 end;
 
 
-Function WithinPastSeconds(const ANow, AThen: TDateTime; const ASeconds: Int64): Boolean;
+Function WithinPastSeconds(const ANow, AThen: TDateTime; const ASeconds: Int64): Boolean; inline;
 begin
   Result:=SecondsBetween(ANow,Athen)<=ASeconds;
 end;
 
 
-Function WithinPastMilliSeconds(const ANow, AThen: TDateTime; const AMilliSeconds: Int64): Boolean;
+Function WithinPastMilliSeconds(const ANow, AThen: TDateTime; const AMilliSeconds: Int64): Boolean; inline;
 begin
   Result:=MilliSecondsBetween(ANow,AThen)<=AMilliSeconds;
 end;
@@ -1742,6 +1699,23 @@ begin
     end;
 end;
 
+{ ---------------------------------------------------------------------
+    Encode time interval, allowing hours>24
+  ---------------------------------------------------------------------}
+
+function TryEncodeTimeInterval(Hour, Min, Sec, MSec: word; out Time: TDateTime): boolean;
+begin
+ Result:= (Min<60) and (Sec<60) and (MSec<1000);
+ If Result then
+   Time:=TDateTime(cardinal(Hour)*3600000+cardinal(Min)*60000+cardinal(Sec)*1000+MSec)/MSecsPerDay;
+end;
+
+function EncodeTimeInterval(Hour, Minute, Second, MilliSecond: word): TDateTime;
+begin
+   If not TryEncodeTimeInterval(Hour,Minute,Second,MilliSecond,Result) then
+     Raise EConvertError.CreateFmt('%d:%d:%d.%d is not a valid time specification',
+                               [Hour,Minute,Second,MilliSecond]);
+end;
 
 { ---------------------------------------------------------------------
     Replace given element with supplied value.
@@ -1888,7 +1862,7 @@ begin
 end;
 
 
-Function SameDate(const A, B: TDateTime): Boolean;
+Function SameDate(const A, B: TDateTime): Boolean; inline;
 begin
   Result:=Trunc(A)=Trunc(B);
 end;