dati.inc 22 KB

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