dati.inc 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416
  1. {
  2. *********************************************************************
  3. Copyright (C) 1997, 1998 Gertjan Schouten
  4. See the file COPYING.FPC, included in this distribution,
  5. for details about the copyright.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  9. **********************************************************************
  10. System Utilities For Free Pascal
  11. }
  12. {==============================================================================}
  13. { internal functions }
  14. {==============================================================================}
  15. Function DoEncodeDate(Year, Month, Day: Word): longint;
  16. Var
  17. D : TDateTime;
  18. begin
  19. If TryEncodeDate(Year,Month,Day,D) then
  20. Result:=Trunc(D)
  21. else
  22. Result:=0;
  23. end;
  24. function DoEncodeTime(Hour, Minute, Second, MilliSecond: word): TDateTime;
  25. begin
  26. If not TryEncodeTime(Hour,Minute,Second,MilliSecond,Result) then
  27. Result:=0;
  28. end;
  29. {==============================================================================}
  30. { Public functions }
  31. {==============================================================================}
  32. { ComposeDateTime converts a Date and a Time into one TDateTime }
  33. function ComposeDateTime(Date,Time : TDateTime) : TDateTime;
  34. begin
  35. if Date < 0 then Result := trunc(Date) - Abs(frac(Time))
  36. else Result := trunc(Date) + Abs(frac(Time));
  37. end;
  38. { DateTimeToTimeStamp converts DateTime to a TTimeStamp }
  39. function DateTimeToTimeStamp(DateTime: TDateTime): TTimeStamp;
  40. Var
  41. D : Double;
  42. begin
  43. D:=DateTime * Single(MSecsPerDay);
  44. if D<0 then
  45. D:=D-0.5
  46. else
  47. D:=D+0.5;
  48. result.Time := Abs(Trunc(D)) Mod MSecsPerDay;
  49. result.Date := DateDelta + Trunc(D) div MSecsPerDay;
  50. end;
  51. { TimeStampToDateTime converts TimeStamp to a TDateTime value }
  52. function TimeStampToDateTime(const TimeStamp: TTimeStamp): TDateTime;
  53. begin
  54. Result := ComposeDateTime(TimeStamp.Date - DateDelta,TimeStamp.Time / MSecsPerDay)
  55. end;
  56. { MSecsToTimeStamp }
  57. function MSecsToTimeStamp(MSecs: comp): TTimeStamp;
  58. begin
  59. result.Date := Trunc(msecs / msecsperday);
  60. msecs:= msecs-comp(result.date)*msecsperday;
  61. result.Time := Round(MSecs);
  62. end ;
  63. { TimeStampToMSecs }
  64. function TimeStampToMSecs(const TimeStamp: TTimeStamp): comp;
  65. begin
  66. result := TimeStamp.Time + comp(timestamp.date)*msecsperday;
  67. end ;
  68. Function TryEncodeDate(Year,Month,Day : Word; Out Date : TDateTime) : Boolean;
  69. var
  70. c, ya: cardinal;
  71. begin
  72. Result:=(Year>0) and (Year<10000) and
  73. (Month in [1..12]) and
  74. (Day>0) and (Day<=MonthDays[IsleapYear(Year),Month]);
  75. If Result then
  76. begin
  77. if month > 2 then
  78. Dec(Month,3)
  79. else
  80. begin
  81. Inc(Month,9);
  82. Dec(Year);
  83. end;
  84. c:= Year DIV 100;
  85. ya:= Year - 100*c;
  86. Date := (146097*c) SHR 2 + (1461*ya) SHR 2 + (153*cardinal(Month)+2) DIV 5 + cardinal(Day);
  87. // Note that this line can't be part of the line above, since TDateTime is
  88. // signed and c and ya are not
  89. Date := Date - 693900;
  90. end
  91. end;
  92. function TryEncodeTime(Hour, Min, Sec, MSec:word; Out Time : TDateTime) : boolean;
  93. begin
  94. Result:=(Hour<24) and (Min<60) and (Sec<60) and (MSec<1000);
  95. If Result then
  96. Time:=TDateTime(cardinal(Hour)*3600000+cardinal(Min)*60000+cardinal(Sec)*1000+MSec)/MSecsPerDay;
  97. end;
  98. { EncodeDate packs three variables Year, Month and Day into a
  99. TDateTime value the result is the number of days since 12/30/1899 }
  100. function EncodeDate(Year, Month, Day: word): TDateTime;
  101. begin
  102. If Not TryEncodeDate(Year,Month,Day,Result) then
  103. Raise EConvertError.CreateFmt('%d-%d-%d is not a valid date specification',
  104. [Year,Month,Day]);
  105. end;
  106. { EncodeTime packs four variables Hour, Minute, Second and MilliSecond into
  107. a TDateTime value }
  108. function EncodeTime(Hour, Minute, Second, MilliSecond:word):TDateTime;
  109. begin
  110. If not TryEncodeTime(Hour,Minute,Second,MilliSecond,Result) then
  111. Raise EConvertError.CreateFmt('%d:%d:%d.%d is not a valid time specification',
  112. [Hour,Minute,Second,MilliSecond]);
  113. end;
  114. { DecodeDate unpacks the value Date into three values:
  115. Year, Month and Day }
  116. procedure DecodeDate(Date: TDateTime; out Year, Month, Day: word);
  117. var
  118. ly,ld,lm,j : cardinal;
  119. begin
  120. if Date <= -datedelta then // If Date is before 1-1-1 then return 0-0-0
  121. begin
  122. Year := 0;
  123. Month := 0;
  124. Day := 0;
  125. end
  126. else
  127. begin
  128. j := pred((Trunc(System.Int(Date)) + 693900) SHL 2);
  129. ly:= j DIV 146097;
  130. j:= j - 146097 * cardinal(ly);
  131. ld := j SHR 2;
  132. j:=(ld SHL 2 + 3) DIV 1461;
  133. ld:= (cardinal(ld) SHL 2 + 7 - 1461*j) SHR 2;
  134. lm:=(5 * ld-3) DIV 153;
  135. ld:= (5 * ld +2 - 153*lm) DIV 5;
  136. ly:= 100 * cardinal(ly) + j;
  137. if lm < 10 then
  138. inc(lm,3)
  139. else
  140. begin
  141. dec(lm,9);
  142. inc(ly);
  143. end;
  144. year:=ly;
  145. month:=lm;
  146. day:=ld;
  147. end;
  148. end;
  149. function DecodeDateFully(const DateTime: TDateTime; out Year, Month, Day, DOW: Word): Boolean;
  150. begin
  151. DecodeDate(DateTime,Year,Month,Day);
  152. DOW:=DayOfWeek(DateTime);
  153. Result:=IsLeapYear(Year);
  154. end;
  155. { DecodeTime unpacks Time into four values:
  156. Hour, Minute, Second and MilliSecond }
  157. procedure DecodeTime(Time: TDateTime; out Hour, Minute, Second, MilliSecond: word);
  158. Var
  159. l : cardinal;
  160. begin
  161. l := DateTimeToTimeStamp(Time).Time;
  162. Hour := l div 3600000;
  163. l := l mod 3600000;
  164. Minute := l div 60000;
  165. l := l mod 60000;
  166. Second := l div 1000;
  167. l := l mod 1000;
  168. MilliSecond := l;
  169. end;
  170. { DateTimeToSystemTime converts DateTime value to SystemTime }
  171. procedure DateTimeToSystemTime(DateTime: TDateTime; out SystemTime: TSystemTime);
  172. begin
  173. DecodeDate(DateTime, SystemTime.Year, SystemTime.Month, SystemTime.Day);
  174. DecodeTime(DateTime, SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond);
  175. end ;
  176. { SystemTimeToDateTime converts SystemTime to a TDateTime value }
  177. function SystemTimeToDateTime(const SystemTime: TSystemTime): TDateTime;
  178. begin
  179. result := ComposeDateTime(DoEncodeDate(SystemTime.Year, SystemTime.Month, SystemTime.Day),
  180. DoEncodeTime(SystemTime.Hour, SystemTime.Minute, SystemTime.Second, SystemTime.MilliSecond));
  181. end ;
  182. { DayOfWeek returns the Day of the week (sunday is day 1) }
  183. function DayOfWeek(DateTime: TDateTime): integer;
  184. begin
  185. Result := 1 + ((Trunc(DateTime) - 1) mod 7);
  186. If (Result<=0) then
  187. Inc(Result,7);
  188. end;
  189. { Date returns the current Date }
  190. function Date: TDateTime;
  191. var
  192. SystemTime: TSystemTime;
  193. begin
  194. GetLocalTime(SystemTime);
  195. result := DoEncodeDate(SystemTime.Year, SystemTime.Month, SystemTime.Day);
  196. end ;
  197. { Time returns the current Time }
  198. function Time: TDateTime;
  199. var
  200. SystemTime: TSystemTime;
  201. begin
  202. GetLocalTime(SystemTime);
  203. Result := DoEncodeTime(SystemTime.Hour,SystemTime.Minute,SystemTime.Second,SystemTime.MilliSecond);
  204. end ;
  205. { Now returns the current Date and Time }
  206. function Now: TDateTime;
  207. var
  208. SystemTime: TSystemTime;
  209. begin
  210. GetLocalTime(SystemTime);
  211. result := systemTimeToDateTime(SystemTime);
  212. end;
  213. { IncMonth increments DateTime with NumberOfMonths months,
  214. NumberOfMonths can be less than zero }
  215. function IncMonth(const DateTime: TDateTime; NumberOfMonths: integer = 1 ): TDateTime;
  216. var
  217. Year, Month, Day : word;
  218. begin
  219. DecodeDate(DateTime, Year, Month, Day);
  220. IncAMonth(Year, Month, Day, NumberOfMonths);
  221. result := ComposeDateTime(DoEncodeDate(Year, Month, Day), DateTime);
  222. end ;
  223. { IncAMonth is the same as IncMonth, but operates on decoded date }
  224. procedure IncAMonth(var Year, Month, Day: Word; NumberOfMonths: Integer = 1);
  225. var
  226. TempMonth, S: Integer;
  227. begin
  228. If NumberOfMonths>=0 then
  229. s:=1
  230. else
  231. s:=-1;
  232. inc(Year,(NumberOfMonths div 12));
  233. TempMonth:=Month+(NumberOfMonths mod 12)-1;
  234. if (TempMonth>11) or
  235. (TempMonth<0) then
  236. begin
  237. Dec(TempMonth, S*12);
  238. Inc(Year, S);
  239. end;
  240. Month:=TempMonth+1; { Months from 1 to 12 }
  241. If (Day>MonthDays[IsLeapYear(Year)][Month]) then
  242. Day:=MonthDays[IsLeapYear(Year)][Month];
  243. end;
  244. { IsLeapYear returns true if Year is a leap year }
  245. function IsLeapYear(Year: Word): boolean;
  246. begin
  247. Result := (Year mod 4 = 0) and ((Year mod 100 <> 0) or (Year mod 400 = 0));
  248. end;
  249. { DateToStr returns a string representation of Date using ShortDateFormat }
  250. function DateToStr(Date: TDateTime): string;
  251. begin
  252. DateTimeToString(Result, 'ddddd', Date);
  253. end ;
  254. function DateToStr(Date: TDateTime; const FormatSettings: TFormatSettings): string;
  255. begin
  256. DateTimeToString(result, FormatSettings.ShortDateFormat, Date, FormatSettings);
  257. end;
  258. { TimeToStr returns a string representation of Time using LongTimeFormat }
  259. function TimeToStr(Time: TDateTime): string;
  260. begin
  261. DateTimeToString(Result, 'tt', Time);
  262. end ;
  263. function TimeToStr(Time: TDateTime; const FormatSettings: TFormatSettings): string;
  264. begin
  265. DateTimeToString(Result, FormatSettings.LongTimeFormat, Time, FormatSettings);
  266. end;
  267. { DateTimeToStr returns a string representation of DateTime using LongDateTimeFormat }
  268. function DateTimeToStr(DateTime: TDateTime): string;
  269. begin
  270. DateTimeToString(Result, 'c', DateTime);
  271. end ;
  272. function DateTimeToStr(DateTime: TDateTime; const FormatSettings: TFormatSettings): string;
  273. begin
  274. DateTimeToString(Result, 'c', DateTime ,FormatSettings);
  275. end;
  276. { StrToDate converts the string S to a TDateTime value
  277. if S does not represent a valid date value
  278. an EConvertError will be raised }
  279. function IntStrToDate(Out ErrorMsg : AnsiString; const S: PChar; Len : integer; const useformat : string; const defs:TFormatSettings; separator : char = #0): TDateTime;
  280. const SInvalidDateFormat = '"%s" is not a valid date format';
  281. procedure FixErrorMsg(const errm :ansistring;const errmarg : ansistring);
  282. begin
  283. errormsg:=format(errm,[errmarg]);
  284. end;
  285. var
  286. df:string;
  287. d,m,y,ly:word;
  288. n,i:longint;
  289. c:word;
  290. dp,mp,yp,which : Byte;
  291. s1:string[4];
  292. values:array[0..3] of longint;
  293. LocalTime:tsystemtime;
  294. YearMoreThenTwoDigits : boolean;
  295. begin
  296. ErrorMsg:=''; Result:=0;
  297. if (Len=0) then
  298. begin
  299. FixErrorMsg(SInvalidDateFormat,'');
  300. exit;
  301. end;
  302. YearMoreThenTwoDigits := False;
  303. if separator = #0 then
  304. separator := defs.DateSeparator;
  305. df := UpperCase(useFormat);
  306. { Determine order of D,M,Y }
  307. yp:=0;
  308. mp:=0;
  309. dp:=0;
  310. Which:=0;
  311. i:=0;
  312. while (i<Length(df)) and (Which<3) do
  313. begin
  314. inc(i);
  315. Case df[i] of
  316. 'Y' :
  317. if yp=0 then
  318. begin
  319. Inc(Which);
  320. yp:=which;
  321. end;
  322. 'M' :
  323. if mp=0 then
  324. begin
  325. Inc(Which);
  326. mp:=which;
  327. end;
  328. 'D' :
  329. if dp=0 then
  330. begin
  331. Inc(Which);
  332. dp:=which;
  333. end;
  334. end;
  335. end;
  336. for i := 1 to 3 do
  337. values[i] := 0;
  338. s1 := '';
  339. n := 0;
  340. dec(len);
  341. for i := 0 to len do
  342. begin
  343. if s[i] in ['0'..'9'] then
  344. s1 := s1 + s[i];
  345. { space can be part of the shortdateformat, and is defaultly in slovak
  346. windows, therefor it shouldn't be taken as separator (unless so specified)
  347. and ignored }
  348. if (Separator <> ' ') and (s[i] = ' ') then
  349. Continue;
  350. if (s[i] = separator) or ((i = len) and (s[i] in ['0'..'9'])) then
  351. begin
  352. inc(n);
  353. if n>3 then
  354. begin
  355. FixErrorMsg(SInvalidDateFormat,s);
  356. exit;
  357. end;
  358. // Check if the year has more then two digits (if n=yp, then we are evaluating the year.)
  359. if (n=yp) and (length(s1)>2) then YearMoreThenTwoDigits := True;
  360. val(s1, values[n], c);
  361. if c<>0 then
  362. begin
  363. FixErrorMsg(SInvalidDateFormat,s);
  364. Exit;
  365. end;
  366. s1 := '';
  367. end
  368. else if not (s[i] in ['0'..'9']) then
  369. begin
  370. FixErrorMsg(SInvalidDateFormat,s);
  371. Exit;
  372. end;
  373. end ;
  374. if (Which<3) and (N>Which) then
  375. begin
  376. FixErrorMsg(SInvalidDateFormat,s);
  377. Exit;
  378. end;
  379. // Fill in values.
  380. getLocalTime(LocalTime);
  381. ly := LocalTime.Year;
  382. If N=3 then
  383. begin
  384. y:=values[yp];
  385. m:=values[mp];
  386. d:=values[dp];
  387. end
  388. Else
  389. begin
  390. Y:=ly;
  391. If n<2 then
  392. begin
  393. d:=values[1];
  394. m := LocalTime.Month;
  395. end
  396. else
  397. If dp<mp then
  398. begin
  399. d:=values[1];
  400. m:=values[2];
  401. end
  402. else
  403. begin
  404. d:=values[2];
  405. m:=values[1];
  406. end;
  407. end;
  408. if (y >= 0) and (y < 100) and not YearMoreThenTwoDigits then
  409. begin
  410. ly := ly - defs.TwoDigitYearCenturyWindow;
  411. Inc(Y, ly div 100 * 100);
  412. if (defs.TwoDigitYearCenturyWindow > 0) and (Y < ly) then
  413. Inc(Y, 100);
  414. end;
  415. if not TryEncodeDate(y, m, d, result) then
  416. errormsg:='Invalid date';
  417. end;
  418. function StrToDate(const S: PChar; Len : integer; const useformat : string; separator : char = #0): TDateTime;
  419. Var
  420. MSg : AnsiString;
  421. begin
  422. Result:=IntStrToDate(Msg,S,Len,useFormat,DefaultFormatSettings,Separator);
  423. If (Msg<>'') then
  424. Raise EConvertError.Create(Msg);
  425. end;
  426. function StrToDate(const S: string; FormatSettings: TFormatSettings): TDateTime;
  427. var
  428. Msg: AnsiString;
  429. begin
  430. Result:=IntStrToDate(Msg,@S[1],Length(S),FormatSettings.ShortDateFormat,FormatSettings);
  431. if Msg<>'' then
  432. raise EConvertError.Create(Msg);
  433. end;
  434. function StrToDate(const S: ShortString; const useformat : string; separator : char = #0): TDateTime;
  435. begin
  436. result := StrToDate(@S[1],Length(s),UseFormat,separator);
  437. end;
  438. function StrToDate(const S: AnsiString; const useformat : string; separator : char = #0): TDateTime;
  439. begin
  440. result := StrToDate(@S[1],Length(s),UseFormat,separator);
  441. end;
  442. function StrToDate(const S: ShortString; separator : char): TDateTime;
  443. begin
  444. result := StrToDate(@S[1],Length(s),DefaultFormatSettings.ShortDateFormat,separator)
  445. end;
  446. function StrToDate(const S: ShortString): TDateTime;
  447. begin
  448. result := StrToDate(@S[1],Length(s),DefaultFormatSettings.ShortDateFormat,#0);
  449. end;
  450. function StrToDate(const S: AnsiString; separator : char): TDateTime;
  451. begin
  452. result := StrToDate(@S[1],Length(s),DefaultFormatSettings.ShortDateFormat,separator)
  453. end;
  454. function StrToDate(const S: AnsiString): TDateTime;
  455. begin
  456. result := StrToDate(@S[1],Length(s),DefaultFormatSettings.ShortDateFormat,#0);
  457. end;
  458. { StrToTime converts the string S to a TDateTime value
  459. if S does not represent a valid time value an
  460. EConvertError will be raised }
  461. function IntStrToTime(Out ErrorMsg : AnsiString; const S: PChar; Len : integer;const defs:TFormatSettings; separator : char = #0): TDateTime;
  462. const
  463. AMPM_None = 0;
  464. AMPM_AM = 1;
  465. AMPM_PM = 2;
  466. tiHour = 0;
  467. tiMin = 1;
  468. tiSec = 2;
  469. tiMSec = 3;
  470. type
  471. TTimeValues = array[tiHour..tiMSec] of Word;
  472. var
  473. AmPm: integer;
  474. TimeValues: TTimeValues;
  475. function StrPas(Src : PChar; len: integer = 0) : ShortString;
  476. begin
  477. //this is unsafe for len > 255, it will trash memory (I tested this)
  478. //reducing it is safe, since whenever we use this a string > 255 is invalid anyway
  479. if len > 255 then len := 255;
  480. SetLength(Result, len);
  481. move(src[0],result[1],len);
  482. end;
  483. function SplitElements(out TimeValues: TTimeValues; out AmPm: Integer): Boolean;
  484. //Strict version. It does not allow #32 as Separator, it will treat it as whitespace always
  485. const
  486. Digits = ['0'..'9'];
  487. var
  488. Cur, Offset, ElemLen, Err, TimeIndex, FirstSignificantDigit: Integer;
  489. Value: Word;
  490. DigitPending, MSecPending: Boolean;
  491. AmPmStr: ShortString;
  492. CurChar: Char;
  493. begin
  494. Result := False;
  495. AmPm := AMPM_None; //No Am or PM in string found yet
  496. MSecPending := False;
  497. TimeIndex := 0; //indicating which TTimeValue must be filled next
  498. FillChar(TimeValues, SizeOf(TTimeValues), 0);
  499. Cur := 0;
  500. //skip leading blanks
  501. While (Cur < Len) and (S[Cur] =#32) do Inc(Cur);
  502. Offset := Cur;
  503. //First non-blank cannot be Separator or DecimalSeparator
  504. if (Cur > Len - 1) or (S[Cur] = Separator) or (S[Cur] = defs.Decimalseparator) then Exit;
  505. DigitPending := (S[Cur] in Digits);
  506. While (Cur < Len) do
  507. begin
  508. //writeln;
  509. //writeln('Main While loop: Cur = ',Cur,' S[Cur] = "',S[Cur],'" Len = ',Len);
  510. CurChar := S[Cur];
  511. if CurChar in Digits then
  512. begin//Digits
  513. //HH, MM, SS, or Msec?
  514. //writeln('Digit');
  515. //Digits are only allowed after starting Am/PM or at beginning of string or after Separator
  516. //and TimeIndex must be <= tiMSec
  517. //Uncomment "or (#32 = Separator)" and it will allllow #32 as separator
  518. if (not (DigitPending {or (#32 = Separator)})) or (TimeIndex > tiMSec) then Exit;
  519. OffSet := Cur;
  520. if (CurChar <> '0') then FirstSignificantDigit := OffSet else FirstSignificantDigit := -1;
  521. while (Cur < Len -1) and (S[Cur + 1] in Digits) do
  522. begin
  523. //Mark first Digit that is not '0'
  524. if (FirstSignificantDigit = -1) and (S[Cur] <> '0') then FirstSignificantDigit := Cur;
  525. Inc(Cur);
  526. end;
  527. if (FirstSignificantDigit = -1) then FirstSignificantDigit := Cur;
  528. ElemLen := 1 + Cur - FirstSignificantDigit;
  529. //writeln(' S[FirstSignificantDigit] = ',S[FirstSignificantDigit], ' S[Cur] = ',S[Cur],' ElemLen = ',ElemLen,' -> ', StrPas(S + Offset, ElemLen));
  530. //writeln(' Cur = ',Cur);
  531. //this way we know that Val() will never overflow Value !
  532. if (ElemLen <= 2) or ((ElemLen <= 3) and (TimeIndex = tiMSec) ) then
  533. begin
  534. Val(StrPas(S + FirstSignificantDigit, ElemLen), Value, Err);
  535. //writeln(' Value = ',Value,' HH = ',TimeValues[0],' MM = ',TimeValues[1],' SS = ',TimeValues[2],' MSec = ',Timevalues[3]);
  536. //This is safe now, because we know Value < High(Word)
  537. TimeValues[TimeIndex] := Value;
  538. Inc(TimeIndex);
  539. DigitPending := False;
  540. end
  541. else Exit; //Value to big, so it must be a wrong timestring
  542. end//Digits
  543. else if (CurChar = #32) then
  544. begin
  545. //writeln('#32');
  546. //just skip, but we must adress this, or it will be parsed by either AM/PM or Separator
  547. end
  548. else if (CurChar = Separator) then
  549. begin
  550. //writeln('Separator');
  551. if DigitPending or (TimeIndex > tiSec) then Exit;
  552. DigitPending := True;
  553. MSecPending := False;
  554. end
  555. else if (CurChar = defs.DecimalSeparator) then
  556. begin
  557. //writeln('DecimalSeparator');
  558. if DigitPending or MSecPending or (TimeIndex <> tiMSec) then Exit;
  559. DigitPending := True;
  560. MSecPending := True;
  561. end
  562. else
  563. begin//AM/PM?
  564. //None of the above, so this char _must_ be the start of AM/PM string
  565. //If we already have found AM/PM or we expect a digit then then timestring must be wrong at this point
  566. //writeln('AM/PM?');
  567. if (AmPm <> AMPM_None) or DigitPending then Exit;
  568. OffSet := Cur;
  569. while (Cur < Len -1) and (not (S[Cur + 1] in [Separator, #32, defs.DecimalSeparator]))
  570. and not (S[Cur + 1] in Digits) do Inc(Cur);
  571. ElemLen := 1 + Cur - OffSet;
  572. //writeln(' S[Offset] = ',S[Offset], ' S[Cur] = ',S[Cur],' ElemLen = ',ElemLen,' -> ', StrPas(S + Offset, ElemLen));
  573. //writeln(' Cur = ',Cur);
  574. AmPmStr := StrPas(S + OffSet, ElemLen);
  575. //writeln('AmPmStr = ',ampmstr,' (',length(ampmstr),')');
  576. //We must compare to TimeAMString before hardcoded 'AM' for delphi compatibility
  577. //Also it is perfectly legal, though insane to have TimeAMString = 'PM' and vice versa
  578. if (AnsiCompareText(AmPmStr, defs.TimeAMString) = 0) then AmPm := AMPM_AM
  579. else if (AnsiCompareText(AmPmStr, defs.TimePMString) = 0) then AmPm := AMPM_PM
  580. else if (CompareText(AmPmStr, 'AM') = 0) then AmPm := AMPM_AM
  581. else if (CompareText(AmPmStr, 'PM') = 0) then AmPm := AMPM_PM
  582. else Exit; //If text does not match any of these, timestring must be wrong;
  583. //if AM/PM is at beginning of string, then a digit is mandatory after it
  584. if (TimeIndex = tiHour) then
  585. begin
  586. DigitPending := True;
  587. end
  588. //otherwise, no more TimeValues allowed after this
  589. else
  590. begin
  591. TimeIndex := tiMSec + 1;
  592. DigitPending := False;
  593. end;
  594. end;//AM/PM
  595. Inc(Cur)
  596. end;//while
  597. //If we arrive here, parsing the elements has been successfull
  598. //if not at least Hours specified then input is not valid
  599. //when am/pm is specified Hour must be <= 12 and not 0
  600. if (TimeIndex = tiHour) or ((AmPm <> AMPM_None) and ((TimeValues[tiHour] > 12) or (TimeValues[tiHour] = 0))) or DigitPending then Exit;
  601. Result := True;
  602. end;
  603. begin
  604. if separator = #0 then
  605. separator := defs.TimeSeparator;
  606. AmPm := AMPM_None;
  607. if not SplitElements(TimeValues, AmPm) then
  608. begin
  609. ErrorMsg:=Format(SErrInvalidTimeFormat,[StrPas(S, Len)]);
  610. Exit;
  611. end;
  612. if (AmPm=AMPM_PM) and (TimeValues[tiHour]<>12) then Inc(TimeValues[tiHour], 12)
  613. else if (AmPm=AMPM_AM) and (TimeValues[tiHour]=12) then TimeValues[tiHour]:=0;
  614. if not TryEncodeTime(TimeValues[tiHour], TimeValues[tiMin], TimeValues[tiSec], TimeValues[tiMSec], result) Then
  615. //errormsg:='Invalid time.';
  616. ErrorMsg:=Format(SErrInvalidTimeFormat,[StrPas(S, Len)]);
  617. end ;
  618. function StrToTime(const S: PChar; Len : integer; separator : char = #0): TDateTime;
  619. Var
  620. Msg : AnsiString;
  621. begin
  622. Result:=IntStrToTime(Msg,S,Len,DefaultFormatSettings,Separator);
  623. If (Msg<>'') then
  624. Raise EConvertError.Create(Msg);
  625. end;
  626. function StrToTime(const S: string; FormatSettings : TFormatSettings): TDateTime;
  627. Var
  628. Msg : AnsiString;
  629. begin
  630. Result:=IntStrToTime(Msg, @S[1], length(S), FormatSettings, #0);
  631. If (Msg<>'') then
  632. Raise EConvertError.Create(Msg);
  633. end;
  634. function StrToTime(const s: ShortString; separator : char): TDateTime;
  635. begin
  636. result := StrToTime(@s[1], length(s), separator);
  637. end;
  638. function StrToTime(const s: AnsiString; separator : char): TDateTime;
  639. begin
  640. result := StrToTime(@s[1], length(s), separator);
  641. end;
  642. function StrToTime(const s: ShortString): TDateTime;
  643. begin
  644. result := StrToTime(@s[1], length(s), #0);
  645. end;
  646. function StrToTime(const s: AnsiString): TDateTime;
  647. begin
  648. result := StrToTime(@s[1], length(s), #0);
  649. end;
  650. { StrToDateTime converts the string S to a TDateTime value
  651. if S does not represent a valid date and/or time value
  652. an EConvertError will be raised }
  653. function StrToDateTime(const s: string): TDateTime;
  654. var
  655. I: integer;
  656. begin
  657. I:=Pos(DefaultFormatSettings.TimeSeparator,S);
  658. If (I>0) then
  659. begin
  660. While (I>0) and (S[I]<>' ') do
  661. Dec(I);
  662. If I>0 then
  663. result:=ComposeDateTime(StrToDate(Copy(S,1,I-1)),StrToTime(Copy(S,i+1, Length(S)-i)))
  664. else
  665. result:=StrToTime(S)
  666. end
  667. else
  668. Result:=StrToDate(S);
  669. end;
  670. function StrToDateTime(const s: AnsiString; const FormatSettings : TFormatSettings): TDateTime;
  671. var
  672. I: integer;
  673. begin
  674. I:=Pos(FormatSettings.TimeSeparator,S);
  675. If (I>0) then
  676. begin
  677. While (I>0) and (S[I]<>' ') do
  678. Dec(I);
  679. If I>0 then
  680. result:=ComposeDateTime(StrToDate(Copy(S,1,I-1),FormatSettings.ShortDateFormat,FormatSettings.DateSeparator),
  681. StrToTime(Copy(S,i+1, Length(S)-i),FormatSettings))
  682. else
  683. result:=StrToTime(S,FormatSettings)
  684. end
  685. else
  686. Result:=StrToDate(S,FormatSettings.ShortDateFormat,FormatSettings.DateSeparator);
  687. end;
  688. function StrToDateTime(const s: ShortString; const FormatSettings : TFormatSettings): TDateTime;
  689. var
  690. I: integer;
  691. begin
  692. I:=Pos(FormatSettings.TimeSeparator,S);
  693. If (I>0) then
  694. begin
  695. While (I>0) and (S[I]<>' ') do
  696. Dec(I);
  697. If I>0 then
  698. result:=ComposeDateTime(StrToDate(Copy(S,1,I-1),FormatSettings.ShortDateFormat,FormatSettings.DateSeparator),
  699. StrToTime(Copy(S,i+1, Length(S)-i),FormatSettings.TimeSeparator))
  700. else
  701. result:=StrToTime(S,FormatSettings.TimeSeparator)
  702. end
  703. else
  704. Result:=StrToDate(S,FormatSettings.ShortDateFormat,FormatSettings.DateSeparator);
  705. end;
  706. { FormatDateTime formats DateTime to the given format string FormatStr }
  707. function FormatDateTime(const FormatStr: string; DateTime: TDateTime): string;
  708. begin
  709. DateTimeToString(Result, FormatStr, DateTime, DefaultFormatSettings);
  710. end;
  711. function FormatDateTime(const FormatStr: string; DateTime: TDateTime; const FormatSettings: TFormatSettings): string;
  712. begin
  713. DateTimeToString(Result, FormatStr, DateTime, FormatSettings);
  714. end;
  715. { DateTimeToString formats DateTime to the given format in FormatStr }
  716. procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime);
  717. begin
  718. DateTimeToString(Result, FormatStr, DateTime, DefaultFormatSettings);
  719. end;
  720. procedure DateTimeToString(out Result: string; const FormatStr: string; const DateTime: TDateTime; const FormatSettings: TFormatSettings);
  721. var
  722. ResultLen: integer;
  723. ResultBuffer: array[0..255] of char;
  724. ResultCurrent: pchar;
  725. {$IFDEF MSWindows}
  726. isEnable_E_Format : Boolean;
  727. isEnable_G_Format : Boolean;
  728. eastasiainited : boolean;
  729. {$ENDIF MSWindows}
  730. {$IFDEF MSWindows}
  731. procedure InitEastAsia;
  732. var ALCID : LCID;
  733. PriLangID , SubLangID : Word;
  734. begin
  735. ALCID := GetThreadLocale;
  736. PriLangID := ALCID and $3FF;
  737. if (PriLangID>0) then
  738. SubLangID := (ALCID and $FFFF) shr 10
  739. else
  740. begin
  741. PriLangID := SysLocale.PriLangID;
  742. SubLangID := SysLocale.SubLangID;
  743. end;
  744. isEnable_E_Format := (PriLangID = LANG_JAPANESE)
  745. or
  746. (PriLangID = LANG_KOREAN)
  747. or
  748. ((PriLangID = LANG_CHINESE)
  749. and
  750. (SubLangID = SUBLANG_CHINESE_TRADITIONAL)
  751. );
  752. isEnable_G_Format := (PriLangID = LANG_JAPANESE)
  753. or
  754. ((PriLangID = LANG_CHINESE)
  755. and
  756. (SubLangID = SUBLANG_CHINESE_TRADITIONAL)
  757. );
  758. eastasiainited :=true;
  759. end;
  760. {$ENDIF MSWindows}
  761. procedure StoreStr(Str: PChar; Len: Integer);
  762. begin
  763. if ResultLen + Len < SizeOf(ResultBuffer) then
  764. begin
  765. StrMove(ResultCurrent, Str, Len);
  766. ResultCurrent := ResultCurrent + Len;
  767. ResultLen := ResultLen + Len;
  768. end;
  769. end;
  770. procedure StoreString(const Str: string);
  771. var Len: integer;
  772. begin
  773. Len := Length(Str);
  774. if ResultLen + Len < SizeOf(ResultBuffer) then
  775. begin
  776. StrMove(ResultCurrent, pchar(Str), Len);
  777. ResultCurrent := ResultCurrent + Len;
  778. ResultLen := ResultLen + Len;
  779. end;
  780. end;
  781. procedure StoreInt(Value, Digits: Integer);
  782. var
  783. S: string[16];
  784. Len: integer;
  785. begin
  786. System.Str(Value:Digits, S);
  787. for Len := 1 to Length(S) do
  788. begin
  789. if S[Len] = ' ' then
  790. S[Len] := '0'
  791. else
  792. Break;
  793. end;
  794. StoreStr(pchar(@S[1]), Length(S));
  795. end ;
  796. var
  797. Year, Month, Day, DayOfWeek, Hour, Minute, Second, MilliSecond: word;
  798. DT : TDateTime;
  799. procedure StoreFormat(const FormatStr: string; Nesting: Integer; TimeFlag: Boolean);
  800. var
  801. Token, lastformattoken: char;
  802. FormatCurrent: pchar;
  803. FormatEnd: pchar;
  804. Count: integer;
  805. Clock12: boolean;
  806. P: pchar;
  807. tmp: integer;
  808. begin
  809. if Nesting > 1 then // 0 is original string, 1 is included FormatString
  810. Exit;
  811. FormatCurrent := PChar(FormatStr);
  812. FormatEnd := FormatCurrent + Length(FormatStr);
  813. Clock12 := false;
  814. P := FormatCurrent;
  815. // look for unquoted 12-hour clock token
  816. while P < FormatEnd do
  817. begin
  818. Token := P^;
  819. case Token of
  820. '''', '"':
  821. begin
  822. Inc(P);
  823. while (P < FormatEnd) and (P^ <> Token) do
  824. Inc(P);
  825. end;
  826. 'A', 'a':
  827. begin
  828. if (StrLIComp(P, 'A/P', 3) = 0) or
  829. (StrLIComp(P, 'AMPM', 4) = 0) or
  830. (StrLIComp(P, 'AM/PM', 5) = 0) then
  831. begin
  832. Clock12 := true;
  833. break;
  834. end;
  835. end;
  836. end; // case
  837. Inc(P);
  838. end ;
  839. token := #255;
  840. lastformattoken := ' ';
  841. while FormatCurrent < FormatEnd do
  842. begin
  843. Token := UpCase(FormatCurrent^);
  844. Count := 1;
  845. P := FormatCurrent + 1;
  846. case Token of
  847. '''', '"':
  848. begin
  849. while (P < FormatEnd) and (p^ <> Token) do
  850. Inc(P);
  851. Inc(P);
  852. Count := P - FormatCurrent;
  853. StoreStr(FormatCurrent + 1, Count - 2);
  854. end ;
  855. 'A':
  856. begin
  857. if StrLIComp(FormatCurrent, 'AMPM', 4) = 0 then
  858. begin
  859. Count := 4;
  860. if Hour < 12 then
  861. StoreString(FormatSettings.TimeAMString)
  862. else
  863. StoreString(FormatSettings.TimePMString);
  864. end
  865. else if StrLIComp(FormatCurrent, 'AM/PM', 5) = 0 then
  866. begin
  867. Count := 5;
  868. if Hour < 12 then StoreStr(FormatCurrent, 2)
  869. else StoreStr(FormatCurrent+3, 2);
  870. end
  871. else if StrLIComp(FormatCurrent, 'A/P', 3) = 0 then
  872. begin
  873. Count := 3;
  874. if Hour < 12 then StoreStr(FormatCurrent, 1)
  875. else StoreStr(FormatCurrent+2, 1);
  876. end
  877. else
  878. raise EConvertError.Create('Illegal character in format string');
  879. end ;
  880. '/': StoreStr(@FormatSettings.DateSeparator, 1);
  881. ':': StoreStr(@FormatSettings.TimeSeparator, 1);
  882. ' ', 'C', 'D', 'H', 'M', 'N', 'S', 'T', 'Y','Z' :
  883. begin
  884. while (P < FormatEnd) and (UpCase(P^) = Token) do
  885. Inc(P);
  886. Count := P - FormatCurrent;
  887. case Token of
  888. ' ': StoreStr(FormatCurrent, Count);
  889. 'Y': begin
  890. if Count > 2 then
  891. StoreInt(Year, 4)
  892. else
  893. StoreInt(Year mod 100, 2);
  894. end;
  895. 'M': begin
  896. if (lastformattoken = 'H') or TimeFlag then
  897. begin
  898. if Count = 1 then
  899. StoreInt(Minute, 0)
  900. else
  901. StoreInt(Minute, 2);
  902. end
  903. else
  904. begin
  905. case Count of
  906. 1: StoreInt(Month, 0);
  907. 2: StoreInt(Month, 2);
  908. 3: StoreString(FormatSettings.ShortMonthNames[Month]);
  909. else
  910. StoreString(FormatSettings.LongMonthNames[Month]);
  911. end;
  912. end;
  913. end;
  914. 'D': begin
  915. case Count of
  916. 1: StoreInt(Day, 0);
  917. 2: StoreInt(Day, 2);
  918. 3: StoreString(FormatSettings.ShortDayNames[DayOfWeek]);
  919. 4: StoreString(FormatSettings.LongDayNames[DayOfWeek]);
  920. 5: StoreFormat(FormatSettings.ShortDateFormat, Nesting+1, False);
  921. else
  922. StoreFormat(FormatSettings.LongDateFormat, Nesting+1, False);
  923. end ;
  924. end ;
  925. 'H': if Clock12 then
  926. begin
  927. tmp := hour mod 12;
  928. if tmp=0 then tmp:=12;
  929. if Count = 1 then
  930. StoreInt(tmp, 0)
  931. else
  932. StoreInt(tmp, 2);
  933. end
  934. else begin
  935. if Count = 1 then
  936. StoreInt(Hour, 0)
  937. else
  938. StoreInt(Hour, 2);
  939. end;
  940. 'N': if Count = 1 then
  941. StoreInt(Minute, 0)
  942. else
  943. StoreInt(Minute, 2);
  944. 'S': if Count = 1 then
  945. StoreInt(Second, 0)
  946. else
  947. StoreInt(Second, 2);
  948. 'Z': if Count = 1 then
  949. StoreInt(MilliSecond, 0)
  950. else
  951. StoreInt(MilliSecond, 3);
  952. 'T': if Count = 1 then
  953. StoreFormat(FormatSettings.ShortTimeFormat, Nesting+1, True)
  954. else
  955. StoreFormat(FormatSettings.LongTimeFormat, Nesting+1, True);
  956. 'C': begin
  957. StoreFormat(FormatSettings.ShortDateFormat, Nesting+1, False);
  958. if (Hour<>0) or (Minute<>0) or (Second<>0) then
  959. begin
  960. StoreString(' ');
  961. StoreFormat(FormatSettings.LongTimeFormat, Nesting+1, True);
  962. end;
  963. end;
  964. {$IFDEF MSWindows}
  965. 'E':
  966. begin
  967. if not Eastasiainited then InitEastAsia;
  968. if Not(isEnable_E_Format) then StoreStr(@FormatCurrent^, 1)
  969. else
  970. begin
  971. while (P < FormatEnd) and (UpCase(P^) = Token) do
  972. P := P + 1;
  973. Count := P - FormatCurrent;
  974. StoreString(ConvertEraYearString(Count,Year,Month,Day));
  975. end;
  976. lastformattoken:=token;
  977. end;
  978. 'G':
  979. begin
  980. if not Eastasiainited then InitEastAsia;
  981. if Not(isEnable_G_Format) then StoreStr(@FormatCurrent^, 1)
  982. else
  983. begin
  984. while (P < FormatEnd) and (UpCase(P^) = Token) do
  985. P := P + 1;
  986. Count := P - FormatCurrent;
  987. StoreString(ConvertEraString(Count,Year,Month,Day));
  988. end;
  989. lastformattoken:=token;
  990. end;
  991. {$ENDIF MSWindows}
  992. end;
  993. lastformattoken := token;
  994. end;
  995. else
  996. StoreStr(@Token, 1);
  997. end ;
  998. Inc(FormatCurrent, Count);
  999. end;
  1000. end;
  1001. begin
  1002. {$ifdef MSWindows}
  1003. eastasiainited:=false;
  1004. {$endif MSWindows}
  1005. DecodeDateFully(DateTime, Year, Month, Day, DayOfWeek);
  1006. DecodeTime(DateTime, Hour, Minute, Second, MilliSecond);
  1007. ResultLen := 0;
  1008. ResultCurrent := @ResultBuffer[0];
  1009. if FormatStr <> '' then
  1010. StoreFormat(FormatStr, 0, False)
  1011. else
  1012. StoreFormat('C', 0, False);
  1013. ResultBuffer[ResultLen] := #0;
  1014. result := StrPas(@ResultBuffer[0]);
  1015. end ;
  1016. Function DateTimeToFileDate(DateTime : TDateTime) : Longint;
  1017. Var YY,MM,DD,H,m,s,msec : Word;
  1018. begin
  1019. Decodedate (DateTime,YY,MM,DD);
  1020. DecodeTime (DateTime,h,m,s,msec);
  1021. {$ifndef unix}
  1022. If (YY<1980) or (YY>2099) then
  1023. Result:=0
  1024. else
  1025. begin
  1026. Result:=(s shr 1) or (m shl 5) or (h shl 11);
  1027. Result:=Result or longint(DD shl 16 or (MM shl 21) or (word(YY-1980) shl 25));
  1028. end;
  1029. {$else unix}
  1030. Result:=LocalToEpoch(yy,mm,dd,h,m,s);
  1031. {$endif unix}
  1032. end;
  1033. function CurrentYear: Word;
  1034. var
  1035. SysTime: TSystemTime;
  1036. begin
  1037. GetLocalTime(SysTime);
  1038. Result := SysTime.Year;
  1039. end;
  1040. Function FileDateToDateTime (Filedate : Longint) : TDateTime;
  1041. {$ifndef unix}
  1042. Var Date,Time : Word;
  1043. begin
  1044. Date:=FileDate shr 16;
  1045. Time:=FileDate and $ffff;
  1046. Result:=ComposeDateTime(EncodeDate((Date shr 9) + 1980,(Date shr 5) and 15, Date and 31),
  1047. EncodeTime(Time shr 11, (Time shr 5) and 63, (Time and 31) shl 1,0));
  1048. end;
  1049. {$else unix}
  1050. var
  1051. y, mon, d, h, min, s: word;
  1052. begin
  1053. EpochToLocal(FileDate,y,mon,d,h,min,s);
  1054. Result:=ComposeDateTime(EncodeDate(y,mon,d),EncodeTime(h,min,s,0));
  1055. end;
  1056. {$endif unix}
  1057. function TryStrToDate(const S: ShortString; out Value: TDateTime): Boolean;
  1058. begin
  1059. result := TryStrToDate(S, Value, #0);
  1060. end;
  1061. function TryStrToDate(const S: ShortString; out Value: TDateTime;
  1062. const useformat : string; separator : char = #0): Boolean;
  1063. Var
  1064. Msg : Ansistring;
  1065. begin
  1066. Value:=IntStrToDate(Msg,@S[1],Length(S),useformat,defaultformatsettings,separator);
  1067. Result:=(Msg='');
  1068. end;
  1069. function TryStrToDate(const S: AnsiString; out Value: TDateTime;
  1070. const useformat : string; separator : char = #0): Boolean;
  1071. Var
  1072. Msg : Ansistring;
  1073. begin
  1074. Result:=Length(S)<>0;
  1075. If Result then
  1076. begin
  1077. Value:=IntStrToDate(Msg,@S[1],Length(S),useformat,DefaultFormatSettings,Separator);
  1078. Result:=(Msg='');
  1079. end;
  1080. end;
  1081. function TryStrToDate(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
  1082. begin
  1083. Result:=TryStrToDate(S,Value,DefaultFormatSettings.ShortDateFormat,Separator);
  1084. end;
  1085. function TryStrToDate(const S: AnsiString; out Value: TDateTime): Boolean;
  1086. begin
  1087. Result:=TryStrToDate(S,Value,DefaultFormatSettings.ShortDateFormat,#0);
  1088. end;
  1089. function TryStrToDate(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
  1090. begin
  1091. Result:=TryStrToDate(S,Value,DefaultFormatSettings.ShortDateFormat,Separator);
  1092. end;
  1093. function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  1094. Var
  1095. Msg : Ansistring;
  1096. begin
  1097. Result:=Length(S)<>0;
  1098. If Result then
  1099. begin
  1100. Value:=IntStrToDate(Msg,@S[1],Length(S),FormatSettings.ShortDateFormat,FormatSettings,#0);
  1101. Result:=(Msg='');
  1102. end;
  1103. end;
  1104. function TryStrToTime(const S: ShortString; out Value: TDateTime; separator : char): Boolean;
  1105. Var
  1106. Msg : AnsiString;
  1107. begin
  1108. Value:=IntStrToTime(Msg,@S[1],Length(S),DefaultFormatSettings,Separator);
  1109. result:=(Msg='');
  1110. end;
  1111. function TryStrToTime(const S: ShortString; out Value: TDateTime): Boolean;
  1112. begin
  1113. Result := TryStrToTime(S,Value,#0);
  1114. end;
  1115. function TryStrToTime(const S: AnsiString; out Value: TDateTime; separator : char): Boolean;
  1116. Var
  1117. Msg : AnsiString;
  1118. begin
  1119. Result:=Length(S)<>0;
  1120. If Result then
  1121. begin
  1122. Value:=IntStrToTime(Msg,@S[1],Length(S),DefaultFormatSettings,Separator);
  1123. Result:=(Msg='');
  1124. end;
  1125. end;
  1126. function TryStrToTime(const S: AnsiString; out Value: TDateTime): Boolean;
  1127. begin
  1128. result := TryStrToTime(S,Value,#0);
  1129. end;
  1130. function TryStrToTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  1131. Var msg : AnsiString;
  1132. begin
  1133. Result:=Length(S)<>0;
  1134. If Result then
  1135. begin
  1136. Value:=IntStrToTime(Msg,@S[1],Length(S),FormatSettings,#0);
  1137. Result:=(Msg='');
  1138. end;
  1139. end;
  1140. function TryStrToDateTime(const S: ShortString; out Value: TDateTime): Boolean;
  1141. begin
  1142. result:=true;
  1143. try
  1144. value:=StrToDateTime(s);
  1145. except
  1146. on EConvertError do
  1147. result:=false
  1148. end;
  1149. end;
  1150. function TryStrToDateTime(const S: AnsiString; out Value: TDateTime): Boolean;
  1151. begin
  1152. result:=true;
  1153. try
  1154. value:=StrToDateTime(s);
  1155. except
  1156. on EConvertError do
  1157. result:=false
  1158. end;
  1159. end;
  1160. function TryStrToDateTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean;
  1161. var
  1162. I: integer;
  1163. dtdate, dttime :TDateTime;
  1164. begin
  1165. result:=false;
  1166. I:=Pos(FormatSettings.TimeSeparator,S);
  1167. If (I>0) then
  1168. begin
  1169. While (I>0) and (S[I]<>' ') do
  1170. Dec(I);
  1171. If I>0 then
  1172. begin
  1173. if not TryStrToDate(Copy(S,1,I-1),dtdate,Formatsettings) then
  1174. exit;
  1175. if not TryStrToTime(Copy(S,i+1, Length(S)-i),dttime,Formatsettings) then
  1176. exit;
  1177. Value:=ComposeDateTime(dtdate,dttime);
  1178. result:=true;
  1179. end
  1180. else
  1181. result:=TryStrToTime(s,Value,Formatsettings);
  1182. end
  1183. else
  1184. result:=TryStrToDate(s,Value,Formatsettings);
  1185. end;
  1186. function StrToDateDef(const S: ShortString; const Defvalue : TDateTime): TDateTime;
  1187. begin
  1188. result := StrToDateDef(S,DefValue,#0);
  1189. end;
  1190. function StrToTimeDef(const S: ShortString; const Defvalue : TDateTime): TDateTime;
  1191. begin
  1192. result := StrToTimeDef(S,DefValue,#0);
  1193. end;
  1194. function StrToDateTimeDef(const S: ShortString; const Defvalue : TDateTime): TDateTime;
  1195. begin
  1196. if not TryStrToDateTime(s,Result) Then
  1197. result:=defvalue;
  1198. end;
  1199. function StrToDateDef(const S: ShortString; const Defvalue : TDateTime; separator : char): TDateTime;
  1200. begin
  1201. if not TryStrToDate(s,Result, separator) Then
  1202. result:=defvalue;
  1203. end;
  1204. function StrToTimeDef(const S: ShortString; const Defvalue : TDateTime; separator : char): TDateTime;
  1205. begin
  1206. if not TryStrToTime(s,Result, separator) Then
  1207. result:=defvalue;
  1208. end;
  1209. function StrToDateDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime;
  1210. begin
  1211. result := StrToDateDef(S,DefValue,#0);
  1212. end;
  1213. function StrToTimeDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime;
  1214. begin
  1215. result := StrToTimeDef(S,DefValue,#0);
  1216. end;
  1217. function StrToDateTimeDef(const S: AnsiString; const Defvalue : TDateTime): TDateTime;
  1218. begin
  1219. if not TryStrToDateTime(s,Result) Then
  1220. result:=defvalue;
  1221. end;
  1222. function StrToDateDef(const S: AnsiString; const Defvalue : TDateTime; separator : char): TDateTime;
  1223. begin
  1224. if not TryStrToDate(s,Result, separator) Then
  1225. result:=defvalue;
  1226. end;
  1227. function StrToTimeDef(const S: AnsiString; const Defvalue : TDateTime; separator : char): TDateTime;
  1228. begin
  1229. if not TryStrToTime(s,Result, separator) Then
  1230. result:=defvalue;
  1231. end;
  1232. procedure ReplaceTime(var dati:TDateTime; NewTime : TDateTime);inline;
  1233. begin
  1234. dati:= ComposeDateTime(dati, newtime);
  1235. end;
  1236. procedure ReplaceDate(var DateTime: TDateTime; const NewDate: TDateTime); inline;
  1237. var
  1238. tmp : TDateTime;
  1239. begin
  1240. tmp:=NewDate;
  1241. ReplaceTime(tmp,DateTime);
  1242. DateTime:=tmp;
  1243. end;
  1244. {$IFNDEF HAS_LOCALTIMEZONEOFFSET}
  1245. Function GetLocalTimeOffset : Integer;
  1246. begin
  1247. Result:=0;
  1248. end;
  1249. {$ENDIF}