Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can find Iso-8601 conversion routines in our <a href="http://synopse.info/fossil/finfo?name=SynCommons.pas" rel="noreferrer">SynCommons unit</a>. </p> <p>It has been deeply optimized for speed, so it's much faster than the DateTimeToString() functions and such, but of course, code is more difficult to follow. ;)</p> <pre><code>procedure Iso8601ToDateTimePUTF8CharVar(P: PUTF8Char; L: integer; var result: TDateTime); var i: integer; B: cardinal; Y,M,D, H,MI,SS: cardinal; // we expect 'YYYYMMDDThhmmss' format but we handle also 'YYYY-MM-DD hh:mm:ss' begin result := 0; if P=nil then exit; if L=0 then L := StrLen(P); if L&lt;4 then exit; // we need 'YYYY' at least if P[0]='T' then dec(P,8) else begin B := ConvertHexToBin[ord(P[0])]; // first digit if B&gt;9 then exit else Y := B; // fast check '0'..'9' for i := 1 to 3 do begin B := ConvertHexToBin[ord(P[i])]; // 3 other digits if B&gt;9 then exit else Y := Y*10+B; end; if P[4] in ['-','/'] then begin inc(P); dec(L); end; // allow YYYY-MM-DD D := 1; if L&gt;=6 then begin // YYYYMM M := ord(P[4])*10+ord(P[5])-(48+480); if (M=0) or (M&gt;12) then exit; if P[6] in ['-','/'] then begin inc(P); dec(L); end; // allow YYYY-MM-DD if L&gt;=8 then begin // YYYYMMDD D := ord(P[6])*10+ord(P[7])-(48+480); if (D=0) or (D&gt;MonthDays[true][M]) then exit; // worse is leap year=true end; end else M := 1; if M&gt;2 then // inlined EncodeDate(Y,M,D) dec(M,3) else if M&gt;0 then begin inc(M,9); dec(Y); end; with Div100(Y) do result := (146097*YDiv100) shr 2 + (1461*YMod100) shr 2 + (153*M+2) div 5+D-693900; if (L&lt;15) or not(P[8] in [' ','T']) then exit; end; H := ord(P[9])*10+ord(P[10])-(48+480); if P[11]=':' then inc(P); // allow hh:mm:ss MI := ord(P[11])*10+ord(P[12])-(48+480); if P[13]=':' then inc(P); // allow hh:mm:ss SS := ord(P[13])*10+ord(P[14])-(48+480); if (H&lt;24) and (MI&lt;60) and (SS&lt;60) then // inlined EncodeTime() result := result + (H * (MinsPerHour * SecsPerMin * MSecsPerSec) + MI * (SecsPerMin * MSecsPerSec) + SS * MSecsPerSec) / MSecsPerDay; end; </code></pre> <p>This is able to handle a very fast conversion from an UTF-8 encoded buffer into a <code>TDateTime</code>. For all constants dependencies, check the unit source code.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload