SRL Archive - SRL Archives

IntToTime


Rich

2009-08-26 18:40:30

{*******************************************************************************
function IntToTime(MS : Integer) : string;
By: R1ch
Description: Converts MS to H/M/S/MS time
*******************************************************************************}
function IntToTime(MS : Integer) : string;
var
I : Integer;
HMS, HMSc : TIntegerArray;

begin
HMS:= [1000, 60000, 3600000];
SetLength(HMSc, 3);
for I:= 2 downto 0 do
begin
repeat
if (MS > HMS[I]) then
begin
MS:= MS - HMS[I];
Inc(HMSc[I]);
end;
until(MS < HMS[I]);
end;
Result:= (IntToStr(HMSc[2]) + ' Hours, ' + IntToStr(HMSc[1]) + ' Minutes, ' + IntToStr(HMSc[0]) + ' Seconds and ' + IntToStr(MS) + ' Milliseconds');
end;

Converts milliseconds to hours, minutes, seconds and milliseconds.

Example:

IntToTime(5214622);

outputs

1 Hours, 26 Minutes, 54 Seconds and 622 Milliseconds

It is used like:
WriteLn(IntToTime(5214622));

Richard.

caused

2009-08-26 19:06:16

Nice =). I often did that "manually". Such function is quite usefull :).
*saves it to ownsrlinclude.scar* ;).

~caused

Rich

2009-08-26 19:07:39

Thanks. You do know there is another function out there which does this - MsToTime? I only remembered after I'd made this though. :o

caused

2009-08-26 19:18:22

From: R1ch
Thanks. You do know there is another function out there which does this - MsToTime? I only remembered after I'd made this though. :o


really?… Well yours sounds much cooler :'D. So, when i need it ill use yours and add some credits ;).

~caused

Rich

2009-08-26 19:26:57

Timing.scar
function MsToTime(MS, TheType: Integer): string;
var
STA: array [0..4] of TVariantArray;
Time: array [0..2] of Integer;
i: Integer;
begin
Result := '';
if (not(InRange(TheType, 0, High(STA)))) then
begin
srl_Warn('MsToTime', 'TheType: ''' + IntToStr(TheType) + ''' is not a valid value. Using Time_Bare.', warn_AllVersions);
TheType := Time_Bare;
end;

STA[0] := [' Hours, ', ' Minutes and ', ' Seconds', False, 0];
STA[1] := ['h ', 'm ', 's', False, 2];
STA[2] := [' hr ', ' min ', ' sec', False, 0];
STA[3] := [':', ':', '', True, 2];
STA[4] := ['.', '.', '', True, 2];

ConvertTime(MS, Time[0], Time[1], Time[2]);
for i := 0 to 2 do
if (Time[i] > 0) or (STA[TheType][3]) or (i = 2) then
Result := Result + PadZ(IntToStr(Time[i]), STA[TheType][4]) + STA[TheType][i];
end;
However, for what it does, MSToTime seems to be over-complicated, I think.

Thanks,
Richard.

Sir R. M8gic1an

2009-08-28 12:39:41

Not getting included…

~RM

Nava2

2009-08-28 18:58:46

MSToTime allows for MANY different formats :)

Edit: Is also faster.