dati.inc 31 KB

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