dati.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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 := Round(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:=TDateTime(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. TempMonth, S: Integer;
  215. Year, Month, Day : word;
  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. TempMonth:=Month+(NumberOfMonths mod 12)-1;
  224. if (TempMonth>11) or
  225. (TempMonth<0) then
  226. begin
  227. Dec(TempMonth, S*12);
  228. Inc(Year, S);
  229. end;
  230. Month:=TempMonth+1; { Months from 1 to 12 }
  231. If (Day>MonthDays[IsLeapYear(Year)][Month]) then
  232. Day:=MonthDays[IsLeapYear(Year)][Month];
  233. result := Frac(DateTime) + DoEncodeDate(Year, Month, Day);
  234. end ;
  235. { IsLeapYear returns true if Year is a leap year }
  236. function IsLeapYear(Year: Word): boolean;
  237. begin
  238. Result := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
  239. end;
  240. { DateToStr returns a string representation of Date using ShortDateFormat }
  241. function DateToStr(Date: TDateTime): string;
  242. begin
  243. result := FormatDateTime('ddddd', Date);
  244. end ;
  245. { TimeToStr returns a string representation of Time using LongTimeFormat }
  246. function TimeToStr(Time: TDateTime): string;
  247. begin
  248. result := FormatDateTime('tt', Time);
  249. end ;
  250. { DateTimeToStr returns a string representation of DateTime using LongDateTimeFormat }
  251. function DateTimeToStr(DateTime: TDateTime): string;
  252. begin
  253. result := FormatDateTime('c', DateTime);
  254. end ;
  255. { StrToDate converts the string S to a TDateTime value
  256. if S does not represent a valid date value
  257. an EConvertError will be raised }
  258. function StrToDate(const S: string): TDateTime;
  259. var
  260. df:string;
  261. d,m,y,ly:word;
  262. n,i:longint;
  263. c:word;
  264. dp,mp,yp,which : Byte;
  265. s1:string[4];
  266. values:array[1..3] of longint;
  267. LocalTime:tsystemtime;
  268. YearMoreThenTwoDigits : boolean;
  269. begin
  270. YearMoreThenTwoDigits := False;
  271. df := UpperCase(ShortDateFormat);
  272. { Determine order of D,M,Y }
  273. yp:=0;
  274. mp:=0;
  275. dp:=0;
  276. Which:=0;
  277. i:=0;
  278. while (i<Length(df)) and (Which<3) do
  279. begin
  280. inc(i);
  281. Case df[i] of
  282. 'Y' :
  283. if yp=0 then
  284. begin
  285. Inc(Which);
  286. yp:=which;
  287. end;
  288. 'M' :
  289. if mp=0 then
  290. begin
  291. Inc(Which);
  292. mp:=which;
  293. end;
  294. 'D' :
  295. if dp=0 then
  296. begin
  297. Inc(Which);
  298. dp:=which;
  299. end;
  300. end;
  301. end;
  302. if Which<>3 then
  303. Raise EConvertError.Create('Illegal format string');
  304. { Get actual values }
  305. for i := 1 to 3 do
  306. values[i] := 0;
  307. s1 := '';
  308. n := 0;
  309. for i := 1 to length(s) do
  310. begin
  311. if (s[i] in ['0'..'9']) then
  312. s1 := s1 + s[i];
  313. if (s[i] in [dateseparator,' ']) or ((i = length(s)) and (s[i] in ['0'..'9'])) then
  314. begin
  315. inc(n);
  316. if n>3 then
  317. Raise EConvertError.Create('Invalid date format');
  318. // Check if the year has more then two digits (if n=yp, then we are evaluating the year.)
  319. if (n=yp) and (length(s1)>2) then YearMoreThenTwoDigits := True;
  320. val(s1, values[n], c);
  321. if c<>0 then
  322. Raise EConvertError.Create('Invalid date format');
  323. s1 := '';
  324. end
  325. else if not (s[i] in ['0'..'9']) then
  326. Raise EConvertError.Create('Invalid date format');
  327. end ;
  328. // Fill in values.
  329. getLocalTime(LocalTime);
  330. ly := LocalTime.Year;
  331. If N=3 then
  332. begin
  333. y:=values[yp];
  334. m:=values[mp];
  335. d:=values[dp];
  336. end
  337. Else
  338. begin
  339. Y:=ly;
  340. If n<2 then
  341. begin
  342. d:=values[1];
  343. m := LocalTime.Month;
  344. end
  345. else
  346. If dp<mp then
  347. begin
  348. d:=values[1];
  349. m:=values[2];
  350. end
  351. else
  352. begin
  353. d:=values[2];
  354. m:=values[1];
  355. end;
  356. end;
  357. if (y >= 0) and (y < 100) and not YearMoreThenTwoDigits then
  358. begin
  359. ly := ly - TwoDigitYearCenturyWindow;
  360. Inc(Y, ly div 100 * 100);
  361. if (TwoDigitYearCenturyWindow > 0) and (Y < ly) then
  362. Inc(Y, 100);
  363. end;
  364. Result := EncodeDate(y, m, d);
  365. end ;
  366. { StrToTime converts the string S to a TDateTime value
  367. if S does not represent a valid time value an
  368. EConvertError will be raised }
  369. function StrToTime(const s: string): TDateTime;
  370. var
  371. Len, Current: integer; PM: boolean;
  372. function GetElement: integer;
  373. var
  374. j, c: integer;
  375. begin
  376. result := -1;
  377. Inc(Current);
  378. while (result = -1) and (Current < Len) do begin
  379. if S[Current] in ['0'..'9'] then begin
  380. j := Current;
  381. while (Current < Len) and (s[Current + 1] in ['0'..'9']) do
  382. Inc(Current);
  383. val(copy(S, j, 1 + Current - j), result, c);
  384. end
  385. else if ((TimeAMString<>'') and (S[Current] = TimeAMString[1])) or (S[Current] in ['a', 'A']) then begin
  386. Current := 1 + Len;
  387. end
  388. else if ((TimePMString<>'') and (S[Current] = TimePMString[1])) or (S[Current] in ['p', 'P']) then begin
  389. Current := 1 + Len;
  390. PM := True;
  391. end
  392. else if (S[Current] = TimeSeparator) or (S[Current] = ' ') then
  393. Inc(Current)
  394. else
  395. raise EConvertError.Create('Invalid Time format');
  396. end ;
  397. end ;
  398. var
  399. i: integer;
  400. TimeValues: array[0..4] of integer;
  401. begin
  402. Current := 0;
  403. Len := length(s);
  404. PM := False;
  405. for i:=0 to 4 do
  406. timevalues[i]:=0;
  407. i := 0;
  408. TimeValues[i] := GetElement;
  409. while (i < 5) and (TimeValues[i] <> -1) do begin
  410. i := i + 1;
  411. TimeValues[i] := GetElement;
  412. end ;
  413. If (i<5) and (TimeValues[I]=-1) then
  414. TimeValues[I]:=0;
  415. if PM then
  416. begin
  417. if (TimeValues[0] <> 12) then
  418. Inc(TimeValues[0], 12);
  419. end
  420. else
  421. begin
  422. if (TimeValues[0]=12) then
  423. TimeValues[0]:=0;
  424. end;
  425. result := EncodeTime(TimeValues[0], TimeValues[1], TimeValues[2], TimeValues[3]);
  426. end ;
  427. { StrToDateTime converts the string S to a TDateTime value
  428. if S does not represent a valid date and time value
  429. an EConvertError will be raised }
  430. function StrToDateTime(const s: string): TDateTime;
  431. var i: integer;
  432. begin
  433. i := pos(' ', s);
  434. if i > 0 then result := ComposeDateTime(StrToDate(Copy(S, 1, i - 1)), StrToTime(Copy(S, i + 1, length(S))))
  435. else result := StrToDate(S);
  436. end ;
  437. { FormatDateTime formats DateTime to the given format string FormatStr }
  438. function FormatDateTime(FormatStr: string; DateTime: TDateTime): string;
  439. var
  440. ResultLen: integer;
  441. ResultBuffer: array[0..255] of char;
  442. ResultCurrent: pchar;
  443. procedure StoreStr(Str: pchar; Len: integer);
  444. begin
  445. if ResultLen + Len < SizeOf(ResultBuffer) then begin
  446. StrMove(ResultCurrent, Str, Len);
  447. ResultCurrent := ResultCurrent + Len;
  448. ResultLen := ResultLen + Len;
  449. end ;
  450. end ;
  451. procedure StoreString(const Str: string);
  452. var Len: integer;
  453. begin
  454. Len := Length(Str);
  455. if ResultLen + Len < SizeOf(ResultBuffer) then begin
  456. StrMove(ResultCurrent, pchar(Str), Len);
  457. ResultCurrent := ResultCurrent + Len;
  458. ResultLen := ResultLen + Len;
  459. end;
  460. end;
  461. procedure StoreInt(Value, Digits: integer);
  462. var S: string; Len: integer;
  463. begin
  464. S := IntToStr(Value);
  465. Len := Length(S);
  466. if Len < Digits then begin
  467. S := copy('0000', 1, Digits - Len) + S;
  468. Len := Digits;
  469. end ;
  470. StoreStr(pchar(@S[1]), Len);
  471. end ;
  472. Function TimeReFormat(Const S : string) : string;
  473. // Change m into n for time formatting.
  474. Var i : longint;
  475. begin
  476. Result:=S;
  477. For I:=1 to Length(Result) do
  478. If Result[i]='m' then
  479. result[i]:='n';
  480. end;
  481. var
  482. Year, Month, Day, DayOfWeek, Hour, Minute, Second, MilliSecond: word;
  483. procedure StoreFormat(const FormatStr: string);
  484. var
  485. Token,lastformattoken: char;
  486. FormatCurrent: pchar;
  487. FormatEnd: pchar;
  488. Count: integer;
  489. Clock12: boolean;
  490. P: pchar;
  491. tmp:integer;
  492. begin
  493. FormatCurrent := Pchar(FormatStr);
  494. FormatEnd := FormatCurrent + Length(FormatStr);
  495. Clock12 := false;
  496. P := FormatCurrent;
  497. while P < FormatEnd do begin
  498. Token := UpCase(P^);
  499. if Token in ['"', ''''] then begin
  500. P := P + 1;
  501. while (P < FormatEnd) and (P^ <> Token) do
  502. P := P + 1;
  503. end
  504. else if Token = 'A' then begin
  505. if (StrLIComp(P, 'A/P', 3) = 0) or
  506. (StrLIComp(P, 'AMPM', 4) = 0) or
  507. (StrLIComp(P, 'AM/PM', 5) = 0) then begin
  508. Clock12 := true;
  509. break;
  510. end ;
  511. end ;
  512. P := P + 1;
  513. end ;
  514. token:=#255;
  515. lastformattoken:=' ';
  516. while FormatCurrent < FormatEnd do
  517. begin
  518. Token := UpCase(FormatCurrent^);
  519. Count := 1;
  520. P := FormatCurrent + 1;
  521. case Token of
  522. '''', '"': begin
  523. while (P < FormatEnd) and (p^ <> Token) do
  524. P := P + 1;
  525. P := P + 1;
  526. Count := P - FormatCurrent;
  527. StoreStr(FormatCurrent + 1, Count - 2);
  528. end ;
  529. 'A': begin
  530. if StrLIComp(FormatCurrent, 'AMPM', 4) = 0 then begin
  531. Count := 4;
  532. if Hour < 12 then StoreString(TimeAMString)
  533. else StoreString(TimePMString);
  534. end
  535. else if StrLIComp(FormatCurrent, 'AM/PM', 5) = 0 then begin
  536. Count := 5;
  537. if Hour < 12 then StoreStr('am', 2)
  538. else StoreStr('pm', 2);
  539. end
  540. else if StrLIComp(FormatCurrent, 'A/P', 3) = 0 then begin
  541. Count := 3;
  542. if Hour < 12 then StoreStr('a', 1)
  543. else StoreStr('p', 1);
  544. end
  545. else
  546. Raise EConvertError.Create('Illegal character in format string');
  547. end ;
  548. '/': StoreStr(@DateSeparator, 1);
  549. ':': StoreStr(@TimeSeparator, 1);
  550. ' ', 'C', 'D', 'H', 'M', 'N', 'S', 'T', 'Y','Z' :
  551. begin
  552. while (P < FormatEnd) and (UpCase(P^) = Token) do
  553. P := P + 1;
  554. Count := P - FormatCurrent;
  555. case Token of
  556. ' ': StoreStr(FormatCurrent, Count);
  557. 'Y': begin
  558. if Count>2 then
  559. StoreInt(Year, 4)
  560. else
  561. StoreInt(Year mod 100, 2);
  562. end;
  563. 'M': begin
  564. if lastformattoken='H' then
  565. begin
  566. if Count = 1 then
  567. StoreInt(Minute, 0)
  568. else
  569. StoreInt(Minute, 2);
  570. end
  571. else
  572. begin
  573. case Count of
  574. 1: StoreInt(Month, 0);
  575. 2: StoreInt(Month, 2);
  576. 3: StoreString(ShortMonthNames[Month]);
  577. 4: StoreString(LongMonthNames[Month]);
  578. end;
  579. end;
  580. end;
  581. 'D': begin
  582. case Count of
  583. 1: StoreInt(Day, 0);
  584. 2: StoreInt(Day, 2);
  585. 3: StoreString(ShortDayNames[DayOfWeek]);
  586. 4: StoreString(LongDayNames[DayOfWeek]);
  587. 5: StoreFormat(ShortDateFormat);
  588. 6: StoreFormat(LongDateFormat);
  589. end ;
  590. end ;
  591. 'H': begin
  592. if Clock12 then begin
  593. tmp:=hour mod 12;
  594. if tmp=0 then tmp:=12;
  595. if Count = 1 then StoreInt(tmp, 0)
  596. else StoreInt(tmp, 2);
  597. end
  598. else begin
  599. if Count = 1 then StoreInt(Hour, 0)
  600. else StoreInt(Hour, 2);
  601. end ;
  602. end ;
  603. 'N': begin
  604. if Count = 1 then StoreInt(Minute, 0)
  605. else StoreInt(Minute, 2);
  606. end ;
  607. 'S': begin
  608. if Count = 1 then StoreInt(Second, 0)
  609. else StoreInt(Second, 2);
  610. end ;
  611. 'Z': begin
  612. if Count = 1 then StoreInt(MilliSecond, 0)
  613. else StoreInt(MilliSecond, 3);
  614. end ;
  615. 'T': begin
  616. if Count = 1 then StoreFormat(timereformat(ShortTimeFormat))
  617. else StoreFormat(TimeReformat(LongTimeFormat));
  618. end ;
  619. 'C':
  620. begin
  621. StoreFormat(ShortDateFormat);
  622. if (Hour<>0) or (Minute<>0) or (Second<>0) then
  623. begin
  624. StoreString(' ');
  625. StoreFormat(TimeReformat(LongTimeFormat));
  626. end;
  627. end;
  628. end;
  629. lastformattoken:=token;
  630. end;
  631. else
  632. StoreStr(@Token, 1);
  633. end ;
  634. FormatCurrent := FormatCurrent + Count;
  635. end ;
  636. end ;
  637. begin
  638. DecodeDateFully(DateTime, Year, Month, Day, DayOfWeek);
  639. DecodeTime(DateTime, Hour, Minute, Second, MilliSecond);
  640. ResultLen := 0;
  641. ResultCurrent := @ResultBuffer[0];
  642. StoreFormat(FormatStr);
  643. ResultBuffer[ResultLen] := #0;
  644. result := StrPas(@ResultBuffer[0]);
  645. end ;
  646. { DateTimeToString formats DateTime to the given format in FormatStr }
  647. procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime);
  648. begin
  649. Result := FormatDateTime(FormatStr, DateTime);
  650. end ;
  651. Function DateTimeToFileDate(DateTime : TDateTime) : Longint;
  652. Var YY,MM,DD,H,m,s,msec : Word;
  653. begin
  654. Decodedate (DateTime,YY,MM,DD);
  655. DecodeTime (DateTime,h,m,s,msec);
  656. {$ifndef unix}
  657. If (YY<1980) or (YY>2099) then
  658. Result:=0
  659. else
  660. begin
  661. Result:=(s shr 1) or (m shl 5) or (h shl 11);
  662. Result:=Result or DD shl 16 or (MM shl 21) or ((YY-1980) shl 25);
  663. end;
  664. {$else unix}
  665. Result:=LocalToEpoch(yy,mm,dd,h,m,s);
  666. {$endif unix}
  667. end;
  668. function CurrentYear:Word;
  669. var yy,mm,dd : word;
  670. begin
  671. Decodedate(now,yy,mm,dd);
  672. Result:=yy;
  673. end;
  674. Function FileDateToDateTime (Filedate : Longint) : TDateTime;
  675. {$ifndef unix}
  676. Var Date,Time : Word;
  677. begin
  678. Date:=FileDate shr 16;
  679. Time:=FileDate and $ffff;
  680. Result:=ComposeDateTime(EncodeDate((Date shr 9) + 1980,(Date shr 5) and 15, Date and 31),
  681. EncodeTime(Time shr 11, (Time shr 5) and 63, (Time and 31) shl 1,0));
  682. end;
  683. {$else unix}
  684. var
  685. y, mon, d, h, min, s: word;
  686. begin
  687. EpochToLocal(FileDate,y,mon,d,h,min,s);
  688. Result:=ComposeDateTime(EncodeDate(y,mon,d),EncodeTime(h,min,s,0));
  689. end;
  690. {$endif unix}
  691. // ieuw. These should be written to work without exceptions?
  692. function TryStrToDate(const S: string; out Value: TDateTime): Boolean;
  693. begin
  694. result:=true;
  695. try
  696. value:=StrToDate(s);
  697. except
  698. on EConvertError do
  699. result:=false
  700. end;
  701. end;
  702. // function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  703. function TryStrToTime(const S: string; out Value: TDateTime): Boolean;
  704. begin
  705. result:=true;
  706. try
  707. value:=StrToTime(s);
  708. except
  709. on EConvertError do
  710. result:=false
  711. end;
  712. end;
  713. // function TryStrToTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  714. function TryStrToDateTime(const S: string; out Value: TDateTime): Boolean;
  715. begin
  716. result:=true;
  717. try
  718. value:=StrToDateTime(s);
  719. except
  720. on EConvertError do
  721. result:=false
  722. end;
  723. end;
  724. // function TryStrToDateTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  725. function StrToDateDef(const S: string; const Defvalue : TDateTime): TDateTime;
  726. begin
  727. if not TryStrToDate(s,Result) Then
  728. result:=defvalue;
  729. end;
  730. function StrToTimeDef(const S: string; const Defvalue : TDateTime): TDateTime;
  731. begin
  732. if not TryStrToTime(s,Result) Then
  733. result:=defvalue;
  734. end;
  735. function StrToDateTimeDef(const S: string; const Defvalue : TDateTime): TDateTime;
  736. begin
  737. if not TryStrToDateTime(s,Result) Then
  738. result:=defvalue;
  739. end;
  740. procedure ReplaceTime(var dati:TDateTime; NewTime : TDateTime);inline;
  741. begin
  742. dati:=trunc(dati)+frac(newtime);
  743. end;