dati.inc 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136
  1. {
  2. *********************************************************************
  3. Copyright (C) 1997, 1998 Gertjan Schouten
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. *********************************************************************
  16. System Utilities For Free Pascal
  17. }
  18. {==============================================================================}
  19. { internal functions }
  20. {==============================================================================}
  21. Function DoEncodeDate(Year, Month, Day: Word): longint;
  22. Var
  23. D : TDateTime;
  24. begin
  25. If TryEncodeDate(Year,Month,Day,D) then
  26. Result:=Trunc(D)
  27. else
  28. Result:=0;
  29. end;
  30. function DoEncodeTime(Hour, Minute, Second, MilliSecond: word): TDateTime;
  31. begin
  32. If not TryEncodeTime(Hour,Minute,Second,MilliSecond,Result) then
  33. Result:=0;
  34. end;
  35. {==============================================================================}
  36. { Public functions }
  37. {==============================================================================}
  38. { ComposeDateTime converts a Date and a Time into one TDateTime }
  39. function ComposeDateTime(Date,Time : TDateTime) : TDateTime;
  40. begin
  41. if Date < 0 then Result := trunc(Date) - Abs(frac(Time))
  42. else Result := trunc(Date) + Abs(frac(Time));
  43. end;
  44. { DateTimeToTimeStamp converts DateTime to a TTimeStamp }
  45. function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
  46. begin
  47. result.Time := Round(abs(Frac(DateTime)) * MSecsPerDay);
  48. result.Date := DateDelta + trunc(DateTime);
  49. end ;
  50. { TimeStampToDateTime converts TimeStamp to a TDateTime value }
  51. function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;
  52. begin
  53. Result := ComposeDateTime(TimeStamp.Date - DateDelta,TimeStamp.Time / MSecsPerDay)
  54. end;
  55. { MSecsToTimeStamp }
  56. function MSecsToTimeStamp(MSecs: comp): TTimeStamp;
  57. begin
  58. result.Date := Trunc(msecs / msecsperday);
  59. msecs:= msecs-comp(result.date)*msecsperday;
  60. result.Time := Round(MSecs);
  61. end ;
  62. { TimeStampToMSecs }
  63. function TimeStampToMSecs(const TimeStamp: TTimeStamp): comp;
  64. begin
  65. result := TimeStamp.Time + comp(timestamp.date)*msecsperday;
  66. end ;
  67. Function TryEncodeDate(Year,Month,Day : Word; Out Date : TDateTime) : Boolean;
  68. var
  69. c, ya: cardinal;
  70. begin
  71. Result:=(Year>0) and (Year<10000) and
  72. (Month in [1..12]) and
  73. (Day>0) and (Day<=MonthDays[IsleapYear(Year),Month]);
  74. If Result then
  75. begin
  76. if month > 2 then
  77. Dec(Month,3)
  78. else
  79. begin
  80. Inc(Month,9);
  81. Dec(Year);
  82. end;
  83. c:= Year DIV 100;
  84. ya:= Year - 100*c;
  85. Date := (146097*c) SHR 2 + (1461*ya) SHR 2 + (153*cardinal(Month)+2) DIV 5 + cardinal(Day);
  86. // Note that this line can't be part of the line above, since TDateTime is
  87. // signed and c and ya are not
  88. Date := Date - 693900;
  89. end
  90. end;
  91. function TryEncodeTime(Hour, Min, Sec, MSec:word; Out Time : TDateTime) : boolean;
  92. begin
  93. Result:=(Hour<24) and (Min<60) and (Sec<60) and (MSec<1000);
  94. If Result then
  95. Time:=TDateTime(cardinal(Hour)*3600000+cardinal(Min)*60000+cardinal(Sec)*1000+MSec)/MSecsPerDay;
  96. end;
  97. { EncodeDate packs three variables Year, Month and Day into a
  98. TDateTime value the result is the number of days since 12/30/1899 }
  99. function EncodeDate(Year, Month, Day: word): TDateTime;
  100. begin
  101. If Not TryEncodeDate(Year,Month,Day,Result) then
  102. Raise EConvertError.CreateFmt('%d-%d-%d is not a valid date specification',
  103. [Year,Month,Day]);
  104. end;
  105. { EncodeTime packs four variables Hour, Minute, Second and MilliSecond into
  106. a TDateTime value }
  107. function EncodeTime(Hour, Minute, Second, MilliSecond:word):TDateTime;
  108. begin
  109. If not TryEncodeTime(Hour,Minute,Second,MilliSecond,Result) then
  110. Raise EConvertError.CreateFmt('%d:%d:%d.%d is not a valid time specification',
  111. [Hour,Minute,Second,MilliSecond]);
  112. end;
  113. { DecodeDate unpacks the value Date into three values:
  114. Year, Month and Day }
  115. procedure DecodeDate(Date: TDateTime; out Year, Month, Day: word);
  116. var
  117. ly,ld,lm,j : cardinal;
  118. begin
  119. if Date <= -datedelta then // If Date is before 1-1-1 then return 0-0-0
  120. begin
  121. Year := 0;
  122. Month := 0;
  123. Day := 0;
  124. end
  125. else
  126. begin
  127. j := pred((Trunc(System.Int(Date)) + 693900) SHL 2);
  128. ly:= j DIV 146097;
  129. j:= j - 146097 * cardinal(ly);
  130. ld := j SHR 2;
  131. j:=(ld SHL 2 + 3) DIV 1461;
  132. ld:= (cardinal(ld) SHL 2 + 7 - 1461*j) SHR 2;
  133. lm:=(5 * ld-3) DIV 153;
  134. ld:= (5 * ld +2 - 153*lm) DIV 5;
  135. ly:= 100 * cardinal(ly) + j;
  136. if lm < 10 then
  137. inc(lm,3)
  138. else
  139. begin
  140. dec(lm,9);
  141. inc(ly);
  142. end;
  143. year:=ly;
  144. month:=lm;
  145. day:=ld;
  146. end;
  147. end;
  148. function DecodeDateFully(const DateTime: TDateTime; out Year, Month, Day, DOW: Word): Boolean;
  149. begin
  150. DecodeDate(DateTime,Year,Month,Day);
  151. DOW:=DayOfWeek(DateTime);
  152. Result:=IsLeapYear(Year);
  153. end;
  154. { DecodeTime unpacks Time into four values:
  155. Hour, Minute, Second and MilliSecond }
  156. procedure DecodeTime(Time: TDateTime; out Hour, Minute, Second, MilliSecond: word);
  157. Var
  158. l : cardinal;
  159. begin
  160. l := Round(abs(Frac(time)) * MSecsPerDay);
  161. Hour := l div 3600000;
  162. l := l mod 3600000;
  163. Minute := l div 60000;
  164. l := l mod 60000;
  165. Second := l div 1000;
  166. l := l mod 1000;
  167. MilliSecond := l;
  168. end;
  169. { DateTimeToSystemTime converts DateTime value to SystemTime }
  170. procedure DateTimeToSystemTime(DateTime: TDateTime; out SystemTime: TSystemTime);
  171. begin
  172. DecodeDate(DateTime, SystemTime.Year, SystemTime.Month, SystemTime.Day);
  173. DecodeTime(DateTime, SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond);
  174. end ;
  175. { SystemTimeToDateTime converts SystemTime to a TDateTime value }
  176. function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;
  177. begin
  178. result := ComposeDateTime(DoEncodeDate(SystemTime.Year, SystemTime.Month, SystemTime.Day),
  179. DoEncodeTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond));
  180. end ;
  181. { DayOfWeek returns the Day of the week (sunday is day 1) }
  182. function DayOfWeek(DateTime: TDateTime): integer;
  183. begin
  184. Result := 1 + (Abs(Trunc(DateTime) - 1) mod 7);
  185. end ;
  186. { Date returns the current Date }
  187. function Date: TDateTime;
  188. var
  189. SystemTime: TSystemTime;
  190. begin
  191. GetLocalTime(SystemTime);
  192. result := DoEncodeDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  193. end ;
  194. { Time returns the current Time }
  195. function Time: TDateTime;
  196. var
  197. SystemTime: TSystemTime;
  198. begin
  199. GetLocalTime(SystemTime);
  200. Result := DoEncodeTime(SystemTime.Hour,SystemTime.Minute,SystemTime.Second,SystemTime.MilliSecond);
  201. end ;
  202. { Now returns the current Date and Time }
  203. function Now: TDateTime;
  204. var
  205. SystemTime: TSystemTime;
  206. begin
  207. GetLocalTime(SystemTime);
  208. result := systemTimeToDateTime(SystemTime);
  209. end;
  210. { IncMonth increments DateTime with NumberOfMonths months,
  211. NumberOfMonths can be less than zero }
  212. function IncMonth(const DateTime: TDateTime; NumberOfMonths: integer = 1 ): TDateTime;
  213. var
  214. Year, Month, Day : word;
  215. begin
  216. DecodeDate(DateTime, Year, Month, Day);
  217. IncAMonth(Year, Month, Day, NumberOfMonths);
  218. result := ComposeDateTime(DoEncodeDate(Year, Month, Day), DateTime);
  219. end ;
  220. { IncAMonth is the same as IncMonth, but operates on decoded date }
  221. procedure IncAMonth(var Year, Month, Day: Word; NumberOfMonths: Integer = 1);
  222. var
  223. TempMonth, S: Integer;
  224. begin
  225. If NumberOfMonths>=0 then
  226. s:=1
  227. else
  228. s:=-1;
  229. inc(Year,(NumberOfMonths div 12));
  230. TempMonth:=Month+(NumberOfMonths mod 12)-1;
  231. if (TempMonth>11) or
  232. (TempMonth<0) then
  233. begin
  234. Dec(TempMonth, S*12);
  235. Inc(Year, S);
  236. end;
  237. Month:=TempMonth+1; { Months from 1 to 12 }
  238. If (Day>MonthDays[IsLeapYear(Year)][Month]) then
  239. Day:=MonthDays[IsLeapYear(Year)][Month];
  240. end;
  241. { IsLeapYear returns true if Year is a leap year }
  242. function IsLeapYear(Year: Word): boolean;
  243. begin
  244. Result := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
  245. end;
  246. { DateToStr returns a string representation of Date using ShortDateFormat }
  247. function DateToStr(Date: TDateTime): string;
  248. begin
  249. result := FormatDateTime('ddddd', Date);
  250. end ;
  251. { TimeToStr returns a string representation of Time using LongTimeFormat }
  252. function TimeToStr(Time: TDateTime): string;
  253. begin
  254. result := FormatDateTime('tt', Time);
  255. end ;
  256. { DateTimeToStr returns a string representation of DateTime using LongDateTimeFormat }
  257. function DateTimeToStr(DateTime: TDateTime): string;
  258. begin
  259. result := FormatDateTime('c', DateTime);
  260. end ;
  261. { StrToDate converts the string S to a TDateTime value
  262. if S does not represent a valid date value
  263. an EConvertError will be raised }
  264. function IntStrToDate(Out ErrorMsg : AnsiString; const S: PChar; Len : integer; const useformat : string; separator : char = #0): TDateTime;
  265. const SInvalidDateFormat = '"%s" is not a valid date format';
  266. var
  267. df:string;
  268. d,m,y,ly:word;
  269. n,i:longint;
  270. c:word;
  271. dp,mp,yp,which : Byte;
  272. s1:string[4];
  273. values:array[1..3] of longint;
  274. LocalTime:tsystemtime;
  275. YearMoreThenTwoDigits : boolean;
  276. begin
  277. ErrorMsg:='';
  278. if (Len=0) then
  279. begin
  280. ErrorMsg:=Format(SInvalidDateFormat,['']);
  281. exit;
  282. end;
  283. YearMoreThenTwoDigits := False;
  284. if separator = #0 then
  285. separator := DateSeparator;
  286. df := UpperCase(useFormat);
  287. { Determine order of D,M,Y }
  288. yp:=0;
  289. mp:=0;
  290. dp:=0;
  291. Which:=0;
  292. i:=0;
  293. while (i<Length(df)) and (Which<3) do
  294. begin
  295. inc(i);
  296. Case df[i] of
  297. 'Y' :
  298. if yp=0 then
  299. begin
  300. Inc(Which);
  301. yp:=which;
  302. end;
  303. 'M' :
  304. if mp=0 then
  305. begin
  306. Inc(Which);
  307. mp:=which;
  308. end;
  309. 'D' :
  310. if dp=0 then
  311. begin
  312. Inc(Which);
  313. dp:=which;
  314. end;
  315. end;
  316. end;
  317. if Which<>3 then
  318. begin
  319. ErrorMsg:=Format(SErrIllegalDateFormatString,[useformat]);
  320. Exit;
  321. end;
  322. { Get actual values }
  323. for i := 1 to 3 do
  324. values[i] := 0;
  325. s1 := '';
  326. n := 0;
  327. dec(len);
  328. for i := 0 to len do
  329. begin
  330. if s[i] in ['0'..'9'] then
  331. s1 := s1 + s[i];
  332. { space can be part of the shortdateformat, and is defaultly in slovak
  333. windows, therefor it shouldn't be taken as separator (unless so specified)
  334. and ignored }
  335. if (Separator <> ' ') and (s[i] = ' ') then
  336. Continue;
  337. if (s[i] = separator) or ((i = len) and (s[i] in ['0'..'9'])) then
  338. begin
  339. inc(n);
  340. if n>3 then
  341. begin
  342. ErrorMsg:=Format(SInvalidDateFormat,[s]);
  343. exit;
  344. end;
  345. // Check if the year has more then two digits (if n=yp, then we are evaluating the year.)
  346. if (n=yp) and (length(s1)>2) then YearMoreThenTwoDigits := True;
  347. val(s1, values[n], c);
  348. if c<>0 then
  349. begin
  350. ErrorMsg:=Format(SInvalidDateFormat,[s]);
  351. Exit;
  352. end;
  353. s1 := '';
  354. end
  355. else if not (s[i] in ['0'..'9']) then
  356. begin
  357. ErrorMsg:=Format(SInvalidDateFormat,[s]);
  358. Exit;
  359. end;
  360. end ;
  361. // Fill in values.
  362. getLocalTime(LocalTime);
  363. ly := LocalTime.Year;
  364. If N=3 then
  365. begin
  366. y:=values[yp];
  367. m:=values[mp];
  368. d:=values[dp];
  369. end
  370. Else
  371. begin
  372. Y:=ly;
  373. If n<2 then
  374. begin
  375. d:=values[1];
  376. m := LocalTime.Month;
  377. end
  378. else
  379. If dp<mp then
  380. begin
  381. d:=values[1];
  382. m:=values[2];
  383. end
  384. else
  385. begin
  386. d:=values[2];
  387. m:=values[1];
  388. end;
  389. end;
  390. if (y >= 0) and (y < 100) and not YearMoreThenTwoDigits then
  391. begin
  392. ly := ly - TwoDigitYearCenturyWindow;
  393. Inc(Y, ly div 100 * 100);
  394. if (TwoDigitYearCenturyWindow > 0) and (Y < ly) then
  395. Inc(Y, 100);
  396. end;
  397. Result := EncodeDate(y, m, d);
  398. end ;
  399. function StrToDate(const S: PChar; Len : integer; const useformat : string; separator : char = #0): TDateTime;
  400. Var
  401. MSg : AnsiString;
  402. begin
  403. Result:=IntStrToDate(Msg,S,Len,useFormat,Separator);
  404. If (Msg<>'') then
  405. Raise EConvertError.Create(Msg);
  406. end;
  407. function StrToDate(const S: ShortString; const useformat : string; separator : char = #0): TDateTime;
  408. begin
  409. result := StrToDate(@S[1],Length(s),UseFormat,separator);
  410. end;
  411. function StrToDate(const S: AnsiString; const useformat : string; separator : char = #0): TDateTime;
  412. begin
  413. result := StrToDate(@S[1],Length(s),UseFormat,separator);
  414. end;
  415. function StrToDate(const S: ShortString; separator : char): TDateTime;
  416. begin
  417. result := StrToDate(@S[1],Length(s),ShortDateFormat,separator)
  418. end;
  419. function StrToDate(const S: ShortString): TDateTime;
  420. begin
  421. result := StrToDate(@S[1],Length(s),ShortDateFormat,#0);
  422. end;
  423. function StrToDate(const S: AnsiString; separator : char): TDateTime;
  424. begin
  425. result := StrToDate(@S[1],Length(s),ShortDateFormat,separator)
  426. end;
  427. function StrToDate(const S: AnsiString): TDateTime;
  428. begin
  429. result := StrToDate(@S[1],Length(s),ShortDateFormat,#0);
  430. end;
  431. { StrToTime converts the string S to a TDateTime value
  432. if S does not represent a valid time value an
  433. EConvertError will be raised }
  434. function IntStrToTime(Out ErrorMsg : AnsiString; const S: PChar; Len : integer; separator : char = #0): TDateTime;
  435. var
  436. Current: integer; PM: integer;
  437. function StrPas(Src : PChar; len: integer = 0) : ShortString;
  438. var
  439. tmp : integer;
  440. begin
  441. {tmp := IndexChar(Src[0], len, #0);
  442. len :=ifthen(tmp >= 0, tmp, len);
  443. len :=ifthen(len > 255, 255, len);}
  444. SetLength(Result, len);
  445. move(src[0],result[1],len);
  446. end;
  447. function GetElement: integer;
  448. var
  449. j, c: integer;
  450. CurrentChar : Char;
  451. begin
  452. result := -1;
  453. while (result = -1) and (Current < Len) do
  454. begin
  455. CurrentChar := S[Current];
  456. if CurrentChar in ['0'..'9'] then
  457. begin
  458. j := Current;
  459. while (Current+1 < Len) and (s[Current + 1] in ['0'..'9']) do
  460. Inc(Current);
  461. val(StrPas(S+j, 1 + current - j), result, c);
  462. end
  463. else if ((TimeAMString<>'') and (CurrentChar = TimeAMString[1])) or (S[Current] in ['a', 'A']) then
  464. begin
  465. pm:=1;
  466. Current := 1 + Len;
  467. end
  468. else if ((TimePMString<>'') and (CurrentChar = TimePMString[1])) or (S[Current] in ['p', 'P']) then
  469. begin
  470. Current := 1 + Len;
  471. PM := 2;
  472. end
  473. else if (CurrentChar = Separator) or (CurrentChar = ' ') then
  474. Inc(Current)
  475. else
  476. ErrorMsg:=Format(SErrInvalidTimeFormat,[StrPas(S)]);
  477. end ;
  478. end ;
  479. var
  480. i: integer;
  481. TimeValues: array[0..4] of integer;
  482. begin
  483. if separator = #0 then
  484. separator := TimeSeparator;
  485. Current := 0;
  486. PM := 0;
  487. for i:=0 to 4 do
  488. timevalues[i]:=0;
  489. i := 0;
  490. TimeValues[i] := GetElement;
  491. If ErrorMsg<>'' then
  492. Exit;
  493. while (i < 5) and (TimeValues[i] <> -1) do
  494. begin
  495. i := i + 1;
  496. Inc(Current);
  497. TimeValues[i] := GetElement;
  498. If ErrorMsg<>'' then
  499. Exit;
  500. end ;
  501. If (i<5) and (TimeValues[I]=-1) then
  502. TimeValues[I]:=0;
  503. if PM=2 then
  504. begin
  505. if (TimeValues[0] <> 12) then
  506. Inc(TimeValues[0], 12);
  507. end
  508. else
  509. begin
  510. if (pm=1) and ((TimeValues[0]=12)) then
  511. TimeValues[0]:=0;
  512. end;
  513. result := EncodeTime(TimeValues[0], TimeValues[1], TimeValues[2], TimeValues[3]);
  514. end ;
  515. function StrToTime(const S: PChar; Len : integer; separator : char = #0): TDateTime;
  516. Var
  517. Msg : AnsiString;
  518. begin
  519. Result:=IntStrToTime(Msg,S,Len,Separator);
  520. If (Msg<>'') then
  521. Raise EConvertError.Create(Msg);
  522. end;
  523. function StrToTime(const s: ShortString; separator : char): TDateTime;
  524. begin
  525. result := StrToTime(@s[1], length(s), separator);
  526. end;
  527. function StrToTime(const s: AnsiString; separator : char): TDateTime;
  528. begin
  529. result := StrToTime(@s[1], length(s), separator);
  530. end;
  531. function StrToTime(const s: ShortString): TDateTime;
  532. begin
  533. result := StrToTime(@s[1], length(s), #0);
  534. end;
  535. function StrToTime(const s: AnsiString): TDateTime;
  536. begin
  537. result := StrToTime(@s[1], length(s), #0);
  538. end;
  539. { StrToDateTime converts the string S to a TDateTime value
  540. if S does not represent a valid date and/or time value
  541. an EConvertError will be raised }
  542. function StrToDateTime(const s: string): TDateTime;
  543. var
  544. I: integer;
  545. begin
  546. I:=Pos(TimeSeparator,S);
  547. If (I>0) then
  548. begin
  549. While (I>0) and (S[I]<>' ') do
  550. Dec(I);
  551. If I>0 then
  552. result:=ComposeDateTime(StrToDate(Copy(S,1,I-1)),StrToTime(Copy(S,i+1, Length(S)-i)))
  553. else
  554. result:=StrToTime(S)
  555. end
  556. else
  557. Result:=StrToDate(S);
  558. end;
  559. function StrToDateTime(const s: AnsiString; const UseFormat : TFormatSettings): TDateTime;
  560. var
  561. I: integer;
  562. begin
  563. I:=Pos(TimeSeparator,S);
  564. If (I>0) then
  565. begin
  566. While (I>0) and (S[I]<>' ') do
  567. Dec(I);
  568. If I>0 then
  569. result:=ComposeDateTime(StrToDate(Copy(S,1,I-1),UseFormat.ShortDateFormat,UseFormat.DateSeparator),
  570. StrToTime(Copy(S,i+1, Length(S)-i),UseFormat.TimeSeparator))
  571. else
  572. result:=StrToTime(S,UseFormat.TimeSeparator)
  573. end
  574. else
  575. Result:=StrToDate(S,UseFormat.ShortDateFormat,UseFormat.DateSeparator);
  576. end;
  577. function StrToDateTime(const s: ShortString; const UseFormat : TFormatSettings): TDateTime;
  578. var
  579. I: integer;
  580. begin
  581. I:=Pos(TimeSeparator,S);
  582. If (I>0) then
  583. begin
  584. While (I>0) and (S[I]<>' ') do
  585. Dec(I);
  586. If I>0 then
  587. result:=ComposeDateTime(StrToDate(Copy(S,1,I-1),UseFormat.ShortDateFormat,UseFormat.DateSeparator),
  588. StrToTime(Copy(S,i+1, Length(S)-i),UseFormat.TimeSeparator))
  589. else
  590. result:=StrToTime(S,UseFormat.TimeSeparator)
  591. end
  592. else
  593. Result:=StrToDate(S,UseFormat.ShortDateFormat,UseFormat.DateSeparator);
  594. end;
  595. { FormatDateTime formats DateTime to the given format string FormatStr }
  596. function FormatDateTime(FormatStr: string; DateTime: TDateTime): string;
  597. var
  598. ResultLen: integer;
  599. ResultBuffer: array[0..255] of char;
  600. ResultCurrent: pchar;
  601. procedure StoreStr(Str: pchar; Len: integer);
  602. begin
  603. if ResultLen + Len < SizeOf(ResultBuffer) then begin
  604. StrMove(ResultCurrent, Str, Len);
  605. ResultCurrent := ResultCurrent + Len;
  606. ResultLen := ResultLen + Len;
  607. end ;
  608. end ;
  609. procedure StoreString(const Str: string);
  610. var Len: integer;
  611. begin
  612. Len := Length(Str);
  613. if ResultLen + Len < SizeOf(ResultBuffer) then begin // strmove not safe
  614. StrMove(ResultCurrent, pchar(Str), Len);
  615. ResultCurrent := ResultCurrent + Len;
  616. ResultLen := ResultLen + Len;
  617. end;
  618. end;
  619. procedure StoreInt(Value, Digits: integer);
  620. var S: string; Len: integer;
  621. begin
  622. S := IntToStr(Value);
  623. Len := Length(S);
  624. if Len < Digits then begin
  625. S := copy('0000', 1, Digits - Len) + S;
  626. Len := Digits;
  627. end ;
  628. StoreStr(pchar(@S[1]), Len);
  629. end ;
  630. var
  631. Year, Month, Day, DayOfWeek, Hour, Minute, Second, MilliSecond: word;
  632. procedure StoreFormat(const FormatStr: string);
  633. var
  634. Token,lastformattoken: char;
  635. FormatCurrent: pchar;
  636. FormatEnd: pchar;
  637. Count: integer;
  638. Clock12: boolean;
  639. P: pchar;
  640. tmp:integer;
  641. begin
  642. FormatCurrent := Pchar(pointer(FormatStr));
  643. FormatEnd := FormatCurrent + Length(FormatStr);
  644. Clock12 := false;
  645. P := FormatCurrent;
  646. while P < FormatEnd do begin
  647. Token := UpCase(P^);
  648. if Token in ['"', ''''] then begin
  649. P := P + 1;
  650. while (P < FormatEnd) and (P^ <> Token) do
  651. P := P + 1;
  652. end
  653. else if Token = 'A' then begin
  654. if (StrLIComp(P, 'A/P', 3) = 0) or
  655. (StrLIComp(P, 'AMPM', 4) = 0) or
  656. (StrLIComp(P, 'AM/PM', 5) = 0) then begin
  657. Clock12 := true;
  658. break;
  659. end ;
  660. end ;
  661. P := P + 1;
  662. end ;
  663. token:=#255;
  664. lastformattoken:=' ';
  665. while FormatCurrent < FormatEnd do
  666. begin
  667. Token := UpCase(FormatCurrent^);
  668. Count := 1;
  669. P := FormatCurrent + 1;
  670. case Token of
  671. '''', '"': begin
  672. while (P < FormatEnd) and (p^ <> Token) do
  673. P := P + 1;
  674. P := P + 1;
  675. Count := P - FormatCurrent;
  676. StoreStr(FormatCurrent + 1, Count - 2);
  677. end ;
  678. 'A': begin
  679. if StrLIComp(FormatCurrent, 'AMPM', 4) = 0 then begin
  680. Count := 4;
  681. if Hour < 12 then StoreString(TimeAMString)
  682. else StoreString(TimePMString);
  683. end
  684. else if StrLIComp(FormatCurrent, 'AM/PM', 5) = 0 then begin
  685. Count := 5;
  686. if Hour < 12 then StoreStr('am', 2)
  687. else StoreStr('pm', 2);
  688. end
  689. else if StrLIComp(FormatCurrent, 'A/P', 3) = 0 then begin
  690. Count := 3;
  691. if Hour < 12 then StoreStr('a', 1)
  692. else StoreStr('p', 1);
  693. end
  694. else
  695. Raise EConvertError.Create('Illegal character in format string');
  696. end ;
  697. '/': StoreStr(@DateSeparator, 1);
  698. ':': StoreStr(@TimeSeparator, 1);
  699. ' ', 'C', 'D', 'H', 'M', 'N', 'S', 'T', 'Y','Z' :
  700. begin
  701. while (P < FormatEnd) and (UpCase(P^) = Token) do
  702. P := P + 1;
  703. Count := P - FormatCurrent;
  704. case Token of
  705. ' ': StoreStr(FormatCurrent, Count);
  706. 'Y': begin
  707. if Count>2 then
  708. StoreInt(Year, 4)
  709. else
  710. StoreInt(Year mod 100, 2);
  711. end;
  712. 'M': begin
  713. if lastformattoken='H' then
  714. begin
  715. if Count = 1 then
  716. StoreInt(Minute, 0)
  717. else
  718. StoreInt(Minute, 2);
  719. end
  720. else
  721. begin
  722. case Count of
  723. 1: StoreInt(Month, 0);
  724. 2: StoreInt(Month, 2);
  725. 3: StoreString(ShortMonthNames[Month]);
  726. 4: StoreString(LongMonthNames[Month]);
  727. end;
  728. end;
  729. end;
  730. 'D': begin
  731. case Count of
  732. 1: StoreInt(Day, 0);
  733. 2: StoreInt(Day, 2);
  734. 3: StoreString(ShortDayNames[DayOfWeek]);
  735. 4: StoreString(LongDayNames[DayOfWeek]);
  736. 5: StoreFormat(ShortDateFormat);
  737. 6: StoreFormat(LongDateFormat);
  738. end ;
  739. end ;
  740. 'H': begin
  741. if Clock12 then begin
  742. tmp:=hour mod 12;
  743. if tmp=0 then tmp:=12;
  744. if Count = 1 then StoreInt(tmp, 0)
  745. else StoreInt(tmp, 2);
  746. end
  747. else begin
  748. if Count = 1 then StoreInt(Hour, 0)
  749. else StoreInt(Hour, 2);
  750. end ;
  751. end ;
  752. 'N': begin
  753. if Count = 1 then StoreInt(Minute, 0)
  754. else StoreInt(Minute, 2);
  755. end ;
  756. 'S': begin
  757. if Count = 1 then StoreInt(Second, 0)
  758. else StoreInt(Second, 2);
  759. end ;
  760. 'Z': begin
  761. if Count = 1 then StoreInt(MilliSecond, 0)
  762. else StoreInt(MilliSecond, 3);
  763. end ;
  764. 'T': begin
  765. if Count = 1 then StoreFormat(ShortTimeFormat)
  766. else StoreFormat(LongTimeFormat);
  767. end ;
  768. 'C':
  769. begin
  770. StoreFormat(ShortDateFormat);
  771. if (Hour<>0) or (Minute<>0) or (Second<>0) then
  772. begin
  773. StoreString(' ');
  774. StoreFormat(LongTimeFormat);
  775. end;
  776. end;
  777. end;
  778. lastformattoken:=token;
  779. end;
  780. else
  781. StoreStr(@Token, 1);
  782. end ;
  783. FormatCurrent := FormatCurrent + Count;
  784. end ;
  785. end ;
  786. begin
  787. DecodeDateFully(DateTime, Year, Month, Day, DayOfWeek);
  788. DecodeTime(DateTime, Hour, Minute, Second, MilliSecond);
  789. ResultLen := 0;
  790. ResultCurrent := @ResultBuffer[0];
  791. StoreFormat(FormatStr);
  792. ResultBuffer[ResultLen] := #0;
  793. result := StrPas(@ResultBuffer[0]);
  794. end ;
  795. { DateTimeToString formats DateTime to the given format in FormatStr }
  796. procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime);
  797. begin
  798. Result := FormatDateTime(FormatStr, DateTime);
  799. end ;
  800. Function DateTimeToFileDate(DateTime : TDateTime) : Longint;
  801. Var YY,MM,DD,H,m,s,msec : Word;
  802. begin
  803. Decodedate (DateTime,YY,MM,DD);
  804. DecodeTime (DateTime,h,m,s,msec);
  805. {$ifndef unix}
  806. If (YY<1980) or (YY>2099) then
  807. Result:=0
  808. else
  809. begin
  810. Result:=(s shr 1) or (m shl 5) or (h shl 11);
  811. Result:=Result or longint(DD shl 16 or (MM shl 21) or (word(YY-1980) shl 25));
  812. end;
  813. {$else unix}
  814. Result:=LocalToEpoch(yy,mm,dd,h,m,s);
  815. {$endif unix}
  816. end;
  817. function CurrentYear:Word;
  818. var yy,mm,dd : word;
  819. begin
  820. Decodedate(now,yy,mm,dd);
  821. Result:=yy;
  822. end;
  823. Function FileDateToDateTime (Filedate : Longint) : TDateTime;
  824. {$ifndef unix}
  825. Var Date,Time : Word;
  826. begin
  827. Date:=FileDate shr 16;
  828. Time:=FileDate and $ffff;
  829. Result:=ComposeDateTime(EncodeDate((Date shr 9) + 1980,(Date shr 5) and 15, Date and 31),
  830. EncodeTime(Time shr 11, (Time shr 5) and 63, (Time and 31) shl 1,0));
  831. end;
  832. {$else unix}
  833. var
  834. y, mon, d, h, min, s: word;
  835. begin
  836. EpochToLocal(FileDate,y,mon,d,h,min,s);
  837. Result:=ComposeDateTime(EncodeDate(y,mon,d),EncodeTime(h,min,s,0));
  838. end;
  839. {$endif unix}
  840. function TryStrToDate(const S: ShortString; out Value: TDateTime): Boolean;
  841. begin
  842. result := TryStrToDate(S, Value, #0);
  843. end;
  844. function TryStrToDate(const S: ShortString; out Value: TDateTime;
  845. const useformat : string; separator : char = #0): Boolean;
  846. Var
  847. Msg : Ansistring;
  848. begin
  849. Value:=IntStrToDate(Msg,@S[1],Length(S),useformat,separator);
  850. Result:=(Msg='');
  851. end;
  852. function TryStrToDate(const S: AnsiString; out Value: TDateTime;
  853. const useformat : string; separator : char = #0): Boolean;
  854. Var
  855. Msg : Ansistring;
  856. begin
  857. Result:=Length(S)<>0;
  858. If Result then
  859. begin
  860. Value:=IntStrToDate(Msg,@S[1],Length(S),useformat);
  861. Result:=(Msg='');
  862. end;
  863. end;
  864. function TryStrToDate(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
  865. begin
  866. Result:=TryStrToDate(S,Value,ShortDateFormat,Separator);
  867. end;
  868. function TryStrToDate(const S: AnsiString; out Value: TDateTime): Boolean;
  869. begin
  870. Result:=TryStrToDate(S,Value,ShortDateFormat,#0);
  871. end;
  872. function TryStrToDate(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
  873. begin
  874. Result:=TryStrToDate(S,Value,ShortDateFormat,Separator);
  875. end;
  876. // function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  877. function TryStrToTime(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
  878. Var
  879. Msg : AnsiString;
  880. begin
  881. Value:=IntStrToTime(Msg,@S[1],Length(S),Separator);
  882. result:=(Msg='');
  883. end;
  884. function TryStrToTime(const S: ShortString; out Value: TDateTime): Boolean;
  885. begin
  886. Result := TryStrToTime(S,Value,#0);
  887. end;
  888. function TryStrToTime(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
  889. Var
  890. Msg : AnsiString;
  891. begin
  892. Result:=Length(S)<>0;
  893. If Result then
  894. begin
  895. Value:=IntStrToTime(Msg,@S[1],Length(S),Separator);
  896. Result:=(Msg='');
  897. end;
  898. end;
  899. function TryStrToTime(const S: AnsiString; out Value: TDateTime): Boolean;
  900. begin
  901. result := TryStrToTime(S,Value,#0);
  902. end;
  903. // function TryStrToTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  904. function TryStrToDateTime(const S: ShortString; out Value: TDateTime): Boolean;
  905. begin
  906. result:=true;
  907. try
  908. value:=StrToDateTime(s);
  909. except
  910. on EConvertError do
  911. result:=false
  912. end;
  913. end;
  914. function TryStrToDateTime(const S: AnsiString; out Value: TDateTime): Boolean;
  915. begin
  916. result:=true;
  917. try
  918. value:=StrToDateTime(s);
  919. except
  920. on EConvertError do
  921. result:=false
  922. end;
  923. end;
  924. // function TryStrToDateTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  925. function StrToDateDef(const S: ShortString; const Defvalue : TDateTime): TDateTime;
  926. begin
  927. result := StrToDateDef(S,DefValue,#0);
  928. end;
  929. function StrToTimeDef(const S: ShortString; const Defvalue : TDateTime): TDateTime;
  930. begin
  931. result := StrToTimeDef(S,DefValue,#0);
  932. end;
  933. function StrToDateTimeDef(const S: ShortString; const Defvalue : TDateTime): TDateTime;
  934. begin
  935. if not TryStrToDateTime(s,Result) Then
  936. result:=defvalue;
  937. end;
  938. function StrToDateDef(const S: ShortString; const Defvalue : TDateTime; separator : char): TDateTime;
  939. begin
  940. if not TryStrToDate(s,Result, separator) Then
  941. result:=defvalue;
  942. end;
  943. function StrToTimeDef(const S: ShortString; const Defvalue : TDateTime; separator : char): TDateTime;
  944. begin
  945. if not TryStrToTime(s,Result, separator) Then
  946. result:=defvalue;
  947. end;
  948. function StrToDateDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime;
  949. begin
  950. result := StrToDateDef(S,DefValue,#0);
  951. end;
  952. function StrToTimeDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime;
  953. begin
  954. result := StrToTimeDef(S,DefValue,#0);
  955. end;
  956. function StrToDateTimeDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime;
  957. begin
  958. if not TryStrToDateTime(s,Result) Then
  959. result:=defvalue;
  960. end;
  961. function StrToDateDef(const S: AnsiString; const Defvalue : TDateTime; separator : char): TDateTime;
  962. begin
  963. if not TryStrToDate(s,Result, separator) Then
  964. result:=defvalue;
  965. end;
  966. function StrToTimeDef(const S: AnsiString; const Defvalue : TDateTime; separator : char): TDateTime;
  967. begin
  968. if not TryStrToTime(s,Result, separator) Then
  969. result:=defvalue;
  970. end;
  971. procedure ReplaceTime(var dati:TDateTime; NewTime : TDateTime);inline;
  972. begin
  973. dati:= ComposeDateTime(dati, newtime);
  974. end;
  975. procedure ReplaceDate(var DateTime: TDateTime; const NewDate: TDateTime); inline;
  976. var
  977. tmp : TDateTime;
  978. begin
  979. tmp:=NewDate;
  980. ReplaceTime(tmp,DateTime);
  981. DateTime:=tmp;
  982. end;