unit MyFunc;
interface
function ToSec(_h, _m, _s: Integer; _ms: Extended): Extended; overload;
function ToSec(_h, _m: Integer; _s: Extended): Extended; overload;
function ToSec(_ms: Extended): Extended; overload;
function ToSec(S: String): Extended; overload;
function SecToStr(sec: Extended; IsMilliSec: Boolean=true): String;
procedure SecBreak(_time: Extended; var _h, _m, _s, _ms: Integer); overload;
procedure SecBreak(_time: Extended; var _h, _m, _s: Integer); overload;
implementation
uses SysUtils;
Function ToSec(_h, _m, _s: Integer; _ms: Extended): Extended;
begin
Result:=_h*3600+_m*60+_s+_ms/1000;
end;
Function ToSec(_h, _m: Integer; _s: Extended): Extended;
begin
Result:=_h*3600+_m*60+_s;
end;
Function ToSec(_ms: Extended): Extended;
begin
Result:=_ms/1000;
end;
Function MyVal(S: String): Integer;
var Res: Extended;
startPos, endPos, Err: Integer;
begin
startPos:=1;
endPos:=length(S);
while (startPos<=length(S)) and (S[startPos]=' ') do Inc(startPos);
while (endPos>=1) and (S[endPos]=' ') do Dec(endPos);
S:=copy(S,startPos,endPos);
for Err:=length(S) downto 1 do
if S[Err]=',' then S[Err]:='.';
val(S,Res,Err);
if (Res>-1e-8) and (Res<2147483647.1) then
Result:=Round(Res)
else
Result:=0;
end;
Function ToSec(S: String): Extended;
var i,posComma, posComma2: Integer;
Res: Extended;
begin
PosComma2:=0;
for i:=Length(S) downto 1 do
if S[i]=':' then begin PosComma2:=i; break; end;
posComma:=posComma2;
for i:=PosComma2+1 to Length(S) do
if (S[i]=',') or (S[i]='.') then begin posComma:=i; break; end;
posComma2:=Length(S)+1; for i:=posComma+1 to Length(S) do
if (S[i]=',') or (S[i]='.') then begin posComma2:=i; break; end;
if posComma>0 then begin
Res:=MyVal(copy(S,posComma+1,posComma2-posComma-1))/1000;
posComma2:=posComma;
end
else begin
Res:=0;
end;
posComma:=0;
for i:=(posComma2-1) downto 1 do
if S[i]=':' then begin posComma:=i; break; end;
Res:=Res+MyVal(copy(S,posComma+1,posComma2-posComma-1));
posComma2:=posComma;
posComma:=0;
for i:=(posComma2-1) downto 1 do
if S[i]=':' then begin posComma:=i; break; end;
Result:=Res+3600*MyVal(copy(S,1,posComma-1))+60*MyVal(copy(S,posComma+1,posComma2-posComma-1));
end;
procedure SecBreak(_time: Extended; var _h, _m, _s, _ms: Integer);
var TmpMilliSec: Integer;
begin
TmpMilliSec:=Round(_time*1000);
_h:=TmpMilliSec div 3600000;
TmpMilliSec:=(TmpMilliSec-_h*3600000);
_m:=TmpMilliSec div 60000;
TmpMilliSec:=(TmpMilliSec-_m*60000);
_s:=TmpMilliSec div 1000;
_ms:=TmpMilliSec-_s*1000;
end;
procedure SecBreak(_time: Extended; var _h, _m, _s: Integer);
var TmpSec: Integer;
begin
TmpSec:=Round(_time);
_h:=TmpSec div 3600;
TmpSec:=(TmpSec-_h*3600);
_m:=TmpSec div 60;
_s:=TmpSec-_m*60;
end;
function IntToStrN(i: Integer; MinLen: Integer): String;
var Res: String;
begin
Res:=IntToStr(i);
while (Length(Res)<MinLen) do Res:='0'+Res;
Result:=Res;
end;
function SecToStr(sec: Extended; IsMilliSec: Boolean): String;
var _h, _m, _s, _ms: Integer;
TmpMilliSec: Integer;
begin
TmpMilliSec:=Round(sec*1000);
_h:=TmpMilliSec div 3600000;
TmpMilliSec:=(TmpMilliSec-_h*3600000);
_m:=TmpMilliSec div 60000;
TmpMilliSec:=(TmpMilliSec-_m*60000);
_s:=TmpMilliSec div 1000;
if IsMilliSec then begin
_ms:=TmpMilliSec-_s*1000;
Result:=IntToStr(_h)+':'+IntToStrN(_m,2)+':'+IntToStrN(_s,2)+'.'+IntToStrN(_ms,3);
end
else
Result:=IntToStr(_h)+':'+IntToStrN(_m,2)+':'+IntToStrN(_s,2);
end;
end.
|