📄 dateutils.pp
字号:
{ --------------------------------------------------------------------- Encode/Decode of complete timestamp ---------------------------------------------------------------------}Function EncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;begin If Not TryEncodeDateTime(AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond,Result) then InvalidDateTimeError(AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond)end;Procedure DecodeDateTime(const AValue: TDateTime; var AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word);begin DecodeDate(AValue,AYear,AMonth,ADay); DecodeTime(AValue,AHour,AMinute,ASecond,AMilliSecond);end;Function TryEncodeDateTime(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; var AValue: TDateTime): Boolean;Var tmp : TDateTime;begin Result:=TryEncodeDate(AYear,AMonth,ADay,AValue); Result:=Result and TryEncodeTime(AHour,AMinute,ASecond,Amillisecond,Tmp); If Result then Avalue:=AValue+Tmp;end;{ --------------------------------------------------------------------- Encode/decode date, specifying week of year and day of week ---------------------------------------------------------------------}Function EncodeDateWeek(const AYear, AWeekOfYear: Word; const ADayOfWeek: Word): TDateTime;begin If Not TryEncodeDateWeek(AYear,AWeekOfYear,Result,ADayOfWeek) then InvalidDateWeekError(AYear,AWeekOfYear,ADayOfWeek);end;Function EncodeDateWeek(const AYear, AWeekOfYear: Word): TDateTime; //; const ADayOfWeek: Word = 1begin EncodeDateWeek(AYear,AWeekOfYear,1);end;Procedure DecodeDateWeek(const AValue: TDateTime; var AYear, AWeekOfYear, ADayOfWeek: Word);begin NotYetImplemented('DecodeDateWeek');end;Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; var AValue: TDateTime; const ADayOfWeek: Word): Boolean;Var DOW : Word; Rest : Integer;begin Result:=IsValidDateWeek(Ayear,AWeekOfYear,ADayOfWeek); If Result then begin AValue:=EncodeDate(AYear,1,1)+(7*(AWeekOfYear-1)); DOW:=DayOfTheWeek(AValue); Rest:=ADayOfWeek-DOW; If (DOW>4) then Inc(Rest,7); AValue:=AValue+Rest; end;end;Function TryEncodeDateWeek(const AYear, AWeekOfYear: Word; var AValue: TDateTime): Boolean; //; const ADayOfWeek: Word = 1begin Result:=TryEncodeDateWeek(AYear,AWeekOfYear,AValue,1);end;{ --------------------------------------------------------------------- Encode/decode date, specifying day of year ---------------------------------------------------------------------}Function EncodeDateDay(const AYear, ADayOfYear: Word): TDateTime;begin If Not TryEncodeDateDay(AYear,ADayOfYear,Result) then InvalidDateDayError(AYear,ADayOfYear);end;Procedure DecodeDateDay(const AValue: TDateTime; var AYear, ADayOfYear: Word);Var M,D : Word;begin DecodeDate(AValue,AYear,M,D); ADayOfyear:=Trunc(AValue-EncodeDate(AYear,1,1))+1;end;Function TryEncodeDateDay(const AYear, ADayOfYear: Word; var AValue: TDateTime): Boolean;begin Result:=(ADayOfYear<>0) and (ADayOfYear<=DaysPerYear [IsleapYear(AYear)]); If Result then AValue:=EncodeDate(AYear,1,1)+ADayOfYear-1;end;{ --------------------------------------------------------------------- Encode/decode date, specifying week of month ---------------------------------------------------------------------}Function EncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word): TDateTime;begin If Not TryEncodeDateMonthWeek(Ayear,AMonth,AWeekOfMonth,ADayOfWeek,Result) then InvalidDateMonthWeekError(AYear,AMonth,AWeekOfMonth,ADayOfWeek);end;Procedure DecodeDateMonthWeek(const AValue: TDateTime; var AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);Var D,SDOM,EDOM : Word; SOM,EOM : TdateTime; DOM : Integer;begin DecodeDate(AValue,AYear,AMonth,D); ADayOfWeek:=DayOfTheWeek(AValue); SOM:=EncodeDate(Ayear,Amonth,1); SDOM:=DayOfTheWeek(SOM); DOM:=D-1+SDOM; If SDOM>4 then Dec(DOM,7); // Too early in the month. First full week is next week, day is after thursday. If DOM<=0 Then DecodeDateMonthWeek(SOM-1,AYear,AMonth,AWeekOfMonth,D) else begin AWeekOfMonth:=(DOM div 7)+Ord((DOM mod 7)<>0); EDOM:=DayOfTheWeek(EndOfAMonth(Ayear,AMonth)); // In last days of last long week, so in next month... If (EDOM<4) and ((DaysInAMonth(AYear,Amonth)-D)<EDOM) then begin AWeekOfMonth:=1; Inc(AMonth); If (AMonth=13) then begin AMonth:=1; Inc(AYear); end; end; end;end;Function TryEncodeDateMonthWeek(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word; var AValue: TDateTime): Boolean;begin NotYetImplemented('TryEncodeDateMonthWeek');end;{ --------------------------------------------------------------------- Replace given element with supplied value. ---------------------------------------------------------------------}Const LFAI = RecodeLeaveFieldAsIS; // Less typing, readable code{ Note: We have little choice but to implement it like Borland did: If AValue contains some 'wrong' value, it will throw an error. To simulate this we'd have to check in each function whether both arguments are correct. To avoid it, all is routed through the 'central' RecodeDateTime function as in Borland's implementation.}Function RecodeYear(const AValue: TDateTime; const AYear: Word): TDateTime;begin RecodeDateTime(AValue,AYear,LFAI,LFAI,LFAI,LFAI,LFAI,LFAI);end;Function RecodeMonth(const AValue: TDateTime; const AMonth: Word): TDateTime;begin RecodeDateTime(AValue,LFAI,AMonth,LFAI,LFAI,LFAI,LFAI,LFAI);end;Function RecodeDay(const AValue: TDateTime; const ADay: Word): TDateTime;begin RecodeDateTime(AValue,LFAI,LFAI,ADay,LFAI,LFAI,LFAI,LFAI);end;Function RecodeHour(const AValue: TDateTime; const AHour: Word): TDateTime;begin RecodeDateTime(AValue,LFAI,LFAI,LFAI,AHour,LFAI,LFAI,LFAI);end;Function RecodeMinute(const AValue: TDateTime; const AMinute: Word): TDateTime;begin RecodeDateTime(AValue,LFAI,LFAI,LFAI,LFAI,AMinute,LFAI,LFAI);end;Function RecodeSecond(const AValue: TDateTime; const ASecond: Word): TDateTime;begin RecodeDateTime(AValue,LFAI,LFAI,LFAI,LFAI,LFAI,ASecond,LFAI);end;Function RecodeMilliSecond(const AValue: TDateTime; const AMilliSecond: Word): TDateTime;begin RecodeDateTime(AValue,LFAI,LFAI,LFAI,LFAI,LFAI,LFAI,AMilliSecond);end;Function RecodeDate(const AValue: TDateTime; const AYear, AMonth, ADay: Word): TDateTime;begin RecodeDateTime(AValue,AYear,AMonth,ADay,LFAI,LFAI,LFAI,LFAI);end;Function RecodeTime(const AValue: TDateTime; const AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;begin RecodeDateTime(AValue,LFAI,LFAI,LFAI,AHour,AMinute,ASecond,AMilliSecond);end;Function RecodeDateTime(const AValue: TDateTime; const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word): TDateTime;begin If Not TryRecodeDateTime(AValue,AYear,AMonth,ADay,AHour,AMinute,ASecond,AMilliSecond,Result) then InvalidDateTimeError(AYear,AMonth,ADay,AHour,AMinute,ASecond,AMilliSecond,AValue);end;Function TryRecodeDateTime(const AValue: TDateTime; const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; var AResult: TDateTime): Boolean; Procedure FV (Var FV : Word; Arg : Word); begin If (Arg<>LFAI) then FV:=Arg; end;Var Y,M,D,H,N,S,MS : Word;begin DecodeDateTime(AValue,Y,M,D,H,N,S,MS); FV(Y,AYear); FV(M,AMonth); FV(D,ADay); FV(H,AHour); FV(N,AMinute); FV(S,ASecond); FV(S,AMillisecond); Result:=TryEncodeDateTime(Y,M,D,H,N,S,MS,AResult);end;{ --------------------------------------------------------------------- Comparision of date/time ---------------------------------------------------------------------}Function CompareDateTime(const A, B: TDateTime): TValueRelationship;begin If SameDateTime(A,B) then Result:=EqualsValue else If A>B then Result:=GreaterThanValue else Result:=LessThanValueend;Function CompareDate(const A, B: TDateTime): TValueRelationship;begin If SameDate(A,B) then Result:=EQualsValue else if A<B then Result:=LessThanValue else Result:=GreaterThanValue;end;Function CompareTime(const A, B: TDateTime): TValueRelationship;begin If SameTime(A,B) then Result:=EQualsValue else If Frac(A)<Frac(B) then Result:=LessThanValue else Result:=GreaterThanValue;end;Function SameDateTime(const A, B: TDateTime): Boolean;begin Result:=Abs(A-B)<OneMilliSecond;end;Function SameDate(const A, B: TDateTime): Boolean;begin Result:=Trunc(A)=Trunc(B);end;Function SameTime(const A, B: TDateTime): Boolean;begin Result:=Frac(Abs(A-B))<OneMilliSecond;end;Function NthDayOfWeek(const AValue: TDateTime): Word;begin Result:=(DayOfTheMonth(AValue)-1) div 7 + 1;end;Procedure DecodeDayOfWeekInMonth(const AValue: TDateTime; var AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word);begin NotYetImplemented('DecodeDayOfWeekInMonth');end;Function EncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word): TDateTime;begin If Not TryEncodeDayOfWeekInMonth(AYear,AMonth,ANthDayOfWeek,ADayOfWeek,Result) then InvalidDayOfWeekInMonthError(AYear,AMonth,ANthDayOfWeek,ADayOfWeek);end;Function TryEncodeDayOfWeekInMonth(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word; var AValue: TDateTime): Boolean;Var SOM,D : Word;begin SOM:=DayOfTheWeek(EncodeDate(Ayear,AMonth,1)); D:=1+ADayOfWeek-SOM+7*(ANthDayOfWeek-1); If SOM>ADayOfWeek then D:=D+7; // Clearer would have been Inc(ANthDayOfweek) but it's a const Result:=TryEncodeDate(Ayear,AMonth,D,AValue);end;{ --------------------------------------------------------------------- Exception throwing routines ---------------------------------------------------------------------}Procedure InvalidDateTimeError(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word; const ABaseDate: TDateTime); Function DoField(Arg,Def : Word; Unknown: String) : String; begin If (Arg<>LFAI) then Result:=Format('%.*d',[Length(Unknown),Arg]) else if (ABaseDate=0) then Result:=Unknown else Result:=Format('%.*d',[Length(Unknown),Arg]); end;Var Y,M,D,H,N,S,MS : Word; Msg : String;begin DecodeDateTime(ABasedate,Y,M,D,H,N,S,MS); Msg:=DoField(AYear,Y,'????'); Msg:=Msg+DateSeparator+DoField(AMonth,M,'??'); Msg:=Msg+DateSeparator+DoField(ADay,D,'??'); Msg:=Msg+' '+DoField(AHour,H,'??'); Msg:=Msg+TimeSeparator+DoField(AMinute,N,'??'); Msg:=Msg+TimeSeparator+Dofield(ASecond,S,'??'); Msg:=Msg+DecimalSeparator+DoField(AMilliSecond,MS,'???'); Raise EConvertError.CreateFmt(SErrInvalidTimeStamp,[Msg]);end;Procedure InvalidDateTimeError(const AYear, AMonth, ADay, AHour, AMinute, ASecond, AMilliSecond: Word); // const ABaseDate: TDateTime = 0begin InvalidDateTimeError(AYear,AMonth,ADay,AHour,AMinute,ASecond,AMilliSecond,0);end;Procedure InvalidDateWeekError(const AYear, AWeekOfYear, ADayOfWeek: Word);begin Raise EConvertError.CreateFmt(SErrInvalidDateWeek,[AYear,AWeekOfYear,ADayOfWeek]);end;Procedure InvalidDateDayError(const AYear, ADayOfYear: Word);begin Raise EConvertError.CreateFmt(SErrInvalidDayOfYear,[AYear,ADayOfYear]);end;Procedure InvalidDateMonthWeekError(const AYear, AMonth, AWeekOfMonth, ADayOfWeek: Word);begin Raise EConvertError.CreateFmt(SErrInvalidDateMonthWeek,[Ayear,AMonth,AWeekOfMonth,ADayOfWeek]);end;Procedure InvalidDayOfWeekInMonthError(const AYear, AMonth, ANthDayOfWeek, ADayOfWeek: Word);begin Raise EConvertError.CreateFmt(SErrInvalidDayOfWeekInMonth,[AYear,AMonth,ANthDayOfWeek,ADayOfWeek]);end;{ --------------------------------------------------------------------- Julian and Modified Julian Date conversion support ---------------------------------------------------------------------}Function DateTimeToJulianDate(const AValue: TDateTime): Double;begin NotYetImplemented('DateTimeToJulianDate');end;Function JulianDateToDateTime(const AValue: Double): TDateTime;begin NotYetImplemented('JulianDateToDateTime');end;Function TryJulianDateToDateTime(const AValue: Double; var ADateTime: TDateTime): Boolean;begin NotYetImplemented('TryJulianDateToDateTime');end;Function DateTimeToModifiedJulianDate(const AValue: TDateTime): Double;begin NotYetImplemented('DateTimeToModifiedJulianDate');end;Function ModifiedJulianDateToDateTime(const AValue: Double): TDateTime;begin NotYetImplemented('ModifiedJulianDateToDateTime');end;Function TryModifiedJulianDateToDateTime(const AValue: Double; var ADateTime: TDateTime): Boolean;begin NotYetImplemented('TryModifiedJulianDateToDateTime');end;{ --------------------------------------------------------------------- Unix timestamp support. ---------------------------------------------------------------------}Function DateTimeToUnix(const AValue: TDateTime): Int64;begin NotYetImplemented('DateTimeToUnix');end;Function UnixToDateTime(const AValue: Int64): TDateTime;begin NotYetImplemented('UnixToDateTime');end;end.{ $Log: dateutils.pp,v $ Revision 1.1 2003/08/16 22:44:37 michael + Initial import Revision 1.2 2003/01/19 14:37:06 michael + Much more functions implemented}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -