dati.inc 20 KB

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