dati.inc 24 KB

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