dati.inc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. {
  2. *********************************************************************
  3. $Id$
  4. Copyright (C) 1997, 1998 Gertjan Schouten
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16. *********************************************************************
  17. System Utilities For Free Pascal
  18. }
  19. {==============================================================================}
  20. { internal functions }
  21. {==============================================================================}
  22. const
  23. DayTable: array[Boolean, 1..12] of longint =
  24. ((0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334),
  25. (0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335));
  26. Function DoEncodeDate(Year, Month, Day: Word): longint;
  27. Var
  28. D : TDateTime;
  29. begin
  30. If TryEncodeDate(Year,Month,Day,D) then
  31. Result:=Trunc(D)
  32. else
  33. Result:=0;
  34. end;
  35. function DoEncodeTime(Hour, Minute, Second, MilliSecond: word): longint;
  36. Var
  37. T : TDateTime;
  38. begin
  39. If TryEncodeTime(Hour,Minute,Second,MilliSecond,T) then
  40. Result:=trunc(T*MSecsPerDay)
  41. else
  42. Result:=0;
  43. end;
  44. {==============================================================================}
  45. { Public functions }
  46. {==============================================================================}
  47. { DateTimeToTimeStamp converts DateTime to a TTimeStamp }
  48. function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
  49. begin
  50. result.Time := Trunc(Frac(DateTime) * MSecsPerDay);
  51. result.Date := 1 + DateDelta + Trunc(System.Int(DateTime));
  52. end ;
  53. { TimeStampToDateTime converts TimeStamp to a TDateTime value }
  54. function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;
  55. begin
  56. result := (TimeStamp.Date - DateDelta - 1) + (TimeStamp.Time / MSecsPerDay);
  57. end ;
  58. { MSecsToTimeStamp }
  59. function MSecsToTimeStamp(MSecs: comp): TTimeStamp;
  60. begin
  61. result.Date := Round(msecs / msecsperday);
  62. msecs:= comp(msecs-result.date*msecsperday);
  63. result.Time := Round(MSecs);
  64. end ;
  65. { TimeStampToMSecs }
  66. function TimeStampToMSecs(const TimeStamp: TTimeStamp): comp;
  67. begin
  68. result := TimeStamp.Time + timestamp.date*msecsperday;
  69. end ;
  70. Function TryEncodeDate(Year,Month,Day : Word; Var Date : TDateTime) : Boolean;
  71. var
  72. c, ya: cardinal;
  73. begin
  74. Result:=(Year>0) and (Year<10000) and
  75. (Month in [1..12]) and
  76. (Day>0) and (Day<=MonthDays[IsleapYear(Year),Month]);
  77. If Result then
  78. begin
  79. if month > 2 then
  80. Dec(Month,3)
  81. else
  82. begin
  83. Inc(Month,9);
  84. Dec(Year);
  85. end;
  86. c:= Year DIV 100;
  87. ya:= Year - 100*c;
  88. Date := (146097*c) SHR 2 + (1461*ya) SHR 2 + (153*cardinal(Month)+2) DIV 5 + cardinal(Day) - 693900;
  89. end
  90. end;
  91. function TryEncodeTime(Hour, Min, Sec, MSec:word; Var Time : TDateTime) : boolean;
  92. begin
  93. Result:=(Hour<24) and (Min<60) and (Sec<60) and (MSec<1000);
  94. If Result then
  95. Time:=(Hour*3600000+Min*60000+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 Exception.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 Exception.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; var Year, Month, Day: word);
  116. var
  117. j : cardinal;
  118. begin
  119. j := pred((Trunc(System.Int(Date)) + 693900) SHL 2);
  120. Year:= j DIV 146097;
  121. j:= j - 146097 * cardinal(Year);
  122. Day := j SHR 2;
  123. j:=(Day SHL 2 + 3) DIV 1461;
  124. Day:= (cardinal(Day) SHL 2 + 7 - 1461*j) SHR 2;
  125. Month:=(5 * Day-3) DIV 153;
  126. Day:= (5 * Day +2 - 153*Month) DIV 5;
  127. Year:= 100 * cardinal(Year) + j;
  128. if Month < 10 then
  129. inc(Month,3)
  130. else
  131. begin
  132. dec(Month,9);
  133. inc(Year);
  134. end;
  135. end;
  136. function DecodeDateFully(const DateTime: TDateTime; var Year, Month, Day, DOW: Word): Boolean;
  137. begin
  138. DecodeDate(DateTime,Year,Month,Day);
  139. DOW:=DateTimeToTimeStamp(DateTime).Date mod 7+1;
  140. Result:=IsLeapYear(Year);
  141. end;
  142. { DecodeTime unpacks Time into four values:
  143. Hour, Minute, Second and MilliSecond }
  144. procedure DecodeTime(Time: TDateTime; var Hour, Minute, Second, MilliSecond: word);
  145. Var
  146. l : cardinal;
  147. begin
  148. l := Round(Frac(time) * MSecsPerDay);
  149. Hour := l div 3600000;
  150. l := l mod 3600000;
  151. Minute := l div 60000;
  152. l := l mod 60000;
  153. Second := l div 1000;
  154. l := l mod 1000;
  155. MilliSecond := l;
  156. end;
  157. { DateTimeToSystemTime converts DateTime value to SystemTime }
  158. procedure DateTimeToSystemTime(DateTime: TDateTime; var SystemTime: TSystemTime);
  159. begin
  160. DecodeDate(DateTime, SystemTime.Year, SystemTime.Month, SystemTime.Day);
  161. DecodeTime(DateTime, SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond);
  162. end ;
  163. { SystemTimeToDateTime converts SystemTime to a TDateTime value }
  164. function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;
  165. begin
  166. result := DoEncodeDate(SystemTime.Year, SystemTime.Month, SystemTime.Day) +
  167. DoEncodeTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond) / MSecsPerDay;
  168. end ;
  169. { DayOfWeek returns the Day of the week (sunday is day 1) }
  170. function DayOfWeek(DateTime: TDateTime): integer;
  171. begin
  172. Result := 1 + (Abs(Trunc(DateTime) - 1) mod 7);
  173. end ;
  174. { Date returns the current Date }
  175. function Date: TDateTime;
  176. var
  177. SystemTime: TSystemTime;
  178. begin
  179. GetLocalTime(SystemTime);
  180. result := DoEncodeDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  181. end ;
  182. { Time returns the current Time }
  183. function Time: TDateTime;
  184. var
  185. SystemTime: TSystemTime;
  186. begin
  187. GetLocalTime(SystemTime);
  188. Result := DoEncodeTime(SystemTime.Hour,SystemTime.Minute,SystemTime.Second,SystemTime.MilliSecond) / MSecsPerDay;
  189. end ;
  190. { Now returns the current Date and Time }
  191. function Now: TDateTime;
  192. var
  193. SystemTime: TSystemTime;
  194. begin
  195. GetLocalTime(SystemTime);
  196. result := DoEncodeDate(SystemTime.Year,SystemTime.Month,SystemTime.Day) +
  197. DoEncodeTime(SystemTime.Hour,SystemTime.Minute,SystemTime.Second,SystemTime.MilliSecond) / MSecsPerDay;
  198. end ;
  199. { IncMonth increments DateTime with NumberOfMonths months,
  200. NumberOfMonths can be less than zero }
  201. function IncMonth(const DateTime: TDateTime; NumberOfMonths: integer): TDateTime;
  202. var
  203. Year, Month, Day: word;
  204. S : Integer;
  205. begin
  206. If NumberOfMonths>=0 then
  207. s:=1
  208. else
  209. s:=-1;
  210. DecodeDate(DateTime, Year, Month, Day);
  211. inc(Year,(NumberOfMonths div 12));
  212. inc(Month,(NumberOfMonths mod 12)-1); // Mod result always positive
  213. if Month>11 then
  214. begin
  215. Dec(Month, S*12);
  216. Inc(Year, S);
  217. end;
  218. Inc(Month); { Months from 1 to 12 }
  219. if (Month = 2) and (IsLeapYear(Year)) and (Day > 28) then
  220. Day := 28;
  221. result := Frac(DateTime) + DoEncodeDate(Year, Month, Day);
  222. end ;
  223. { IsLeapYear returns true if Year is a leap year }
  224. function IsLeapYear(Year: Word): boolean;
  225. begin
  226. Result := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
  227. end;
  228. { DateToStr returns a string representation of Date using ShortDateFormat }
  229. function DateToStr(Date: TDateTime): string;
  230. begin
  231. result := FormatDateTime('ddddd', Date);
  232. end ;
  233. { TimeToStr returns a string representation of Time using ShortTimeFormat }
  234. function TimeToStr(Time: TDateTime): string;
  235. begin
  236. result := FormatDateTime('t', Time);
  237. end ;
  238. { DateTimeToStr returns a string representation of DateTime using ShortDateTimeFormat }
  239. function DateTimeToStr(DateTime: TDateTime): string;
  240. begin
  241. result := FormatDateTime('c', DateTime);
  242. end ;
  243. { StrToDate converts the string S to a TDateTime value
  244. if S does not represent a valid date value
  245. an EConvertError will be raised }
  246. function StrToDate(const S: string): TDateTime;
  247. var
  248. df:string;
  249. d,m,y:word;
  250. n,i:longint;
  251. c:word;
  252. dp,mp,yp,which : Byte;
  253. s1:string[4];
  254. values:array[1..3] of longint;
  255. LocalTime:tsystemtime;
  256. begin
  257. df := UpperCase(ShortDateFormat);
  258. { Determine order of D,M,Y }
  259. yp:=0;
  260. mp:=0;
  261. dp:=0;
  262. Which:=0;
  263. i:=0;
  264. while (i<Length(df)) and (Which<3) do
  265. begin
  266. inc(i);
  267. Case df[i] of
  268. 'Y' :
  269. if yp=0 then
  270. begin
  271. Inc(Which);
  272. yp:=which;
  273. end;
  274. 'M' :
  275. if mp=0 then
  276. begin
  277. Inc(Which);
  278. mp:=which;
  279. end;
  280. 'D' :
  281. if dp=0 then
  282. begin
  283. Inc(Which);
  284. dp:=which;
  285. end;
  286. end;
  287. end;
  288. if Which<>3 then
  289. Raise EConvertError.Create('Illegal format string');
  290. { Get actual values }
  291. for i := 1 to 3 do
  292. values[i] := 0;
  293. s1 := '';
  294. n := 0;
  295. for i := 1 to length(s) do
  296. begin
  297. if (s[i] in ['0'..'9']) then
  298. s1 := s1 + s[i];
  299. if (s[i] in [dateseparator,' ']) or (i = length(s)) then
  300. begin
  301. inc(n);
  302. if n>3 then
  303. Raise EConvertError.Create('Invalid date format');
  304. val(s1, values[n], c);
  305. if c<>0 then
  306. Raise EConvertError.Create('Invalid date format');
  307. s1 := '';
  308. end ;
  309. end ;
  310. // Fill in values.
  311. If N=3 then
  312. begin
  313. y:=values[yp];
  314. m:=values[mp];
  315. d:=values[dp];
  316. end
  317. Else
  318. begin
  319. getLocalTime(LocalTime);
  320. y := LocalTime.Year;
  321. If n<2 then
  322. begin
  323. d:=values[1];
  324. m := LocalTime.Month;
  325. end
  326. else
  327. If dp<mp then
  328. begin
  329. d:=values[1];
  330. m:=values[2];
  331. end
  332. else
  333. begin
  334. d:=values[2];
  335. m:=values[1];
  336. end;
  337. end;
  338. if (y >= 0) and (y < 100) then
  339. inc(y,1900);
  340. Result := DoEncodeDate(y, m, d);
  341. end ;
  342. { StrToTime converts the string S to a TDateTime value
  343. if S does not represent a valid time value an
  344. EConvertError will be raised }
  345. function StrToTime(const s: string): TDateTime;
  346. var
  347. Len, Current: integer; PM: boolean;
  348. function GetElement: integer;
  349. var
  350. j: integer; c: word;
  351. begin
  352. result := -1;
  353. Inc(Current);
  354. while (result = -1) and (Current < Len) do begin
  355. if S[Current] in ['0'..'9'] then begin
  356. j := Current;
  357. while (Current < Len) and (s[Current + 1] in ['0'..'9']) do
  358. Inc(Current);
  359. val(copy(S, j, 1 + Current - j), result, c);
  360. end
  361. else if (S[Current] = TimeAMString[1]) or (S[Current] in ['a', 'A']) then begin
  362. Current := 1 + Len;
  363. end
  364. else if (S[Current] = TimePMString[1]) or (S[Current] in ['p', 'P']) then begin
  365. Current := 1 + Len;
  366. PM := True;
  367. end
  368. else if (S[Current] = TimeSeparator) or (S[Current] = ' ') then
  369. Inc(Current)
  370. else
  371. raise EConvertError.Create('Invalid Time format');
  372. end ;
  373. end ;
  374. var
  375. i: integer;
  376. TimeValues: array[0..4] of integer;
  377. begin
  378. Current := 0;
  379. Len := length(s);
  380. PM := False;
  381. for i:=0 to 4 do
  382. timevalues[i]:=0;
  383. i := 0;
  384. TimeValues[i] := GetElement;
  385. while (i < 5) and (TimeValues[i] <> -1) do begin
  386. i := i + 1;
  387. TimeValues[i] := GetElement;
  388. end ;
  389. If (i<5) and (TimeValues[I]=-1) then
  390. TimeValues[I]:=0;
  391. if PM then Inc(TimeValues[0], 12);
  392. result := EncodeTime(TimeValues[0], TimeValues[1], TimeValues[2], TimeValues[3]);
  393. end ;
  394. { StrToDateTime converts the string S to a TDateTime value
  395. if S does not represent a valid date and time value
  396. an EConvertError will be raised }
  397. function StrToDateTime(const s: string): TDateTime;
  398. var i: integer;
  399. begin
  400. i := pos(' ', s);
  401. if i > 0 then result := StrToDate(Copy(S, 1, i - 1)) + StrToTime(Copy(S, i + 1, length(S)))
  402. else result := StrToDate(S);
  403. end ;
  404. { FormatDateTime formats DateTime to the given format string FormatStr }
  405. function FormatDateTime(FormatStr: string; DateTime: TDateTime): string;
  406. var
  407. ResultLen: integer;
  408. ResultBuffer: array[0..255] of char;
  409. ResultCurrent: pchar;
  410. procedure StoreStr(Str: pchar; Len: integer);
  411. begin
  412. if ResultLen + Len < SizeOf(ResultBuffer) then begin
  413. StrMove(ResultCurrent, Str, Len);
  414. ResultCurrent := ResultCurrent + Len;
  415. ResultLen := ResultLen + Len;
  416. end ;
  417. end ;
  418. procedure StoreString(const Str: string);
  419. var Len: integer;
  420. begin
  421. Len := Length(Str);
  422. if ResultLen + Len < SizeOf(ResultBuffer) then begin
  423. StrMove(ResultCurrent, pchar(Str), Len);
  424. ResultCurrent := ResultCurrent + Len;
  425. ResultLen := ResultLen + Len;
  426. end;
  427. end;
  428. procedure StoreInt(Value, Digits: integer);
  429. var S: string; Len: integer;
  430. begin
  431. S := IntToStr(Value);
  432. Len := Length(S);
  433. if Len < Digits then begin
  434. S := copy('0000', 1, Digits - Len) + S;
  435. Len := Digits;
  436. end ;
  437. StoreStr(pchar(@S[1]), Len);
  438. end ;
  439. Function TimeReFormat(Const S : string) : string;
  440. // Change m into n for time formatting.
  441. Var i : longint;
  442. begin
  443. Result:=S;
  444. For I:=1 to Length(Result) do
  445. If Result[i]='m' then
  446. result[i]:='n';
  447. end;
  448. var
  449. Year, Month, Day, DayOfWeek, Hour, Minute, Second, MilliSecond: word;
  450. procedure StoreFormat(const FormatStr: string);
  451. var
  452. Token: char;
  453. FormatCurrent: pchar;
  454. FormatEnd: pchar;
  455. Count: integer;
  456. Clock12: boolean;
  457. P: pchar;
  458. begin
  459. FormatCurrent := Pchar(FormatStr);
  460. FormatEnd := FormatCurrent + Length(FormatStr);
  461. Clock12 := false;
  462. P := FormatCurrent;
  463. while P < FormatEnd do begin
  464. Token := UpCase(P^);
  465. if Token in ['"', ''''] then begin
  466. P := P + 1;
  467. while (P < FormatEnd) and (P^ <> Token) do
  468. P := P + 1;
  469. end
  470. else if Token = 'A' then begin
  471. if (StrLIComp(P, 'A/P', 3) = 0) or
  472. (StrLIComp(P, 'AMPM', 4) = 0) or
  473. (StrLIComp(P, 'AM/PM', 5) = 0) then begin
  474. Clock12 := true;
  475. break;
  476. end ;
  477. end ;
  478. P := P + 1;
  479. end ;
  480. while FormatCurrent < FormatEnd do begin
  481. Token := UpCase(FormatCurrent^);
  482. Count := 1;
  483. P := FormatCurrent + 1;
  484. case Token of
  485. '''', '"': begin
  486. while (P < FormatEnd) and (p^ <> Token) do
  487. P := P + 1;
  488. P := P + 1;
  489. Count := P - FormatCurrent;
  490. StoreStr(FormatCurrent + 1, Count - 2);
  491. end ;
  492. 'A': begin
  493. if StrLIComp(FormatCurrent, 'AMPM', 4) = 0 then begin
  494. Count := 4;
  495. if Hour < 12 then StoreString(TimeAMString)
  496. else StoreString(TimePMString);
  497. end
  498. else if StrLIComp(FormatCurrent, 'AM/PM', 5) = 0 then begin
  499. Count := 5;
  500. if Hour < 12 then StoreStr('am', 2)
  501. else StoreStr('pm', 2);
  502. end
  503. else if StrLIComp(FormatCurrent, 'A/P', 3) = 0 then begin
  504. Count := 3;
  505. if Hour < 12 then StoreStr('a', 1)
  506. else StoreStr('p', 1);
  507. end
  508. else
  509. Raise EConvertError.Create('Illegal character in format string');
  510. end ;
  511. '/': StoreStr(@DateSeparator, 1);
  512. ':': StoreStr(@TimeSeparator, 1);
  513. ' ', 'C', 'D', 'H', 'M', 'N', 'S', 'T', 'Y','Z' : begin
  514. while (P < FormatEnd) and (UpCase(P^) = Token) do
  515. P := P + 1;
  516. Count := P - FormatCurrent;
  517. case Token of
  518. ' ': StoreStr(FormatCurrent, Count);
  519. 'Y': begin
  520. case Count of
  521. 1: StoreInt(Year, 0);
  522. 2: StoreInt(Year mod 100, 2);
  523. 4: StoreInt(Year, 4);
  524. end ;
  525. end ;
  526. 'M': begin
  527. case Count of
  528. 1: StoreInt(Month, 0);
  529. 2: StoreInt(Month, 2);
  530. 3: StoreString(ShortMonthNames[Month]);
  531. 4: StoreString(LongMonthNames[Month]);
  532. end ;
  533. end ;
  534. 'D': begin
  535. case Count of
  536. 1: StoreInt(Day, 0);
  537. 2: StoreInt(Day, 2);
  538. 3: StoreString(ShortDayNames[DayOfWeek]);
  539. 4: StoreString(LongDayNames[DayOfWeek]);
  540. 5: StoreFormat(ShortDateFormat);
  541. 6: StoreFormat(LongDateFormat);
  542. end ;
  543. end ;
  544. 'H': begin
  545. if Clock12 then begin
  546. if Count = 1 then StoreInt(Hour mod 12, 0)
  547. else StoreInt(Hour mod 12, 2);
  548. end
  549. else begin
  550. if Count = 1 then StoreInt(Hour, 0)
  551. else StoreInt(Hour, 2);
  552. end ;
  553. end ;
  554. 'N': begin
  555. if Count = 1 then StoreInt(Minute, 0)
  556. else StoreInt(Minute, 2);
  557. end ;
  558. 'S': begin
  559. if Count = 1 then StoreInt(Second, 0)
  560. else StoreInt(Second, 2);
  561. end ;
  562. 'Z': begin
  563. if Count = 1 then StoreInt(MilliSecond, 0)
  564. else StoreInt(MilliSecond, 3);
  565. end ;
  566. 'T': begin
  567. if Count = 1 then StoreFormat(timereformat(ShortTimeFormat))
  568. else StoreFormat(TimeReformat(LongTimeFormat));
  569. end ;
  570. 'C':
  571. begin
  572. StoreFormat(ShortDateFormat);
  573. if (Hour<>0) or (Minute<>0) or (Second<>0) then
  574. begin
  575. StoreString(' ');
  576. StoreFormat(TimeReformat(ShortTimeFormat));
  577. end;
  578. end;
  579. end ;
  580. end ;
  581. else
  582. StoreStr(@Token, 1);
  583. end ;
  584. FormatCurrent := FormatCurrent + Count;
  585. end ;
  586. end ;
  587. begin
  588. DecodeDate(DateTime, Year, Month, Day);
  589. DecodeTime(DateTime, Hour, Minute, Second, MilliSecond);
  590. DayOfWeek := SysUtils.DayOfWeek(DateTime);
  591. ResultLen := 0;
  592. ResultCurrent := @ResultBuffer;
  593. StoreFormat(FormatStr);
  594. ResultBuffer[ResultLen] := #0;
  595. result := StrPas(@ResultBuffer);
  596. end ;
  597. { DateTimeToString formats DateTime to the given format in FormatStr }
  598. procedure DateTimeToString(var Result: string; const FormatStr: string; const DateTime: TDateTime);
  599. begin
  600. Result := FormatDateTime(FormatStr, DateTime);
  601. end ;
  602. Function DateTimeToFileDate(DateTime : TDateTime) : Longint;
  603. Var YY,MM,DD,H,m,s,msec : Word;
  604. begin
  605. Decodedate (DateTime,YY,MM,DD);
  606. If (YY<1980) or (YY>2099) then
  607. Result:=0
  608. else
  609. begin
  610. DecodeTime (DateTime,h,m,s,msec);
  611. Result:=(s shr 1) or (m shl 5) or (h shl 11);
  612. Result:=Result or DD shl 16 or (MM shl 21) or ((YY-1980) shl 25);
  613. end;
  614. end;
  615. Function FileDateToDateTime (Filedate : Longint) : TDateTime;
  616. Var Date,Time : Word;
  617. begin
  618. Date:=FileDate shr 16;
  619. Time:=FileDate and $ffff;
  620. Result:=EncodeDate((Date shr 9) + 1980,(Date shr 5) and 15, Date and 31) +
  621. EncodeTime(Time shr 11, (Time shr 5) and 63, (Time and 31) shl 1,0);
  622. end;
  623. {
  624. $Log$
  625. Revision 1.9 2003-01-18 23:45:37 michael
  626. + Fixed EncodeDate/Time so they use TryEncodeDate/Time
  627. Revision 1.8 2002/12/25 01:03:48 peter
  628. * some date constants added
  629. Revision 1.7 2002/09/07 21:06:51 carl
  630. * bugfix 1867 (merged)
  631. Revision 1.6 2002/09/07 16:01:22 peter
  632. * old logs removed and tabs fixed
  633. }