dati.inc 22 KB

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