12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136 |
- {
- *********************************************************************
- Copyright (C) 1997, 1998 Gertjan Schouten
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation; either version 2 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- *********************************************************************
- System Utilities For Free Pascal
- }
- {==============================================================================}
- { internal functions }
- {==============================================================================}
- Function DoEncodeDate(Year, Month, Day: Word): longint;
- Var
- D : TDateTime;
- begin
- If TryEncodeDate(Year,Month,Day,D) then
- Result:=Trunc(D)
- else
- Result:=0;
- end;
- function DoEncodeTime(Hour, Minute, Second, MilliSecond: word): TDateTime;
- begin
- If not TryEncodeTime(Hour,Minute,Second,MilliSecond,Result) then
- Result:=0;
- end;
- {==============================================================================}
- { Public functions }
- {==============================================================================}
- { ComposeDateTime converts a Date and a Time into one TDateTime }
- function ComposeDateTime(Date,Time : TDateTime) : TDateTime;
- begin
- if Date < 0 then Result := trunc(Date) - Abs(frac(Time))
- else Result := trunc(Date) + Abs(frac(Time));
- end;
- { DateTimeToTimeStamp converts DateTime to a TTimeStamp }
- function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
- begin
- result.Time := Round(abs(Frac(DateTime)) * MSecsPerDay);
- result.Date := DateDelta + trunc(DateTime);
- end ;
- { TimeStampToDateTime converts TimeStamp to a TDateTime value }
- function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;
- begin
- Result := ComposeDateTime(TimeStamp.Date - DateDelta,TimeStamp.Time / MSecsPerDay)
- end;
- { MSecsToTimeStamp }
- function MSecsToTimeStamp(MSecs: comp): TTimeStamp;
- begin
- result.Date := Trunc(msecs / msecsperday);
- msecs:= msecs-comp(result.date)*msecsperday;
- result.Time := Round(MSecs);
- end ;
- { TimeStampToMSecs }
- function TimeStampToMSecs(const TimeStamp: TTimeStamp): comp;
- begin
- result := TimeStamp.Time + comp(timestamp.date)*msecsperday;
- end ;
- Function TryEncodeDate(Year,Month,Day : Word; Out Date : TDateTime) : Boolean;
- var
- c, ya: cardinal;
- begin
- Result:=(Year>0) and (Year<10000) and
- (Month in [1..12]) and
- (Day>0) and (Day<=MonthDays[IsleapYear(Year),Month]);
- If Result then
- begin
- if month > 2 then
- Dec(Month,3)
- else
- begin
- Inc(Month,9);
- Dec(Year);
- end;
- c:= Year DIV 100;
- ya:= Year - 100*c;
- Date := (146097*c) SHR 2 + (1461*ya) SHR 2 + (153*cardinal(Month)+2) DIV 5 + cardinal(Day);
- // Note that this line can't be part of the line above, since TDateTime is
- // signed and c and ya are not
- Date := Date - 693900;
- end
- end;
- function TryEncodeTime(Hour, Min, Sec, MSec:word; Out Time : TDateTime) : boolean;
- begin
- Result:=(Hour<24) and (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;
- { EncodeDate packs three variables Year, Month and Day into a
- TDateTime value the result is the number of days since 12/30/1899 }
- function EncodeDate(Year, Month, Day: word): TDateTime;
- begin
- If Not TryEncodeDate(Year,Month,Day,Result) then
- Raise EConvertError.CreateFmt('%d-%d-%d is not a valid date specification',
- [Year,Month,Day]);
- end;
- { EncodeTime packs four variables Hour, Minute, Second and MilliSecond into
- a TDateTime value }
- function EncodeTime(Hour, Minute, Second, MilliSecond:word):TDateTime;
- begin
- If not TryEncodeTime(Hour,Minute,Second,MilliSecond,Result) then
- Raise EConvertError.CreateFmt('%d:%d:%d.%d is not a valid time specification',
- [Hour,Minute,Second,MilliSecond]);
- end;
- { DecodeDate unpacks the value Date into three values:
- Year, Month and Day }
- procedure DecodeDate(Date: TDateTime; out Year, Month, Day: word);
- var
- ly,ld,lm,j : cardinal;
- begin
- if Date <= -datedelta then // If Date is before 1-1-1 then return 0-0-0
- begin
- Year := 0;
- Month := 0;
- Day := 0;
- end
- else
- begin
- j := pred((Trunc(System.Int(Date)) + 693900) SHL 2);
- ly:= j DIV 146097;
- j:= j - 146097 * cardinal(ly);
- ld := j SHR 2;
- j:=(ld SHL 2 + 3) DIV 1461;
- ld:= (cardinal(ld) SHL 2 + 7 - 1461*j) SHR 2;
- lm:=(5 * ld-3) DIV 153;
- ld:= (5 * ld +2 - 153*lm) DIV 5;
- ly:= 100 * cardinal(ly) + j;
- if lm < 10 then
- inc(lm,3)
- else
- begin
- dec(lm,9);
- inc(ly);
- end;
- year:=ly;
- month:=lm;
- day:=ld;
- end;
- end;
- function DecodeDateFully(const DateTime: TDateTime; out Year, Month, Day, DOW: Word): Boolean;
- begin
- DecodeDate(DateTime,Year,Month,Day);
- DOW:=DayOfWeek(DateTime);
- Result:=IsLeapYear(Year);
- end;
- { DecodeTime unpacks Time into four values:
- Hour, Minute, Second and MilliSecond }
- procedure DecodeTime(Time: TDateTime; out Hour, Minute, Second, MilliSecond: word);
- Var
- l : cardinal;
- begin
- l := Round(abs(Frac(time)) * MSecsPerDay);
- Hour := l div 3600000;
- l := l mod 3600000;
- Minute := l div 60000;
- l := l mod 60000;
- Second := l div 1000;
- l := l mod 1000;
- MilliSecond := l;
- end;
- { DateTimeToSystemTime converts DateTime value to SystemTime }
- procedure DateTimeToSystemTime(DateTime: TDateTime; out SystemTime: TSystemTime);
- begin
- DecodeDate(DateTime, SystemTime.Year, SystemTime.Month, SystemTime.Day);
- DecodeTime(DateTime, SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond);
- end ;
- { SystemTimeToDateTime converts SystemTime to a TDateTime value }
- function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;
- begin
- result := ComposeDateTime(DoEncodeDate(SystemTime.Year, SystemTime.Month, SystemTime.Day),
- DoEncodeTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond));
- end ;
- { DayOfWeek returns the Day of the week (sunday is day 1) }
- function DayOfWeek(DateTime: TDateTime): integer;
- begin
- Result := 1 + (Abs(Trunc(DateTime) - 1) mod 7);
- end ;
- { Date returns the current Date }
- function Date: TDateTime;
- var
- SystemTime: TSystemTime;
- begin
- GetLocalTime(SystemTime);
- result := DoEncodeDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
- end ;
- { Time returns the current Time }
- function Time: TDateTime;
- var
- SystemTime: TSystemTime;
- begin
- GetLocalTime(SystemTime);
- Result := DoEncodeTime(SystemTime.Hour,SystemTime.Minute,SystemTime.Second,SystemTime.MilliSecond);
- end ;
- { Now returns the current Date and Time }
- function Now: TDateTime;
- var
- SystemTime: TSystemTime;
- begin
- GetLocalTime(SystemTime);
- result := systemTimeToDateTime(SystemTime);
- end;
- { IncMonth increments DateTime with NumberOfMonths months,
- NumberOfMonths can be less than zero }
- function IncMonth(const DateTime: TDateTime; NumberOfMonths: integer = 1 ): TDateTime;
- var
- Year, Month, Day : word;
- begin
- DecodeDate(DateTime, Year, Month, Day);
- IncAMonth(Year, Month, Day, NumberOfMonths);
- result := ComposeDateTime(DoEncodeDate(Year, Month, Day), DateTime);
- end ;
- { IncAMonth is the same as IncMonth, but operates on decoded date }
- procedure IncAMonth(var Year, Month, Day: Word; NumberOfMonths: Integer = 1);
- var
- TempMonth, S: Integer;
- begin
- If NumberOfMonths>=0 then
- s:=1
- else
- s:=-1;
- inc(Year,(NumberOfMonths div 12));
- TempMonth:=Month+(NumberOfMonths mod 12)-1;
- if (TempMonth>11) or
- (TempMonth<0) then
- begin
- Dec(TempMonth, S*12);
- Inc(Year, S);
- end;
- Month:=TempMonth+1; { Months from 1 to 12 }
- If (Day>MonthDays[IsLeapYear(Year)][Month]) then
- Day:=MonthDays[IsLeapYear(Year)][Month];
- end;
- { IsLeapYear returns true if Year is a leap year }
- function IsLeapYear(Year: Word): boolean;
- begin
- Result := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
- end;
- { DateToStr returns a string representation of Date using ShortDateFormat }
- function DateToStr(Date: TDateTime): string;
- begin
- result := FormatDateTime('ddddd', Date);
- end ;
- { TimeToStr returns a string representation of Time using LongTimeFormat }
- function TimeToStr(Time: TDateTime): string;
- begin
- result := FormatDateTime('tt', Time);
- end ;
- { DateTimeToStr returns a string representation of DateTime using LongDateTimeFormat }
- function DateTimeToStr(DateTime: TDateTime): string;
- begin
- result := FormatDateTime('c', DateTime);
- end ;
- { StrToDate converts the string S to a TDateTime value
- if S does not represent a valid date value
- an EConvertError will be raised }
- function IntStrToDate(Out ErrorMsg : AnsiString; const S: PChar; Len : integer; const useformat : string; separator : char = #0): TDateTime;
- const SInvalidDateFormat = '"%s" is not a valid date format';
- var
- df:string;
- d,m,y,ly:word;
- n,i:longint;
- c:word;
- dp,mp,yp,which : Byte;
- s1:string[4];
- values:array[1..3] of longint;
- LocalTime:tsystemtime;
- YearMoreThenTwoDigits : boolean;
- begin
- ErrorMsg:='';
- if (Len=0) then
- begin
- ErrorMsg:=Format(SInvalidDateFormat,['']);
- exit;
- end;
- YearMoreThenTwoDigits := False;
- if separator = #0 then
- separator := DateSeparator;
- df := UpperCase(useFormat);
- { Determine order of D,M,Y }
- yp:=0;
- mp:=0;
- dp:=0;
- Which:=0;
- i:=0;
- while (i<Length(df)) and (Which<3) do
- begin
- inc(i);
- Case df[i] of
- 'Y' :
- if yp=0 then
- begin
- Inc(Which);
- yp:=which;
- end;
- 'M' :
- if mp=0 then
- begin
- Inc(Which);
- mp:=which;
- end;
- 'D' :
- if dp=0 then
- begin
- Inc(Which);
- dp:=which;
- end;
- end;
- end;
- if Which<>3 then
- begin
- ErrorMsg:=Format(SErrIllegalDateFormatString,[useformat]);
- Exit;
- end;
- { Get actual values }
- for i := 1 to 3 do
- values[i] := 0;
- s1 := '';
- n := 0;
- dec(len);
- for i := 0 to len do
- begin
- if s[i] in ['0'..'9'] then
- s1 := s1 + s[i];
- { space can be part of the shortdateformat, and is defaultly in slovak
- windows, therefor it shouldn't be taken as separator (unless so specified)
- and ignored }
- if (Separator <> ' ') and (s[i] = ' ') then
- Continue;
- if (s[i] = separator) or ((i = len) and (s[i] in ['0'..'9'])) then
- begin
- inc(n);
- if n>3 then
- begin
- ErrorMsg:=Format(SInvalidDateFormat,[s]);
- exit;
- end;
- // Check if the year has more then two digits (if n=yp, then we are evaluating the year.)
- if (n=yp) and (length(s1)>2) then YearMoreThenTwoDigits := True;
- val(s1, values[n], c);
- if c<>0 then
- begin
- ErrorMsg:=Format(SInvalidDateFormat,[s]);
- Exit;
- end;
- s1 := '';
- end
- else if not (s[i] in ['0'..'9']) then
- begin
- ErrorMsg:=Format(SInvalidDateFormat,[s]);
- Exit;
- end;
- end ;
- // Fill in values.
- getLocalTime(LocalTime);
- ly := LocalTime.Year;
- If N=3 then
- begin
- y:=values[yp];
- m:=values[mp];
- d:=values[dp];
- end
- Else
- begin
- Y:=ly;
- If n<2 then
- begin
- d:=values[1];
- m := LocalTime.Month;
- end
- else
- If dp<mp then
- begin
- d:=values[1];
- m:=values[2];
- end
- else
- begin
- d:=values[2];
- m:=values[1];
- end;
- end;
- if (y >= 0) and (y < 100) and not YearMoreThenTwoDigits then
- begin
- ly := ly - TwoDigitYearCenturyWindow;
- Inc(Y, ly div 100 * 100);
- if (TwoDigitYearCenturyWindow > 0) and (Y < ly) then
- Inc(Y, 100);
- end;
- Result := EncodeDate(y, m, d);
- end ;
- function StrToDate(const S: PChar; Len : integer; const useformat : string; separator : char = #0): TDateTime;
- Var
- MSg : AnsiString;
- begin
- Result:=IntStrToDate(Msg,S,Len,useFormat,Separator);
- If (Msg<>'') then
- Raise EConvertError.Create(Msg);
- end;
- function StrToDate(const S: ShortString; const useformat : string; separator : char = #0): TDateTime;
- begin
- result := StrToDate(@S[1],Length(s),UseFormat,separator);
- end;
- function StrToDate(const S: AnsiString; const useformat : string; separator : char = #0): TDateTime;
- begin
- result := StrToDate(@S[1],Length(s),UseFormat,separator);
- end;
- function StrToDate(const S: ShortString; separator : char): TDateTime;
- begin
- result := StrToDate(@S[1],Length(s),ShortDateFormat,separator)
- end;
- function StrToDate(const S: ShortString): TDateTime;
- begin
- result := StrToDate(@S[1],Length(s),ShortDateFormat,#0);
- end;
- function StrToDate(const S: AnsiString; separator : char): TDateTime;
- begin
- result := StrToDate(@S[1],Length(s),ShortDateFormat,separator)
- end;
- function StrToDate(const S: AnsiString): TDateTime;
- begin
- result := StrToDate(@S[1],Length(s),ShortDateFormat,#0);
- end;
- { StrToTime converts the string S to a TDateTime value
- if S does not represent a valid time value an
- EConvertError will be raised }
- function IntStrToTime(Out ErrorMsg : AnsiString; const S: PChar; Len : integer; separator : char = #0): TDateTime;
- var
- Current: integer; PM: integer;
- function StrPas(Src : PChar; len: integer = 0) : ShortString;
- var
- tmp : integer;
- begin
- {tmp := IndexChar(Src[0], len, #0);
- len :=ifthen(tmp >= 0, tmp, len);
- len :=ifthen(len > 255, 255, len);}
- SetLength(Result, len);
- move(src[0],result[1],len);
- end;
- function GetElement: integer;
- var
- j, c: integer;
- CurrentChar : Char;
- begin
- result := -1;
- while (result = -1) and (Current < Len) do
- begin
- CurrentChar := S[Current];
- if CurrentChar in ['0'..'9'] then
- begin
- j := Current;
- while (Current+1 < Len) and (s[Current + 1] in ['0'..'9']) do
- Inc(Current);
- val(StrPas(S+j, 1 + current - j), result, c);
- end
- else if ((TimeAMString<>'') and (CurrentChar = TimeAMString[1])) or (S[Current] in ['a', 'A']) then
- begin
- pm:=1;
- Current := 1 + Len;
- end
- else if ((TimePMString<>'') and (CurrentChar = TimePMString[1])) or (S[Current] in ['p', 'P']) then
- begin
- Current := 1 + Len;
- PM := 2;
- end
- else if (CurrentChar = Separator) or (CurrentChar = ' ') then
- Inc(Current)
- else
- ErrorMsg:=Format(SErrInvalidTimeFormat,[StrPas(S)]);
- end ;
- end ;
- var
- i: integer;
- TimeValues: array[0..4] of integer;
- begin
- if separator = #0 then
- separator := TimeSeparator;
- Current := 0;
- PM := 0;
- for i:=0 to 4 do
- timevalues[i]:=0;
- i := 0;
- TimeValues[i] := GetElement;
- If ErrorMsg<>'' then
- Exit;
- while (i < 5) and (TimeValues[i] <> -1) do
- begin
- i := i + 1;
- Inc(Current);
- TimeValues[i] := GetElement;
- If ErrorMsg<>'' then
- Exit;
- end ;
- If (i<5) and (TimeValues[I]=-1) then
- TimeValues[I]:=0;
- if PM=2 then
- begin
- if (TimeValues[0] <> 12) then
- Inc(TimeValues[0], 12);
- end
- else
- begin
- if (pm=1) and ((TimeValues[0]=12)) then
- TimeValues[0]:=0;
- end;
- result := EncodeTime(TimeValues[0], TimeValues[1], TimeValues[2], TimeValues[3]);
- end ;
- function StrToTime(const S: PChar; Len : integer; separator : char = #0): TDateTime;
- Var
- Msg : AnsiString;
- begin
- Result:=IntStrToTime(Msg,S,Len,Separator);
- If (Msg<>'') then
- Raise EConvertError.Create(Msg);
- end;
- function StrToTime(const s: ShortString; separator : char): TDateTime;
- begin
- result := StrToTime(@s[1], length(s), separator);
- end;
- function StrToTime(const s: AnsiString; separator : char): TDateTime;
- begin
- result := StrToTime(@s[1], length(s), separator);
- end;
- function StrToTime(const s: ShortString): TDateTime;
- begin
- result := StrToTime(@s[1], length(s), #0);
- end;
- function StrToTime(const s: AnsiString): TDateTime;
- begin
- result := StrToTime(@s[1], length(s), #0);
- end;
- { StrToDateTime converts the string S to a TDateTime value
- if S does not represent a valid date and/or time value
- an EConvertError will be raised }
- function StrToDateTime(const s: string): TDateTime;
- var
- I: integer;
- begin
- I:=Pos(TimeSeparator,S);
- If (I>0) then
- begin
- While (I>0) and (S[I]<>' ') do
- Dec(I);
- If I>0 then
- result:=ComposeDateTime(StrToDate(Copy(S,1,I-1)),StrToTime(Copy(S,i+1, Length(S)-i)))
- else
- result:=StrToTime(S)
- end
- else
- Result:=StrToDate(S);
- end;
- function StrToDateTime(const s: AnsiString; const UseFormat : TFormatSettings): TDateTime;
- var
- I: integer;
- begin
- I:=Pos(TimeSeparator,S);
- If (I>0) then
- begin
- While (I>0) and (S[I]<>' ') do
- Dec(I);
- If I>0 then
- result:=ComposeDateTime(StrToDate(Copy(S,1,I-1),UseFormat.ShortDateFormat,UseFormat.DateSeparator),
- StrToTime(Copy(S,i+1, Length(S)-i),UseFormat.TimeSeparator))
- else
- result:=StrToTime(S,UseFormat.TimeSeparator)
- end
- else
- Result:=StrToDate(S,UseFormat.ShortDateFormat,UseFormat.DateSeparator);
- end;
- function StrToDateTime(const s: ShortString; const UseFormat : TFormatSettings): TDateTime;
- var
- I: integer;
- begin
- I:=Pos(TimeSeparator,S);
- If (I>0) then
- begin
- While (I>0) and (S[I]<>' ') do
- Dec(I);
- If I>0 then
- result:=ComposeDateTime(StrToDate(Copy(S,1,I-1),UseFormat.ShortDateFormat,UseFormat.DateSeparator),
- StrToTime(Copy(S,i+1, Length(S)-i),UseFormat.TimeSeparator))
- else
- result:=StrToTime(S,UseFormat.TimeSeparator)
- end
- else
- Result:=StrToDate(S,UseFormat.ShortDateFormat,UseFormat.DateSeparator);
- end;
- { FormatDateTime formats DateTime to the given format string FormatStr }
- function FormatDateTime(FormatStr: string; DateTime: TDateTime): string;
- var
- ResultLen: integer;
- ResultBuffer: array[0..255] of char;
- ResultCurrent: pchar;
- procedure StoreStr(Str: pchar; Len: integer);
- begin
- if ResultLen + Len < SizeOf(ResultBuffer) then begin
- StrMove(ResultCurrent, Str, Len);
- ResultCurrent := ResultCurrent + Len;
- ResultLen := ResultLen + Len;
- end ;
- end ;
- procedure StoreString(const Str: string);
- var Len: integer;
- begin
- Len := Length(Str);
- if ResultLen + Len < SizeOf(ResultBuffer) then begin // strmove not safe
- StrMove(ResultCurrent, pchar(Str), Len);
- ResultCurrent := ResultCurrent + Len;
- ResultLen := ResultLen + Len;
- end;
- end;
- procedure StoreInt(Value, Digits: integer);
- var S: string; Len: integer;
- begin
- S := IntToStr(Value);
- Len := Length(S);
- if Len < Digits then begin
- S := copy('0000', 1, Digits - Len) + S;
- Len := Digits;
- end ;
- StoreStr(pchar(@S[1]), Len);
- end ;
- var
- Year, Month, Day, DayOfWeek, Hour, Minute, Second, MilliSecond: word;
- procedure StoreFormat(const FormatStr: string);
- var
- Token,lastformattoken: char;
- FormatCurrent: pchar;
- FormatEnd: pchar;
- Count: integer;
- Clock12: boolean;
- P: pchar;
- tmp:integer;
- begin
- FormatCurrent := Pchar(pointer(FormatStr));
- FormatEnd := FormatCurrent + Length(FormatStr);
- Clock12 := false;
- P := FormatCurrent;
- while P < FormatEnd do begin
- Token := UpCase(P^);
- if Token in ['"', ''''] then begin
- P := P + 1;
- while (P < FormatEnd) and (P^ <> Token) do
- P := P + 1;
- end
- else if Token = 'A' then begin
- if (StrLIComp(P, 'A/P', 3) = 0) or
- (StrLIComp(P, 'AMPM', 4) = 0) or
- (StrLIComp(P, 'AM/PM', 5) = 0) then begin
- Clock12 := true;
- break;
- end ;
- end ;
- P := P + 1;
- end ;
- token:=#255;
- lastformattoken:=' ';
- while FormatCurrent < FormatEnd do
- begin
- Token := UpCase(FormatCurrent^);
- Count := 1;
- P := FormatCurrent + 1;
- case Token of
- '''', '"': begin
- while (P < FormatEnd) and (p^ <> Token) do
- P := P + 1;
- P := P + 1;
- Count := P - FormatCurrent;
- StoreStr(FormatCurrent + 1, Count - 2);
- end ;
- 'A': begin
- if StrLIComp(FormatCurrent, 'AMPM', 4) = 0 then begin
- Count := 4;
- if Hour < 12 then StoreString(TimeAMString)
- else StoreString(TimePMString);
- end
- else if StrLIComp(FormatCurrent, 'AM/PM', 5) = 0 then begin
- Count := 5;
- if Hour < 12 then StoreStr('am', 2)
- else StoreStr('pm', 2);
- end
- else if StrLIComp(FormatCurrent, 'A/P', 3) = 0 then begin
- Count := 3;
- if Hour < 12 then StoreStr('a', 1)
- else StoreStr('p', 1);
- end
- else
- Raise EConvertError.Create('Illegal character in format string');
- end ;
- '/': StoreStr(@DateSeparator, 1);
- ':': StoreStr(@TimeSeparator, 1);
- ' ', 'C', 'D', 'H', 'M', 'N', 'S', 'T', 'Y','Z' :
- begin
- while (P < FormatEnd) and (UpCase(P^) = Token) do
- P := P + 1;
- Count := P - FormatCurrent;
- case Token of
- ' ': StoreStr(FormatCurrent, Count);
- 'Y': begin
- if Count>2 then
- StoreInt(Year, 4)
- else
- StoreInt(Year mod 100, 2);
- end;
- 'M': begin
- if lastformattoken='H' then
- begin
- if Count = 1 then
- StoreInt(Minute, 0)
- else
- StoreInt(Minute, 2);
- end
- else
- begin
- case Count of
- 1: StoreInt(Month, 0);
- 2: StoreInt(Month, 2);
- 3: StoreString(ShortMonthNames[Month]);
- 4: StoreString(LongMonthNames[Month]);
- end;
- end;
- end;
- 'D': begin
- case Count of
- 1: StoreInt(Day, 0);
- 2: StoreInt(Day, 2);
- 3: StoreString(ShortDayNames[DayOfWeek]);
- 4: StoreString(LongDayNames[DayOfWeek]);
- 5: StoreFormat(ShortDateFormat);
- 6: StoreFormat(LongDateFormat);
- end ;
- end ;
- 'H': begin
- if Clock12 then begin
- tmp:=hour mod 12;
- if tmp=0 then tmp:=12;
- if Count = 1 then StoreInt(tmp, 0)
- else StoreInt(tmp, 2);
- end
- else begin
- if Count = 1 then StoreInt(Hour, 0)
- else StoreInt(Hour, 2);
- end ;
- end ;
- 'N': begin
- if Count = 1 then StoreInt(Minute, 0)
- else StoreInt(Minute, 2);
- end ;
- 'S': begin
- if Count = 1 then StoreInt(Second, 0)
- else StoreInt(Second, 2);
- end ;
- 'Z': begin
- if Count = 1 then StoreInt(MilliSecond, 0)
- else StoreInt(MilliSecond, 3);
- end ;
- 'T': begin
- if Count = 1 then StoreFormat(ShortTimeFormat)
- else StoreFormat(LongTimeFormat);
- end ;
- 'C':
- begin
- StoreFormat(ShortDateFormat);
- if (Hour<>0) or (Minute<>0) or (Second<>0) then
- begin
- StoreString(' ');
- StoreFormat(LongTimeFormat);
- end;
- end;
- end;
- lastformattoken:=token;
- end;
- else
- StoreStr(@Token, 1);
- end ;
- FormatCurrent := FormatCurrent + Count;
- end ;
- end ;
- begin
- DecodeDateFully(DateTime, Year, Month, Day, DayOfWeek);
- DecodeTime(DateTime, Hour, Minute, Second, MilliSecond);
- ResultLen := 0;
- ResultCurrent := @ResultBuffer[0];
- StoreFormat(FormatStr);
- ResultBuffer[ResultLen] := #0;
- result := StrPas(@ResultBuffer[0]);
- end ;
- { DateTimeToString formats DateTime to the given format in FormatStr }
- procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime);
- begin
- Result := FormatDateTime(FormatStr, DateTime);
- end ;
- Function DateTimeToFileDate(DateTime : TDateTime) : Longint;
- Var YY,MM,DD,H,m,s,msec : Word;
- begin
- Decodedate (DateTime,YY,MM,DD);
- DecodeTime (DateTime,h,m,s,msec);
- {$ifndef unix}
- If (YY<1980) or (YY>2099) then
- Result:=0
- else
- begin
- Result:=(s shr 1) or (m shl 5) or (h shl 11);
- Result:=Result or longint(DD shl 16 or (MM shl 21) or (word(YY-1980) shl 25));
- end;
- {$else unix}
- Result:=LocalToEpoch(yy,mm,dd,h,m,s);
- {$endif unix}
- end;
- function CurrentYear:Word;
- var yy,mm,dd : word;
- begin
- Decodedate(now,yy,mm,dd);
- Result:=yy;
- end;
- Function FileDateToDateTime (Filedate : Longint) : TDateTime;
- {$ifndef unix}
- Var Date,Time : Word;
- begin
- Date:=FileDate shr 16;
- Time:=FileDate and $ffff;
- Result:=ComposeDateTime(EncodeDate((Date shr 9) + 1980,(Date shr 5) and 15, Date and 31),
- EncodeTime(Time shr 11, (Time shr 5) and 63, (Time and 31) shl 1,0));
- end;
- {$else unix}
- var
- y, mon, d, h, min, s: word;
- begin
- EpochToLocal(FileDate,y,mon,d,h,min,s);
- Result:=ComposeDateTime(EncodeDate(y,mon,d),EncodeTime(h,min,s,0));
- end;
- {$endif unix}
- function TryStrToDate(const S: ShortString; out Value: TDateTime): Boolean;
- begin
- result := TryStrToDate(S, Value, #0);
- end;
- function TryStrToDate(const S: ShortString; out Value: TDateTime;
- const useformat : string; separator : char = #0): Boolean;
-
- Var
- Msg : Ansistring;
-
- begin
- Value:=IntStrToDate(Msg,@S[1],Length(S),useformat,separator);
- Result:=(Msg='');
- end;
- function TryStrToDate(const S: AnsiString; out Value: TDateTime;
- const useformat : string; separator : char = #0): Boolean;
-
- Var
- Msg : Ansistring;
-
- begin
- Result:=Length(S)<>0;
- If Result then
- begin
- Value:=IntStrToDate(Msg,@S[1],Length(S),useformat);
- Result:=(Msg='');
- end;
- end;
- function TryStrToDate(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
- begin
- Result:=TryStrToDate(S,Value,ShortDateFormat,Separator);
- end;
- function TryStrToDate(const S: AnsiString; out Value: TDateTime): Boolean;
- begin
- Result:=TryStrToDate(S,Value,ShortDateFormat,#0);
- end;
- function TryStrToDate(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
- begin
- Result:=TryStrToDate(S,Value,ShortDateFormat,Separator);
- end;
- // function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
- function TryStrToTime(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
- Var
- Msg : AnsiString;
- begin
- Value:=IntStrToTime(Msg,@S[1],Length(S),Separator);
- result:=(Msg='');
- end;
- function TryStrToTime(const S: ShortString; out Value: TDateTime): Boolean;
- begin
- Result := TryStrToTime(S,Value,#0);
- end;
- function TryStrToTime(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
- Var
- Msg : AnsiString;
- begin
- Result:=Length(S)<>0;
- If Result then
- begin
- Value:=IntStrToTime(Msg,@S[1],Length(S),Separator);
- Result:=(Msg='');
- end;
- end;
- function TryStrToTime(const S: AnsiString; out Value: TDateTime): Boolean;
- begin
- result := TryStrToTime(S,Value,#0);
- end;
- // function TryStrToTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
- function TryStrToDateTime(const S: ShortString; out Value: TDateTime): Boolean;
- begin
- result:=true;
- try
- value:=StrToDateTime(s);
- except
- on EConvertError do
- result:=false
- end;
- end;
- function TryStrToDateTime(const S: AnsiString; out Value: TDateTime): Boolean;
- begin
- result:=true;
- try
- value:=StrToDateTime(s);
- except
- on EConvertError do
- result:=false
- end;
- end;
- // function TryStrToDateTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
- function StrToDateDef(const S: ShortString; const Defvalue : TDateTime): TDateTime;
- begin
- result := StrToDateDef(S,DefValue,#0);
- end;
- function StrToTimeDef(const S: ShortString; const Defvalue : TDateTime): TDateTime;
- begin
- result := StrToTimeDef(S,DefValue,#0);
- end;
- function StrToDateTimeDef(const S: ShortString; const Defvalue : TDateTime): TDateTime;
- begin
- if not TryStrToDateTime(s,Result) Then
- result:=defvalue;
- end;
- function StrToDateDef(const S: ShortString; const Defvalue : TDateTime; separator : char): TDateTime;
- begin
- if not TryStrToDate(s,Result, separator) Then
- result:=defvalue;
- end;
- function StrToTimeDef(const S: ShortString; const Defvalue : TDateTime; separator : char): TDateTime;
- begin
- if not TryStrToTime(s,Result, separator) Then
- result:=defvalue;
- end;
- function StrToDateDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime;
- begin
- result := StrToDateDef(S,DefValue,#0);
- end;
- function StrToTimeDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime;
- begin
- result := StrToTimeDef(S,DefValue,#0);
- end;
- function StrToDateTimeDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime;
- begin
- if not TryStrToDateTime(s,Result) Then
- result:=defvalue;
- end;
- function StrToDateDef(const S: AnsiString; const Defvalue : TDateTime; separator : char): TDateTime;
- begin
- if not TryStrToDate(s,Result, separator) Then
- result:=defvalue;
- end;
- function StrToTimeDef(const S: AnsiString; const Defvalue : TDateTime; separator : char): TDateTime;
- begin
- if not TryStrToTime(s,Result, separator) Then
- result:=defvalue;
- end;
- procedure ReplaceTime(var dati:TDateTime; NewTime : TDateTime);inline;
- begin
- dati:= ComposeDateTime(dati, newtime);
- end;
- procedure ReplaceDate(var DateTime: TDateTime; const NewDate: TDateTime); inline;
- var
- tmp : TDateTime;
- begin
- tmp:=NewDate;
- ReplaceTime(tmp,DateTime);
- DateTime:=tmp;
- end;
|