dati.inc 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182
  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) - Abs(frac(Time))
  42. else Result := trunc(Date) + Abs(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:= msecs-comp(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. Year, Month, Day : word;
  215. begin
  216. DecodeDate(DateTime, Year, Month, Day);
  217. IncAMonth(Year, Month, Day, NumberOfMonths);
  218. result := ComposeDateTime(DoEncodeDate(Year, Month, Day), DateTime);
  219. end ;
  220. { IncAMonth is the same as IncMonth, but operates on decoded date }
  221. procedure IncAMonth(var Year, Month, Day: Word; NumberOfMonths: Integer = 1);
  222. var
  223. TempMonth, S: Integer;
  224. begin
  225. If NumberOfMonths>=0 then
  226. s:=1
  227. else
  228. s:=-1;
  229. inc(Year,(NumberOfMonths div 12));
  230. TempMonth:=Month+(NumberOfMonths mod 12)-1;
  231. if (TempMonth>11) or
  232. (TempMonth<0) then
  233. begin
  234. Dec(TempMonth, S*12);
  235. Inc(Year, S);
  236. end;
  237. Month:=TempMonth+1; { Months from 1 to 12 }
  238. If (Day>MonthDays[IsLeapYear(Year)][Month]) then
  239. Day:=MonthDays[IsLeapYear(Year)][Month];
  240. end;
  241. { IsLeapYear returns true if Year is a leap year }
  242. function IsLeapYear(Year: Word): boolean;
  243. begin
  244. Result := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
  245. end;
  246. { DateToStr returns a string representation of Date using ShortDateFormat }
  247. function DateToStr(Date: TDateTime): string;
  248. begin
  249. result := FormatDateTime('ddddd', Date);
  250. end ;
  251. { TimeToStr returns a string representation of Time using LongTimeFormat }
  252. function TimeToStr(Time: TDateTime): string;
  253. begin
  254. result := FormatDateTime('tt', Time);
  255. end ;
  256. { DateTimeToStr returns a string representation of DateTime using LongDateTimeFormat }
  257. function DateTimeToStr(DateTime: TDateTime): string;
  258. begin
  259. result := FormatDateTime('c', DateTime);
  260. end ;
  261. { StrToDate converts the string S to a TDateTime value
  262. if S does not represent a valid date value
  263. an EConvertError will be raised }
  264. function IntStrToDate(Out ErrorMsg : AnsiString; const S: PChar; Len : integer; const useformat : string; const defs:TFormatSettings; separator : char = #0): TDateTime;
  265. const SInvalidDateFormat = '"%s" is not a valid date format';
  266. procedure FixErrorMsg(const errm :ansistring;const errmarg : ansistring);
  267. begin
  268. errormsg:=format(errm,[errmarg]);
  269. end;
  270. var
  271. df:string;
  272. d,m,y,ly:word;
  273. n,i:longint;
  274. c:word;
  275. dp,mp,yp,which : Byte;
  276. s1:string[4];
  277. values:array[1..3] of longint;
  278. LocalTime:tsystemtime;
  279. YearMoreThenTwoDigits : boolean;
  280. begin
  281. ErrorMsg:=''; Result:=0;
  282. if (Len=0) then
  283. begin
  284. FixErrorMsg(SInvalidDateFormat,'');
  285. exit;
  286. end;
  287. YearMoreThenTwoDigits := False;
  288. if separator = #0 then
  289. separator := defs.DateSeparator;
  290. df := UpperCase(useFormat);
  291. { Determine order of D,M,Y }
  292. yp:=0;
  293. mp:=0;
  294. dp:=0;
  295. Which:=0;
  296. i:=0;
  297. while (i<Length(df)) and (Which<3) do
  298. begin
  299. inc(i);
  300. Case df[i] of
  301. 'Y' :
  302. if yp=0 then
  303. begin
  304. Inc(Which);
  305. yp:=which;
  306. end;
  307. 'M' :
  308. if mp=0 then
  309. begin
  310. Inc(Which);
  311. mp:=which;
  312. end;
  313. 'D' :
  314. if dp=0 then
  315. begin
  316. Inc(Which);
  317. dp:=which;
  318. end;
  319. end;
  320. end;
  321. if Which<>3 then
  322. begin
  323. FixErrorMsg(SErrIllegalDateFormatString,useformat);
  324. Exit;
  325. end;
  326. { Get actual values }
  327. for i := 1 to 3 do
  328. values[i] := 0;
  329. s1 := '';
  330. n := 0;
  331. dec(len);
  332. for i := 0 to len do
  333. begin
  334. if s[i] in ['0'..'9'] then
  335. s1 := s1 + s[i];
  336. { space can be part of the shortdateformat, and is defaultly in slovak
  337. windows, therefor it shouldn't be taken as separator (unless so specified)
  338. and ignored }
  339. if (Separator <> ' ') and (s[i] = ' ') then
  340. Continue;
  341. if (s[i] = separator) or ((i = len) and (s[i] in ['0'..'9'])) then
  342. begin
  343. inc(n);
  344. if n>3 then
  345. begin
  346. FixErrorMsg(SInvalidDateFormat,s);
  347. exit;
  348. end;
  349. // Check if the year has more then two digits (if n=yp, then we are evaluating the year.)
  350. if (n=yp) and (length(s1)>2) then YearMoreThenTwoDigits := True;
  351. val(s1, values[n], c);
  352. if c<>0 then
  353. begin
  354. FixErrorMsg(SInvalidDateFormat,s);
  355. Exit;
  356. end;
  357. s1 := '';
  358. end
  359. else if not (s[i] in ['0'..'9']) then
  360. begin
  361. FixErrorMsg(SInvalidDateFormat,s);
  362. Exit;
  363. end;
  364. end ;
  365. // Fill in values.
  366. getLocalTime(LocalTime);
  367. ly := LocalTime.Year;
  368. If N=3 then
  369. begin
  370. y:=values[yp];
  371. m:=values[mp];
  372. d:=values[dp];
  373. end
  374. Else
  375. begin
  376. Y:=ly;
  377. If n<2 then
  378. begin
  379. d:=values[1];
  380. m := LocalTime.Month;
  381. end
  382. else
  383. If dp<mp then
  384. begin
  385. d:=values[1];
  386. m:=values[2];
  387. end
  388. else
  389. begin
  390. d:=values[2];
  391. m:=values[1];
  392. end;
  393. end;
  394. if (y >= 0) and (y < 100) and not YearMoreThenTwoDigits then
  395. begin
  396. ly := ly - defs.TwoDigitYearCenturyWindow;
  397. Inc(Y, ly div 100 * 100);
  398. if (defs.TwoDigitYearCenturyWindow > 0) and (Y < ly) then
  399. Inc(Y, 100);
  400. end;
  401. Result := EncodeDate(y, m, d);
  402. end;
  403. function StrToDate(const S: PChar; Len : integer; const useformat : string; separator : char = #0): TDateTime;
  404. Var
  405. MSg : AnsiString;
  406. begin
  407. Result:=IntStrToDate(Msg,S,Len,useFormat,DefaultFormatSettings,Separator);
  408. If (Msg<>'') then
  409. Raise EConvertError.Create(Msg);
  410. end;
  411. function StrToDate(const S: ShortString; const useformat : string; separator : char = #0): TDateTime;
  412. begin
  413. result := StrToDate(@S[1],Length(s),UseFormat,separator);
  414. end;
  415. function StrToDate(const S: AnsiString; const useformat : string; separator : char = #0): TDateTime;
  416. begin
  417. result := StrToDate(@S[1],Length(s),UseFormat,separator);
  418. end;
  419. function StrToDate(const S: ShortString; separator : char): TDateTime;
  420. begin
  421. result := StrToDate(@S[1],Length(s),ShortDateFormat,separator)
  422. end;
  423. function StrToDate(const S: ShortString): TDateTime;
  424. begin
  425. result := StrToDate(@S[1],Length(s),ShortDateFormat,#0);
  426. end;
  427. function StrToDate(const S: AnsiString; separator : char): TDateTime;
  428. begin
  429. result := StrToDate(@S[1],Length(s),ShortDateFormat,separator)
  430. end;
  431. function StrToDate(const S: AnsiString): TDateTime;
  432. begin
  433. result := StrToDate(@S[1],Length(s),ShortDateFormat,#0);
  434. end;
  435. { StrToTime converts the string S to a TDateTime value
  436. if S does not represent a valid time value an
  437. EConvertError will be raised }
  438. function IntStrToTime(Out ErrorMsg : AnsiString; const S: PChar; Len : integer;const defs:TFormatSettings; separator : char = #0): TDateTime;
  439. var
  440. Current: integer; PM: integer;
  441. function StrPas(Src : PChar; len: integer = 0) : ShortString;
  442. var
  443. tmp : integer;
  444. begin
  445. {tmp := IndexChar(Src[0], len, #0);
  446. len :=ifthen(tmp >= 0, tmp, len);
  447. len :=ifthen(len > 255, 255, len);}
  448. SetLength(Result, len);
  449. move(src[0],result[1],len);
  450. end;
  451. function GetElement: integer;
  452. var
  453. j, c: integer;
  454. CurrentChar : Char;
  455. begin
  456. result := -1;
  457. while (result = -1) and (Current < Len) do
  458. begin
  459. CurrentChar := S[Current];
  460. if CurrentChar in ['0'..'9'] then
  461. begin
  462. j := Current;
  463. while (Current+1 < Len) and (s[Current + 1] in ['0'..'9']) do
  464. Inc(Current);
  465. val(StrPas(S+j, 1 + current - j), result, c);
  466. end
  467. else if ((defs.TimeAMString<>'') and (CurrentChar = defs.TimeAMString[1])) or (S[Current] in ['a', 'A']) then
  468. begin
  469. pm:=1;
  470. Current := 1 + Len;
  471. end
  472. else if ((defs.TimePMString<>'') and (CurrentChar = defs.TimePMString[1])) or (S[Current] in ['p', 'P']) then
  473. begin
  474. Current := 1 + Len;
  475. PM := 2;
  476. end
  477. else if (CurrentChar = Separator) or (CurrentChar = ' ') then
  478. Inc(Current)
  479. else
  480. ErrorMsg:=Format(SErrInvalidTimeFormat,[StrPas(S)]);
  481. end ;
  482. end ;
  483. var
  484. i: integer;
  485. TimeValues: array[0..4] of integer;
  486. begin
  487. if separator = #0 then
  488. separator := defs.TimeSeparator;
  489. Current := 0;
  490. PM := 0;
  491. for i:=0 to 4 do
  492. timevalues[i]:=0;
  493. i := 0;
  494. TimeValues[i] := GetElement;
  495. If ErrorMsg<>'' then
  496. Exit;
  497. while (i < 5) and (TimeValues[i] <> -1) do
  498. begin
  499. i := i + 1;
  500. Inc(Current);
  501. TimeValues[i] := GetElement;
  502. If ErrorMsg<>'' then
  503. Exit;
  504. end ;
  505. If (i<5) and (TimeValues[I]=-1) then
  506. TimeValues[I]:=0;
  507. if PM=2 then
  508. begin
  509. if (TimeValues[0] <> 12) then
  510. Inc(TimeValues[0], 12);
  511. end
  512. else
  513. begin
  514. if (pm=1) and ((TimeValues[0]=12)) then
  515. TimeValues[0]:=0;
  516. end;
  517. result := EncodeTime(TimeValues[0], TimeValues[1], TimeValues[2], TimeValues[3]);
  518. end ;
  519. function StrToTime(const S: PChar; Len : integer; separator : char = #0): TDateTime;
  520. Var
  521. Msg : AnsiString;
  522. begin
  523. Result:=IntStrToTime(Msg,S,Len,DefaultFormatSettings,Separator);
  524. If (Msg<>'') then
  525. Raise EConvertError.Create(Msg);
  526. end;
  527. function StrToTime(const s: ShortString; separator : char): TDateTime;
  528. begin
  529. result := StrToTime(@s[1], length(s), separator);
  530. end;
  531. function StrToTime(const s: AnsiString; separator : char): TDateTime;
  532. begin
  533. result := StrToTime(@s[1], length(s), separator);
  534. end;
  535. function StrToTime(const s: ShortString): TDateTime;
  536. begin
  537. result := StrToTime(@s[1], length(s), #0);
  538. end;
  539. function StrToTime(const s: AnsiString): TDateTime;
  540. begin
  541. result := StrToTime(@s[1], length(s), #0);
  542. end;
  543. { StrToDateTime converts the string S to a TDateTime value
  544. if S does not represent a valid date and/or time value
  545. an EConvertError will be raised }
  546. function StrToDateTime(const s: string): TDateTime;
  547. var
  548. I: integer;
  549. begin
  550. I:=Pos(TimeSeparator,S);
  551. If (I>0) then
  552. begin
  553. While (I>0) and (S[I]<>' ') do
  554. Dec(I);
  555. If I>0 then
  556. result:=ComposeDateTime(StrToDate(Copy(S,1,I-1)),StrToTime(Copy(S,i+1, Length(S)-i)))
  557. else
  558. result:=StrToTime(S)
  559. end
  560. else
  561. Result:=StrToDate(S);
  562. end;
  563. function StrToDateTime(const s: AnsiString; const UseFormat : TFormatSettings): TDateTime;
  564. var
  565. I: integer;
  566. begin
  567. I:=Pos(TimeSeparator,S);
  568. If (I>0) then
  569. begin
  570. While (I>0) and (S[I]<>' ') do
  571. Dec(I);
  572. If I>0 then
  573. result:=ComposeDateTime(StrToDate(Copy(S,1,I-1),UseFormat.ShortDateFormat,UseFormat.DateSeparator),
  574. StrToTime(Copy(S,i+1, Length(S)-i),UseFormat.TimeSeparator))
  575. else
  576. result:=StrToTime(S,UseFormat.TimeSeparator)
  577. end
  578. else
  579. Result:=StrToDate(S,UseFormat.ShortDateFormat,UseFormat.DateSeparator);
  580. end;
  581. function StrToDateTime(const s: ShortString; const UseFormat : TFormatSettings): TDateTime;
  582. var
  583. I: integer;
  584. begin
  585. I:=Pos(TimeSeparator,S);
  586. If (I>0) then
  587. begin
  588. While (I>0) and (S[I]<>' ') do
  589. Dec(I);
  590. If I>0 then
  591. result:=ComposeDateTime(StrToDate(Copy(S,1,I-1),UseFormat.ShortDateFormat,UseFormat.DateSeparator),
  592. StrToTime(Copy(S,i+1, Length(S)-i),UseFormat.TimeSeparator))
  593. else
  594. result:=StrToTime(S,UseFormat.TimeSeparator)
  595. end
  596. else
  597. Result:=StrToDate(S,UseFormat.ShortDateFormat,UseFormat.DateSeparator);
  598. end;
  599. { FormatDateTime formats DateTime to the given format string FormatStr }
  600. function FormatDateTime(FormatStr: string; DateTime: TDateTime): string;
  601. var
  602. ResultLen: integer;
  603. ResultBuffer: array[0..255] of char;
  604. ResultCurrent: pchar;
  605. procedure StoreStr(Str: pchar; Len: integer);
  606. begin
  607. if ResultLen + Len < SizeOf(ResultBuffer) then begin
  608. StrMove(ResultCurrent, Str, Len);
  609. ResultCurrent := ResultCurrent + Len;
  610. ResultLen := ResultLen + Len;
  611. end ;
  612. end ;
  613. procedure StoreString(const Str: string);
  614. var Len: integer;
  615. begin
  616. Len := Length(Str);
  617. if ResultLen + Len < SizeOf(ResultBuffer) then begin // strmove not safe
  618. StrMove(ResultCurrent, pchar(Str), Len);
  619. ResultCurrent := ResultCurrent + Len;
  620. ResultLen := ResultLen + Len;
  621. end;
  622. end;
  623. procedure StoreInt(Value, Digits: integer);
  624. var S: string; Len: integer;
  625. begin
  626. S := IntToStr(Value);
  627. Len := Length(S);
  628. if Len < Digits then begin
  629. S := copy('0000', 1, Digits - Len) + S;
  630. Len := Digits;
  631. end ;
  632. StoreStr(pchar(@S[1]), Len);
  633. end ;
  634. var
  635. Year, Month, Day, DayOfWeek, Hour, Minute, Second, MilliSecond: word;
  636. procedure StoreFormat(const FormatStr: string);
  637. var
  638. Token,lastformattoken: char;
  639. FormatCurrent: pchar;
  640. FormatEnd: pchar;
  641. Count: integer;
  642. Clock12: boolean;
  643. P: pchar;
  644. tmp:integer;
  645. begin
  646. FormatCurrent := Pchar(pointer(FormatStr));
  647. FormatEnd := FormatCurrent + Length(FormatStr);
  648. Clock12 := false;
  649. P := FormatCurrent;
  650. while P < FormatEnd do begin
  651. Token := UpCase(P^);
  652. if Token in ['"', ''''] then begin
  653. P := P + 1;
  654. while (P < FormatEnd) and (P^ <> Token) do
  655. P := P + 1;
  656. end
  657. else if Token = 'A' then begin
  658. if (StrLIComp(P, 'A/P', 3) = 0) or
  659. (StrLIComp(P, 'AMPM', 4) = 0) or
  660. (StrLIComp(P, 'AM/PM', 5) = 0) then begin
  661. Clock12 := true;
  662. break;
  663. end ;
  664. end ;
  665. P := P + 1;
  666. end ;
  667. token:=#255;
  668. lastformattoken:=' ';
  669. while FormatCurrent < FormatEnd do
  670. begin
  671. Token := UpCase(FormatCurrent^);
  672. Count := 1;
  673. P := FormatCurrent + 1;
  674. case Token of
  675. '''', '"': begin
  676. while (P < FormatEnd) and (p^ <> Token) do
  677. P := P + 1;
  678. P := P + 1;
  679. Count := P - FormatCurrent;
  680. StoreStr(FormatCurrent + 1, Count - 2);
  681. end ;
  682. 'A': begin
  683. if StrLIComp(FormatCurrent, 'AMPM', 4) = 0 then begin
  684. Count := 4;
  685. if Hour < 12 then StoreString(TimeAMString)
  686. else StoreString(TimePMString);
  687. end
  688. else if StrLIComp(FormatCurrent, 'AM/PM', 5) = 0 then begin
  689. Count := 5;
  690. if Hour < 12 then StoreStr('am', 2)
  691. else StoreStr('pm', 2);
  692. end
  693. else if StrLIComp(FormatCurrent, 'A/P', 3) = 0 then begin
  694. Count := 3;
  695. if Hour < 12 then StoreStr('a', 1)
  696. else StoreStr('p', 1);
  697. end
  698. else
  699. Raise EConvertError.Create('Illegal character in format string');
  700. end ;
  701. '/': StoreStr(@DateSeparator, 1);
  702. ':': StoreStr(@TimeSeparator, 1);
  703. ' ', 'C', 'D', 'H', 'M', 'N', 'S', 'T', 'Y','Z' :
  704. begin
  705. while (P < FormatEnd) and (UpCase(P^) = Token) do
  706. P := P + 1;
  707. Count := P - FormatCurrent;
  708. case Token of
  709. ' ': StoreStr(FormatCurrent, Count);
  710. 'Y': begin
  711. if Count>2 then
  712. StoreInt(Year, 4)
  713. else
  714. StoreInt(Year mod 100, 2);
  715. end;
  716. 'M': begin
  717. if lastformattoken='H' then
  718. begin
  719. if Count = 1 then
  720. StoreInt(Minute, 0)
  721. else
  722. StoreInt(Minute, 2);
  723. end
  724. else
  725. begin
  726. case Count of
  727. 1: StoreInt(Month, 0);
  728. 2: StoreInt(Month, 2);
  729. 3: StoreString(ShortMonthNames[Month]);
  730. 4: StoreString(LongMonthNames[Month]);
  731. end;
  732. end;
  733. end;
  734. 'D': begin
  735. case Count of
  736. 1: StoreInt(Day, 0);
  737. 2: StoreInt(Day, 2);
  738. 3: StoreString(ShortDayNames[DayOfWeek]);
  739. 4: StoreString(LongDayNames[DayOfWeek]);
  740. 5: StoreFormat(ShortDateFormat);
  741. 6: StoreFormat(LongDateFormat);
  742. end ;
  743. end ;
  744. 'H': begin
  745. if Clock12 then begin
  746. tmp:=hour mod 12;
  747. if tmp=0 then tmp:=12;
  748. if Count = 1 then StoreInt(tmp, 0)
  749. else StoreInt(tmp, 2);
  750. end
  751. else begin
  752. if Count = 1 then StoreInt(Hour, 0)
  753. else StoreInt(Hour, 2);
  754. end ;
  755. end ;
  756. 'N': begin
  757. if Count = 1 then StoreInt(Minute, 0)
  758. else StoreInt(Minute, 2);
  759. end ;
  760. 'S': begin
  761. if Count = 1 then StoreInt(Second, 0)
  762. else StoreInt(Second, 2);
  763. end ;
  764. 'Z': begin
  765. if Count = 1 then StoreInt(MilliSecond, 0)
  766. else StoreInt(MilliSecond, 3);
  767. end ;
  768. 'T': begin
  769. if Count = 1 then StoreFormat(ShortTimeFormat)
  770. else StoreFormat(LongTimeFormat);
  771. end ;
  772. 'C':
  773. begin
  774. StoreFormat(ShortDateFormat);
  775. if (Hour<>0) or (Minute<>0) or (Second<>0) then
  776. begin
  777. StoreString(' ');
  778. StoreFormat(LongTimeFormat);
  779. end;
  780. end;
  781. end;
  782. lastformattoken:=token;
  783. end;
  784. else
  785. StoreStr(@Token, 1);
  786. end ;
  787. FormatCurrent := FormatCurrent + Count;
  788. end ;
  789. end ;
  790. begin
  791. DecodeDateFully(DateTime, Year, Month, Day, DayOfWeek);
  792. DecodeTime(DateTime, Hour, Minute, Second, MilliSecond);
  793. ResultLen := 0;
  794. ResultCurrent := @ResultBuffer[0];
  795. StoreFormat(FormatStr);
  796. ResultBuffer[ResultLen] := #0;
  797. result := StrPas(@ResultBuffer[0]);
  798. end ;
  799. { DateTimeToString formats DateTime to the given format in FormatStr }
  800. procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime);
  801. begin
  802. Result := FormatDateTime(FormatStr, DateTime);
  803. end ;
  804. Function DateTimeToFileDate(DateTime : TDateTime) : Longint;
  805. Var YY,MM,DD,H,m,s,msec : Word;
  806. begin
  807. Decodedate (DateTime,YY,MM,DD);
  808. DecodeTime (DateTime,h,m,s,msec);
  809. {$ifndef unix}
  810. If (YY<1980) or (YY>2099) then
  811. Result:=0
  812. else
  813. begin
  814. Result:=(s shr 1) or (m shl 5) or (h shl 11);
  815. Result:=Result or longint(DD shl 16 or (MM shl 21) or (word(YY-1980) shl 25));
  816. end;
  817. {$else unix}
  818. Result:=LocalToEpoch(yy,mm,dd,h,m,s);
  819. {$endif unix}
  820. end;
  821. function CurrentYear:Word;
  822. var yy,mm,dd : word;
  823. begin
  824. Decodedate(now,yy,mm,dd);
  825. Result:=yy;
  826. end;
  827. Function FileDateToDateTime (Filedate : Longint) : TDateTime;
  828. {$ifndef unix}
  829. Var Date,Time : Word;
  830. begin
  831. Date:=FileDate shr 16;
  832. Time:=FileDate and $ffff;
  833. Result:=ComposeDateTime(EncodeDate((Date shr 9) + 1980,(Date shr 5) and 15, Date and 31),
  834. EncodeTime(Time shr 11, (Time shr 5) and 63, (Time and 31) shl 1,0));
  835. end;
  836. {$else unix}
  837. var
  838. y, mon, d, h, min, s: word;
  839. begin
  840. EpochToLocal(FileDate,y,mon,d,h,min,s);
  841. Result:=ComposeDateTime(EncodeDate(y,mon,d),EncodeTime(h,min,s,0));
  842. end;
  843. {$endif unix}
  844. function TryStrToDate(const S: ShortString; out Value: TDateTime): Boolean;
  845. begin
  846. result := TryStrToDate(S, Value, #0);
  847. end;
  848. function TryStrToDate(const S: ShortString; out Value: TDateTime;
  849. const useformat : string; separator : char = #0): Boolean;
  850. Var
  851. Msg : Ansistring;
  852. begin
  853. Value:=IntStrToDate(Msg,@S[1],Length(S),useformat,defaultformatsettings,separator);
  854. Result:=(Msg='');
  855. end;
  856. function TryStrToDate(const S: AnsiString; out Value: TDateTime;
  857. const useformat : string; separator : char = #0): Boolean;
  858. Var
  859. Msg : Ansistring;
  860. begin
  861. Result:=Length(S)<>0;
  862. If Result then
  863. begin
  864. Value:=IntStrToDate(Msg,@S[1],Length(S),useformat,DefaultFormatSettings,Separator);
  865. Result:=(Msg='');
  866. end;
  867. end;
  868. function TryStrToDate(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
  869. begin
  870. Result:=TryStrToDate(S,Value,ShortDateFormat,Separator);
  871. end;
  872. function TryStrToDate(const S: AnsiString; out Value: TDateTime): Boolean;
  873. begin
  874. Result:=TryStrToDate(S,Value,ShortDateFormat,#0);
  875. end;
  876. function TryStrToDate(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
  877. begin
  878. Result:=TryStrToDate(S,Value,ShortDateFormat,Separator);
  879. end;
  880. function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  881. Var
  882. Msg : Ansistring;
  883. begin
  884. Result:=Length(S)<>0;
  885. If Result then
  886. begin
  887. Value:=IntStrToDate(Msg,@S[1],Length(S),FormatSettings.ShortDateFormat,FormatSettings,#0);
  888. Result:=(Msg='');
  889. end;
  890. end;
  891. function TryStrToTime(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
  892. Var
  893. Msg : AnsiString;
  894. begin
  895. Value:=IntStrToTime(Msg,@S[1],Length(S),DefaultFormatSettings,Separator);
  896. result:=(Msg='');
  897. end;
  898. function TryStrToTime(const S: ShortString; out Value: TDateTime): Boolean;
  899. begin
  900. Result := TryStrToTime(S,Value,#0);
  901. end;
  902. function TryStrToTime(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
  903. Var
  904. Msg : AnsiString;
  905. begin
  906. Result:=Length(S)<>0;
  907. If Result then
  908. begin
  909. Value:=IntStrToTime(Msg,@S[1],Length(S),DefaultFormatSettings,Separator);
  910. Result:=(Msg='');
  911. end;
  912. end;
  913. function TryStrToTime(const S: AnsiString; out Value: TDateTime): Boolean;
  914. begin
  915. result := TryStrToTime(S,Value,#0);
  916. end;
  917. function TryStrToTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  918. Var msg : AnsiString;
  919. begin
  920. Result:=Length(S)<>0;
  921. If Result then
  922. begin
  923. Value:=IntStrToTime(Msg,@S[1],Length(S),FormatSettings,#0);
  924. Result:=(Msg='');
  925. end;
  926. end;
  927. function TryStrToDateTime(const S: ShortString; out Value: TDateTime): Boolean;
  928. begin
  929. result:=true;
  930. try
  931. value:=StrToDateTime(s);
  932. except
  933. on EConvertError do
  934. result:=false
  935. end;
  936. end;
  937. function TryStrToDateTime(const S: AnsiString; out Value: TDateTime): Boolean;
  938. begin
  939. result:=true;
  940. try
  941. value:=StrToDateTime(s);
  942. except
  943. on EConvertError do
  944. result:=false
  945. end;
  946. end;
  947. function TryStrToDateTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  948. var
  949. I: integer;
  950. dtdate, dttime :TDateTime;
  951. begin
  952. result:=true;
  953. I:=Pos(FormatSettings.TimeSeparator,S);
  954. If (I>0) then
  955. begin
  956. While (I>0) and (S[I]<>' ') do
  957. Dec(I);
  958. If I>0 then
  959. begin
  960. if not TryStrToDate(Copy(S,1,I-1),dtdate,Formatsettings) then
  961. exit;
  962. if not TryStrToTime(Copy(S,i+1, Length(S)-i),dttime,Formatsettings) then
  963. exit;
  964. Value:=ComposeDateTime(dtdate,dttime);
  965. end
  966. else
  967. result:=TryStrToTime(s,Value,Formatsettings);
  968. end
  969. else
  970. result:=TryStrToDate(s,Value,Formatsettings);
  971. end;
  972. function StrToDateDef(const S: ShortString; const Defvalue : TDateTime): TDateTime;
  973. begin
  974. result := StrToDateDef(S,DefValue,#0);
  975. end;
  976. function StrToTimeDef(const S: ShortString; const Defvalue : TDateTime): TDateTime;
  977. begin
  978. result := StrToTimeDef(S,DefValue,#0);
  979. end;
  980. function StrToDateTimeDef(const S: ShortString; const Defvalue : TDateTime): TDateTime;
  981. begin
  982. if not TryStrToDateTime(s,Result) Then
  983. result:=defvalue;
  984. end;
  985. function StrToDateDef(const S: ShortString; const Defvalue : TDateTime; separator : char): TDateTime;
  986. begin
  987. if not TryStrToDate(s,Result, separator) Then
  988. result:=defvalue;
  989. end;
  990. function StrToTimeDef(const S: ShortString; const Defvalue : TDateTime; separator : char): TDateTime;
  991. begin
  992. if not TryStrToTime(s,Result, separator) Then
  993. result:=defvalue;
  994. end;
  995. function StrToDateDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime;
  996. begin
  997. result := StrToDateDef(S,DefValue,#0);
  998. end;
  999. function StrToTimeDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime;
  1000. begin
  1001. result := StrToTimeDef(S,DefValue,#0);
  1002. end;
  1003. function StrToDateTimeDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime;
  1004. begin
  1005. if not TryStrToDateTime(s,Result) Then
  1006. result:=defvalue;
  1007. end;
  1008. function StrToDateDef(const S: AnsiString; const Defvalue : TDateTime; separator : char): TDateTime;
  1009. begin
  1010. if not TryStrToDate(s,Result, separator) Then
  1011. result:=defvalue;
  1012. end;
  1013. function StrToTimeDef(const S: AnsiString; const Defvalue : TDateTime; separator : char): TDateTime;
  1014. begin
  1015. if not TryStrToTime(s,Result, separator) Then
  1016. result:=defvalue;
  1017. end;
  1018. procedure ReplaceTime(var dati:TDateTime; NewTime : TDateTime);inline;
  1019. begin
  1020. dati:= ComposeDateTime(dati, newtime);
  1021. end;
  1022. procedure ReplaceDate(var DateTime: TDateTime; const NewDate: TDateTime); inline;
  1023. var
  1024. tmp : TDateTime;
  1025. begin
  1026. tmp:=NewDate;
  1027. ReplaceTime(tmp,DateTime);
  1028. DateTime:=tmp;
  1029. end;