dati.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. {
  2. *********************************************************************
  3. Copyright (C) 1997, 1998 Gertjan Schouten
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. *********************************************************************
  16. System Utilities For Free Pascal
  17. }
  18. {==============================================================================}
  19. { internal functions }
  20. {==============================================================================}
  21. Function DoEncodeDate(Year, Month, Day: Word): longint;
  22. Var
  23. D : TDateTime;
  24. begin
  25. If TryEncodeDate(Year,Month,Day,D) then
  26. Result:=Trunc(D)
  27. else
  28. Result:=0;
  29. end;
  30. function DoEncodeTime(Hour, Minute, Second, MilliSecond: word): TDateTime;
  31. begin
  32. If not TryEncodeTime(Hour,Minute,Second,MilliSecond,Result) then
  33. Result:=0;
  34. end;
  35. {==============================================================================}
  36. { Public functions }
  37. {==============================================================================}
  38. { ComposeDateTime converts a Date and a Time into one TDateTime }
  39. function ComposeDateTime(Date,Time : TDateTime) : TDateTime;
  40. begin
  41. if Date < 0 then Result := trunc(Date) - frac(Time)
  42. else Result := trunc(Date) + frac(Time);
  43. end;
  44. { DateTimeToTimeStamp converts DateTime to a TTimeStamp }
  45. function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
  46. begin
  47. result.Time := Trunc(abs(Frac(DateTime)) * MSecsPerDay);
  48. result.Date := DateDelta + trunc(DateTime);
  49. end ;
  50. { TimeStampToDateTime converts TimeStamp to a TDateTime value }
  51. function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;
  52. begin
  53. Result := ComposeDateTime(TimeStamp.Date - DateDelta,TimeStamp.Time / MSecsPerDay)
  54. end;
  55. { MSecsToTimeStamp }
  56. function MSecsToTimeStamp(MSecs: comp): TTimeStamp;
  57. begin
  58. result.Date := Trunc(msecs / msecsperday);
  59. msecs:= comp(msecs-result.date*msecsperday);
  60. result.Time := Round(MSecs);
  61. end ;
  62. { TimeStampToMSecs }
  63. function TimeStampToMSecs(const TimeStamp: TTimeStamp): comp;
  64. begin
  65. result := TimeStamp.Time + comp(timestamp.date)*msecsperday;
  66. end ;
  67. Function TryEncodeDate(Year,Month,Day : Word; Out Date : TDateTime) : Boolean;
  68. var
  69. c, ya: cardinal;
  70. begin
  71. Result:=(Year>0) and (Year<10000) and
  72. (Month in [1..12]) and
  73. (Day>0) and (Day<=MonthDays[IsleapYear(Year),Month]);
  74. If Result then
  75. begin
  76. if month > 2 then
  77. Dec(Month,3)
  78. else
  79. begin
  80. Inc(Month,9);
  81. Dec(Year);
  82. end;
  83. c:= Year DIV 100;
  84. ya:= Year - 100*c;
  85. Date := (146097*c) SHR 2 + (1461*ya) SHR 2 + (153*cardinal(Month)+2) DIV 5 + cardinal(Day);
  86. // Note that this line can't be part of the line above, since TDateTime is
  87. // signed and c and ya are not
  88. Date := Date - 693900;
  89. end
  90. end;
  91. function TryEncodeTime(Hour, Min, Sec, MSec:word; Out Time : TDateTime) : boolean;
  92. begin
  93. Result:=(Hour<24) and (Min<60) and (Sec<60) and (MSec<1000);
  94. If Result then
  95. Time:=(cardinal(Hour)*3600000+cardinal(Min)*60000+cardinal(Sec)*1000+MSec)/MSecsPerDay;
  96. end;
  97. { EncodeDate packs three variables Year, Month and Day into a
  98. TDateTime value the result is the number of days since 12/30/1899 }
  99. function EncodeDate(Year, Month, Day: word): TDateTime;
  100. begin
  101. If Not TryEncodeDate(Year,Month,Day,Result) then
  102. Raise EConvertError.CreateFmt('%d-%d-%d is not a valid date specification',
  103. [Year,Month,Day]);
  104. end;
  105. { EncodeTime packs four variables Hour, Minute, Second and MilliSecond into
  106. a TDateTime value }
  107. function EncodeTime(Hour, Minute, Second, MilliSecond:word):TDateTime;
  108. begin
  109. If not TryEncodeTime(Hour,Minute,Second,MilliSecond,Result) then
  110. Raise EConvertError.CreateFmt('%d:%d:%d.%d is not a valid time specification',
  111. [Hour,Minute,Second,MilliSecond]);
  112. end;
  113. { DecodeDate unpacks the value Date into three values:
  114. Year, Month and Day }
  115. procedure DecodeDate(Date: TDateTime; out Year, Month, Day: word);
  116. var
  117. ly,ld,lm,j : cardinal;
  118. begin
  119. if Date <= -datedelta then // If Date is before 1-1-1 then return 0-0-0
  120. begin
  121. Year := 0;
  122. Month := 0;
  123. Day := 0;
  124. end
  125. else
  126. begin
  127. j := pred((Trunc(System.Int(Date)) + 693900) SHL 2);
  128. ly:= j DIV 146097;
  129. j:= j - 146097 * cardinal(ly);
  130. ld := j SHR 2;
  131. j:=(ld SHL 2 + 3) DIV 1461;
  132. ld:= (cardinal(ld) SHL 2 + 7 - 1461*j) SHR 2;
  133. lm:=(5 * ld-3) DIV 153;
  134. ld:= (5 * ld +2 - 153*lm) DIV 5;
  135. ly:= 100 * cardinal(ly) + j;
  136. if lm < 10 then
  137. inc(lm,3)
  138. else
  139. begin
  140. dec(lm,9);
  141. inc(ly);
  142. end;
  143. year:=ly;
  144. month:=lm;
  145. day:=ld;
  146. end;
  147. end;
  148. function DecodeDateFully(const DateTime: TDateTime; out Year, Month, Day, DOW: Word): Boolean;
  149. begin
  150. DecodeDate(DateTime,Year,Month,Day);
  151. DOW:=DayOfWeek(DateTime);
  152. Result:=IsLeapYear(Year);
  153. end;
  154. { DecodeTime unpacks Time into four values:
  155. Hour, Minute, Second and MilliSecond }
  156. procedure DecodeTime(Time: TDateTime; out Hour, Minute, Second, MilliSecond: word);
  157. Var
  158. l : cardinal;
  159. begin
  160. l := Round(abs(Frac(time)) * MSecsPerDay);
  161. Hour := l div 3600000;
  162. l := l mod 3600000;
  163. Minute := l div 60000;
  164. l := l mod 60000;
  165. Second := l div 1000;
  166. l := l mod 1000;
  167. MilliSecond := l;
  168. end;
  169. { DateTimeToSystemTime converts DateTime value to SystemTime }
  170. procedure DateTimeToSystemTime(DateTime: TDateTime; out SystemTime: TSystemTime);
  171. begin
  172. DecodeDate(DateTime, SystemTime.Year, SystemTime.Month, SystemTime.Day);
  173. DecodeTime(DateTime, SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond);
  174. end ;
  175. { SystemTimeToDateTime converts SystemTime to a TDateTime value }
  176. function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;
  177. begin
  178. result := ComposeDateTime(DoEncodeDate(SystemTime.Year, SystemTime.Month, SystemTime.Day),
  179. DoEncodeTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond));
  180. end ;
  181. { DayOfWeek returns the Day of the week (sunday is day 1) }
  182. function DayOfWeek(DateTime: TDateTime): integer;
  183. begin
  184. Result := 1 + (Abs(Trunc(DateTime) - 1) mod 7);
  185. end ;
  186. { Date returns the current Date }
  187. function Date: TDateTime;
  188. var
  189. SystemTime: TSystemTime;
  190. begin
  191. GetLocalTime(SystemTime);
  192. result := DoEncodeDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  193. end ;
  194. { Time returns the current Time }
  195. function Time: TDateTime;
  196. var
  197. SystemTime: TSystemTime;
  198. begin
  199. GetLocalTime(SystemTime);
  200. Result := DoEncodeTime(SystemTime.Hour,SystemTime.Minute,SystemTime.Second,SystemTime.MilliSecond);
  201. end ;
  202. { Now returns the current Date and Time }
  203. function Now: TDateTime;
  204. var
  205. SystemTime: TSystemTime;
  206. begin
  207. GetLocalTime(SystemTime);
  208. result := systemTimeToDateTime(SystemTime);
  209. end;
  210. { IncMonth increments DateTime with NumberOfMonths months,
  211. NumberOfMonths can be less than zero }
  212. function IncMonth(const DateTime: TDateTime; NumberOfMonths: integer = 1 ): TDateTime;
  213. var
  214. Year, Month, Day : word;
  215. S : Integer;
  216. begin
  217. If NumberOfMonths>=0 then
  218. s:=1
  219. else
  220. s:=-1;
  221. DecodeDate(DateTime, Year, Month, Day);
  222. inc(Year,(NumberOfMonths div 12));
  223. inc(Month,(NumberOfMonths mod 12)-1); // Mod result always positive
  224. if Month>11 then
  225. begin
  226. Dec(Month, S*12);
  227. Inc(Year, S);
  228. end;
  229. Inc(Month); { Months from 1 to 12 }
  230. If (Day>MonthDays[IsLeapYear(Year)][Month]) then
  231. Day:=MonthDays[IsLeapYear(Year)][Month];
  232. result := Frac(DateTime) + DoEncodeDate(Year, Month, Day);
  233. end ;
  234. { IsLeapYear returns true if Year is a leap year }
  235. function IsLeapYear(Year: Word): boolean;
  236. begin
  237. Result := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
  238. end;
  239. { DateToStr returns a string representation of Date using ShortDateFormat }
  240. function DateToStr(Date: TDateTime): string;
  241. begin
  242. result := FormatDateTime('ddddd', Date);
  243. end ;
  244. { TimeToStr returns a string representation of Time using ShortTimeFormat }
  245. function TimeToStr(Time: TDateTime): string;
  246. begin
  247. result := FormatDateTime('t', Time);
  248. end ;
  249. { DateTimeToStr returns a string representation of DateTime using ShortDateTimeFormat }
  250. function DateTimeToStr(DateTime: TDateTime): string;
  251. begin
  252. result := FormatDateTime('c', DateTime);
  253. end ;
  254. { StrToDate converts the string S to a TDateTime value
  255. if S does not represent a valid date value
  256. an EConvertError will be raised }
  257. function StrToDate(const S: string): TDateTime;
  258. var
  259. df:string;
  260. d,m,y,ly:word;
  261. n,i:longint;
  262. c:word;
  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)) and (s[i] in ['0'..'9'])) 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. else if not (s[i] in ['0'..'9']) then
  321. Raise EConvertError.Create('Invalid date format');
  322. end ;
  323. // Fill in values.
  324. getLocalTime(LocalTime);
  325. ly := LocalTime.Year;
  326. If N=3 then
  327. begin
  328. y:=values[yp];
  329. m:=values[mp];
  330. d:=values[dp];
  331. end
  332. Else
  333. begin
  334. Y:=ly;
  335. If n<2 then
  336. begin
  337. d:=values[1];
  338. m := LocalTime.Month;
  339. end
  340. else
  341. If dp<mp then
  342. begin
  343. d:=values[1];
  344. m:=values[2];
  345. end
  346. else
  347. begin
  348. d:=values[2];
  349. m:=values[1];
  350. end;
  351. end;
  352. if (y >= 0) and (y < 100) then
  353. begin
  354. ly := ly - TwoDigitYearCenturyWindow;
  355. Inc(Y, ly div 100 * 100);
  356. if (TwoDigitYearCenturyWindow > 0) and (Y < ly) then
  357. Inc(Y, 100);
  358. end;
  359. Result := EncodeDate(y, m, d);
  360. end ;
  361. { StrToTime converts the string S to a TDateTime value
  362. if S does not represent a valid time value an
  363. EConvertError will be raised }
  364. function StrToTime(const s: string): TDateTime;
  365. var
  366. Len, Current: integer; PM: boolean;
  367. function GetElement: integer;
  368. var
  369. j, c: integer;
  370. begin
  371. result := -1;
  372. Inc(Current);
  373. while (result = -1) and (Current < Len) do begin
  374. if S[Current] in ['0'..'9'] then begin
  375. j := Current;
  376. while (Current < Len) and (s[Current + 1] in ['0'..'9']) do
  377. Inc(Current);
  378. val(copy(S, j, 1 + Current - j), result, c);
  379. end
  380. else if (S[Current] = TimeAMString[1]) or (S[Current] in ['a', 'A']) then begin
  381. Current := 1 + Len;
  382. end
  383. else if (S[Current] = TimePMString[1]) or (S[Current] in ['p', 'P']) then begin
  384. Current := 1 + Len;
  385. PM := True;
  386. end
  387. else if (S[Current] = TimeSeparator) or (S[Current] = ' ') then
  388. Inc(Current)
  389. else
  390. raise EConvertError.Create('Invalid Time format');
  391. end ;
  392. end ;
  393. var
  394. i: integer;
  395. TimeValues: array[0..4] of integer;
  396. begin
  397. Current := 0;
  398. Len := length(s);
  399. PM := False;
  400. for i:=0 to 4 do
  401. timevalues[i]:=0;
  402. i := 0;
  403. TimeValues[i] := GetElement;
  404. while (i < 5) and (TimeValues[i] <> -1) do begin
  405. i := i + 1;
  406. TimeValues[i] := GetElement;
  407. end ;
  408. If (i<5) and (TimeValues[I]=-1) then
  409. TimeValues[I]:=0;
  410. if PM then Inc(TimeValues[0], 12);
  411. result := EncodeTime(TimeValues[0], TimeValues[1], TimeValues[2], TimeValues[3]);
  412. end ;
  413. { StrToDateTime converts the string S to a TDateTime value
  414. if S does not represent a valid date and time value
  415. an EConvertError will be raised }
  416. function StrToDateTime(const s: string): TDateTime;
  417. var i: integer;
  418. begin
  419. i := pos(' ', s);
  420. if i > 0 then result := ComposeDateTime(StrToDate(Copy(S, 1, i - 1)), StrToTime(Copy(S, i + 1, length(S))))
  421. else result := StrToDate(S);
  422. end ;
  423. { FormatDateTime formats DateTime to the given format string FormatStr }
  424. function FormatDateTime(FormatStr: string; DateTime: TDateTime): string;
  425. var
  426. ResultLen: integer;
  427. ResultBuffer: array[0..255] of char;
  428. ResultCurrent: pchar;
  429. procedure StoreStr(Str: pchar; Len: integer);
  430. begin
  431. if ResultLen + Len < SizeOf(ResultBuffer) then begin
  432. StrMove(ResultCurrent, Str, Len);
  433. ResultCurrent := ResultCurrent + Len;
  434. ResultLen := ResultLen + Len;
  435. end ;
  436. end ;
  437. procedure StoreString(const Str: string);
  438. var Len: integer;
  439. begin
  440. Len := Length(Str);
  441. if ResultLen + Len < SizeOf(ResultBuffer) then begin
  442. StrMove(ResultCurrent, pchar(Str), Len);
  443. ResultCurrent := ResultCurrent + Len;
  444. ResultLen := ResultLen + Len;
  445. end;
  446. end;
  447. procedure StoreInt(Value, Digits: integer);
  448. var S: string; Len: integer;
  449. begin
  450. S := IntToStr(Value);
  451. Len := Length(S);
  452. if Len < Digits then begin
  453. S := copy('0000', 1, Digits - Len) + S;
  454. Len := Digits;
  455. end ;
  456. StoreStr(pchar(@S[1]), Len);
  457. end ;
  458. Function TimeReFormat(Const S : string) : string;
  459. // Change m into n for time formatting.
  460. Var i : longint;
  461. begin
  462. Result:=S;
  463. For I:=1 to Length(Result) do
  464. If Result[i]='m' then
  465. result[i]:='n';
  466. end;
  467. var
  468. Year, Month, Day, DayOfWeek, Hour, Minute, Second, MilliSecond: word;
  469. procedure StoreFormat(const FormatStr: string);
  470. var
  471. Token,lastformattoken: char;
  472. FormatCurrent: pchar;
  473. FormatEnd: pchar;
  474. Count: integer;
  475. Clock12: boolean;
  476. P: pchar;
  477. tmp:integer;
  478. begin
  479. FormatCurrent := Pchar(FormatStr);
  480. FormatEnd := FormatCurrent + Length(FormatStr);
  481. Clock12 := false;
  482. P := FormatCurrent;
  483. while P < FormatEnd do begin
  484. Token := UpCase(P^);
  485. if Token in ['"', ''''] then begin
  486. P := P + 1;
  487. while (P < FormatEnd) and (P^ <> Token) do
  488. P := P + 1;
  489. end
  490. else if Token = 'A' then begin
  491. if (StrLIComp(P, 'A/P', 3) = 0) or
  492. (StrLIComp(P, 'AMPM', 4) = 0) or
  493. (StrLIComp(P, 'AM/PM', 5) = 0) then begin
  494. Clock12 := true;
  495. break;
  496. end ;
  497. end ;
  498. P := P + 1;
  499. end ;
  500. token:=#255;
  501. lastformattoken:=' ';
  502. while FormatCurrent < FormatEnd do
  503. 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' :
  537. begin
  538. while (P < FormatEnd) and (UpCase(P^) = Token) do
  539. P := P + 1;
  540. Count := P - FormatCurrent;
  541. case Token of
  542. ' ': StoreStr(FormatCurrent, Count);
  543. 'Y': begin
  544. if Count>2 then
  545. StoreInt(Year, 4)
  546. else
  547. StoreInt(Year mod 100, 2);
  548. end;
  549. 'M': begin
  550. if lastformattoken='H' then
  551. begin
  552. if Count = 1 then
  553. StoreInt(Minute, 0)
  554. else
  555. StoreInt(Minute, 2);
  556. end
  557. else
  558. begin
  559. case Count of
  560. 1: StoreInt(Month, 0);
  561. 2: StoreInt(Month, 2);
  562. 3: StoreString(ShortMonthNames[Month]);
  563. 4: StoreString(LongMonthNames[Month]);
  564. end;
  565. end;
  566. end;
  567. 'D': begin
  568. case Count of
  569. 1: StoreInt(Day, 0);
  570. 2: StoreInt(Day, 2);
  571. 3: StoreString(ShortDayNames[DayOfWeek]);
  572. 4: StoreString(LongDayNames[DayOfWeek]);
  573. 5: StoreFormat(ShortDateFormat);
  574. 6: StoreFormat(LongDateFormat);
  575. end ;
  576. end ;
  577. 'H': begin
  578. if Clock12 then begin
  579. tmp:=hour mod 12;
  580. if tmp=0 then tmp:=12;
  581. if Count = 1 then StoreInt(tmp, 0)
  582. else StoreInt(tmp, 2);
  583. end
  584. else begin
  585. if Count = 1 then StoreInt(Hour, 0)
  586. else StoreInt(Hour, 2);
  587. end ;
  588. end ;
  589. 'N': begin
  590. if Count = 1 then StoreInt(Minute, 0)
  591. else StoreInt(Minute, 2);
  592. end ;
  593. 'S': begin
  594. if Count = 1 then StoreInt(Second, 0)
  595. else StoreInt(Second, 2);
  596. end ;
  597. 'Z': begin
  598. if Count = 1 then StoreInt(MilliSecond, 0)
  599. else StoreInt(MilliSecond, 3);
  600. end ;
  601. 'T': begin
  602. if Count = 1 then StoreFormat(timereformat(ShortTimeFormat))
  603. else StoreFormat(TimeReformat(LongTimeFormat));
  604. end ;
  605. 'C':
  606. begin
  607. StoreFormat(ShortDateFormat);
  608. if (Hour<>0) or (Minute<>0) or (Second<>0) then
  609. begin
  610. StoreString(' ');
  611. StoreFormat(TimeReformat(ShortTimeFormat));
  612. end;
  613. end;
  614. end;
  615. lastformattoken:=token;
  616. end;
  617. else
  618. StoreStr(@Token, 1);
  619. end ;
  620. FormatCurrent := FormatCurrent + Count;
  621. end ;
  622. end ;
  623. begin
  624. DecodeDateFully(DateTime, Year, Month, Day, DayOfWeek);
  625. DecodeTime(DateTime, Hour, Minute, Second, MilliSecond);
  626. ResultLen := 0;
  627. ResultCurrent := @ResultBuffer;
  628. StoreFormat(FormatStr);
  629. ResultBuffer[ResultLen] := #0;
  630. result := StrPas(@ResultBuffer);
  631. end ;
  632. { DateTimeToString formats DateTime to the given format in FormatStr }
  633. procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime);
  634. begin
  635. Result := FormatDateTime(FormatStr, DateTime);
  636. end ;
  637. Function DateTimeToFileDate(DateTime : TDateTime) : Longint;
  638. Var YY,MM,DD,H,m,s,msec : Word;
  639. begin
  640. Decodedate (DateTime,YY,MM,DD);
  641. DecodeTime (DateTime,h,m,s,msec);
  642. {$ifndef unix}
  643. If (YY<1980) or (YY>2099) then
  644. Result:=0
  645. else
  646. begin
  647. Result:=(s shr 1) or (m shl 5) or (h shl 11);
  648. Result:=Result or DD shl 16 or (MM shl 21) or ((YY-1980) shl 25);
  649. end;
  650. {$else unix}
  651. Result:=LocalToEpoch(yy,mm,dd,h,m,s);
  652. {$endif unix}
  653. end;
  654. function CurrentYear:Word;
  655. var yy,mm,dd : word;
  656. begin
  657. Decodedate(now,yy,mm,dd);
  658. Result:=yy;
  659. end;
  660. Function FileDateToDateTime (Filedate : Longint) : TDateTime;
  661. {$ifndef unix}
  662. Var Date,Time : Word;
  663. begin
  664. Date:=FileDate shr 16;
  665. Time:=FileDate and $ffff;
  666. Result:=ComposeDateTime(EncodeDate((Date shr 9) + 1980,(Date shr 5) and 15, Date and 31),
  667. EncodeTime(Time shr 11, (Time shr 5) and 63, (Time and 31) shl 1,0));
  668. end;
  669. {$else unix}
  670. var
  671. y, mon, d, h, min, s: word;
  672. begin
  673. EpochToLocal(FileDate,y,mon,d,h,min,s);
  674. Result:=ComposeDateTime(EncodeDate(y,mon,d),EncodeTime(h,min,s,0));
  675. end;
  676. {$endif unix}
  677. function TryStrToDate(const S: string; out Value: TDateTime): Boolean;
  678. begin
  679. result:=true;
  680. try
  681. value:=StrToDate(s);
  682. except
  683. on EConvertError do
  684. result:=false
  685. end;
  686. end;
  687. // function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  688. function TryStrToTime(const S: string; out Value: TDateTime): Boolean;
  689. begin
  690. result:=true;
  691. try
  692. value:=StrToTime(s);
  693. except
  694. on EConvertError do
  695. result:=false
  696. end;
  697. end;
  698. // function TryStrToTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  699. function TryStrToDateTime(const S: string; out Value: TDateTime): Boolean;
  700. begin
  701. result:=true;
  702. try
  703. value:=StrToDateTime(s);
  704. except
  705. on EConvertError do
  706. result:=false
  707. end;
  708. end;
  709. // function TryStrToDateTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;