dati.inc 21 KB

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