dati.inc 33 KB

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