dati.inc 22 KB

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