SRL Archive - SRL Archives

Function msToTime(ms: integer): string;


Sir R. M8gic1an

2009-04-22 09:15:59


Function msToTime(ms: integer): string;
var
RHours, Minutes, Seconds, RMinutes, RSeconds: LongInt;
Time: string;
begin
Seconds := ms div 1000;
Minutes := Seconds div 60;
RHours := Minutes div 60;
Time := IntToStr(Seconds) + ' Seconds';
if Minutes <> 0 then
begin
RSeconds := Seconds mod (Minutes * 60);
Time := IntToStr(Minutes) + ' Minutes and ' + IntToStr(RSeconds) +
' Seconds';
end;
if RHours <> 0 then
begin
RMinutes := Minutes mod (RHours * 60);
RSeconds := Seconds mod (Minutes * 60);
Time := IntToStr(RHours) + ' Hours, ' + IntToStr(RMinutes) +
' Minutes and ' + IntToStr(RSeconds) + ' Seconds';
end;
Result := Time;
end;

{*******************************************************************************
function TimeRunning: String;
By: from Phalanx's script/RsN
Description: Returns Time since ST (Script StartTime).
*******************************************************************************}

function TimeRunning: String;
begin
Result := msToTime(GetTimeRunning);
end;

I don't feel comfortable taking credit for one line :/ or worse, one variable >.>

anyway, just thought it might be better broken down like this, allows people to use it for more things.

~RM

ZephyrsFury

2009-04-22 09:41:09

How bout something like this:


function MsToTime(Ms, TimeStringType: Integer): string;


TimeStringType:
1: '9 Hours, 9 Minutes and 9 seconds';
2: '9h, 9m, 9s';
3: '09:09:09';


?

Anyone think thats a good idea? I can add other strings too.

Sir R. M8gic1an

2009-04-22 09:49:46

sounds good, you might as well just make it an array;

case TimeStringType of
0: TSA := ['Hours,', 'Minutes,', 'Seconds'];
1: TSA := ['h,', 'm,', 's'];
2: TSA := [':', ':', ''];
end;

//...
RSeconds := Seconds mod (Minutes * 60);
Time := IntToStr(Minutes) + TSA[1] + IntToStr(RSeconds) +
TSA[2];

~RM

ZephyrsFury

2009-04-22 12:02:38

From: Rasta Magician
sounds good, you might as well just make it an array;


Good idea!

Heres what I've come up with:


function MsToTime(MS, StrType: Integer): string;
var
STA: array [1..4] of TVariantArray;
Sec, H, M, S: Integer;
begin
STA[1] := [' Hours, ', ' Minutes and ', ' Seconds', False, 0];
STA[2] := ['h, ', 'm, ', 's', False, 2];
STA[3] := [' hr, ', ' min, ', ' sec', False, 0];
STA[4] := [':', ':', '', True, 2];
if (not(InRange(StrType, 1, Length(STA)))) then
begin
srl_Warn('MsToTime', 'StrType: ' + IntToStr(StrType) + ' is not a valid value (1, 2, 3, 4 only)', warn_AllVersions);
Exit;
end;
Sec := Ms / 1000;
H := Floor(Sec / 3600.0);
M := Floor(Sec / 60.0 - H * 60);
S := Floor(Sec - H * 3600 - M * 60);
Result := '';
if (Sec > 3599) or (STA[StrType][3]) then Result := Result + PadZ(IntToStr(H), STA[StrType][4]) + STA[StrType][0];
if (Sec > 59) or (STA[StrType][3]) then Result := Result + PadZ(IntToStr(M), STA[StrType][4]) + STA[StrType][1];
Result := Result + PadZ(IntToStr(S), STA[StrType][4]) + STA[StrType][2];
end;

Anyone have any other suggestions for time-string formats?

Wizzup?

2009-04-22 12:27:48

Adding constants. time_fulltime, time_shorttime, etc.

ZephyrsFury

2009-04-22 13:04:00

From: Wizzup?
Adding constants. time_fulltime, time_shorttime, etc.


For something like 4 or 5 different formats I think constants will make it more confusing.

Wizzup?

2009-04-22 13:40:01

From: ZephyrsFury
For something like 4 or 5 different formats I think constants will make it more confusing.


I think inserting just a plain number makes no sense. You'll see that in every possible library or program, plain numbers make no sense to coder himself, nor to people who try to read your code, they are instead, constants… (That is what constants and enums are for. usually.)

For example:

typedef struct _mail_data {
int cstate;
int iport;
char* clientname;
} *mail_data;


typedef enum _smtp_command {
/* Known SMTP commands. */
SMTP_HELO,
SMTP_MAIL,
SMTP_RCPT,
SMTP_RSET,
SMTP_NOOP,
SMTP_DATA,
SMTP_QUIT,

/* Errors */
SMTP_500,
SMTP_501,
SMTP_502,
SMTP_503,
SMTP_504,

/* Internal errors */
SMTP_OK,
SMTP_FAIL,
SMTP_MAX_COMMAND_VALUE,
SMTP_NONE, /* Indicates that the command is not set. Just for initialisation */
SMTP_ERROR = -1
} smtp_command;

/*
* Bitwise SMTP, for keeping track of used commands.
* Might drop the enum eventually.
*/
#define HELO_SMTP_B 1
#define MAIL_SMTP_B 2
#define RCPT_SMTP_B 4
#define RSET_SMTP_B 8
#define NOOP_SMTP_B 16
#define DATA_SMTP_B 32
#define QUIT_SMTP_B 64;

#define SERVER_NONE 0
#define SERVER_QUIT 1
#define SERVER_SEND 2

#define ARGS_MAX_LENGTH 256
#define MAIL_INIT_SIZE 2048
#define SENDER_INIT_SIZE 100
#define RECIPIENT_INIT_SIZE 100
#define MAX_ARGUMENTS 100
#define HOSTNAME_MAX_SIZE 50

Freddy1990

2009-04-22 15:16:38

Maybe shorten it a bit?

function msToTime(ms: Integer): string;
var
h, m, s: Integer;
begin
ConvertTime(ms, h, m, s);
if h <> 0 then
Result := IntToStr(h) + ' Hours, ' + IntToStr(m) + ' Minutes and ' +
IntToStr(s) + ' Seconds'
else if m <> 0 then
Result := IntToStr(m) + ' Minutes and ' + IntToStr(s) + ' Seconds'
else
Result := IntToStr(s) + ' Seconds';
end;

ZephyrsFury

2009-04-22 15:23:28

From: Freddy1990
Maybe shorten it a bit?

function msToTime(ms: Integer): string;
var
h, m, s: Integer;
begin
ConvertTime(ms, h, m, s);
if h <> 0 then
Result := IntToStr(h) + ' Hours, ' + IntToStr(m) + ' Minutes and ' +
IntToStr(s) + ' Seconds'
else if m <> 0 then
Result := IntToStr(m) + ' Minutes and ' + IntToStr(s) + ' Seconds'
else
Result := IntToStr(s) + ' Seconds';
end;


Oh I thought ConvertTime just returned ms in Hours, Mins and Seconds. Or was that DecodeTime…

Anyway thanks Freddy. :)

Freddy1990

2009-04-22 15:28:02

From: ZephyrsFury
Oh I thought ConvertTime just returned ms in Hours, Mins and Seconds. Or was that DecodeTime…

Anyway thanks Freddy. :)


Hmm, that is what it does and that's all you seem to be needing?

Sir R. M8gic1an

2009-04-22 16:39:50

okay.. i think this will be sort of what it looks like in the end then:

two possibilities


const
s_Hour = 0;
s_Min = 1;
s_Sec = 2;
FullTime = 3;
TimeLength = 4;

function MsToTime(MS, StrType: Integer): string;
var
STA: array [1..4] of TVariantArray;
H, M, S: Integer;
begin
STA[1] := [' Hours, ', ' Minutes and ', ' Seconds', False, 0];
STA[2] := ['h, ', 'm, ', 's', False, 2];
STA[3] := [' hr, ', ' min, ', ' sec', False, 0];
STA[4] := [':', ':', '', True, 2];

if (not(InRange(StrType, 1, Length(STA)))) then
begin
srl_Warn('MsToTime', 'StrType: ' + IntToStr(StrType) + ' is not a valid value (1, 2, 3, 4 only)', warn_AllVersions);
Exit;
end;

ConvertTime(MS, H, M, S);

Result := '';
if (H > 0) or (STA[StrType][FullTime]) then
Result := Result + PadZ(IntToStr(H), STA[StrType][TimeLength]) + STA[StrType][s_Hour];
if (M > 0) or (STA[StrType][FullTime]) then
Result := Result + PadZ(IntToStr(M), STA[StrType][TimeLength]) + STA[StrType][s_Min];

Result := Result + PadZ(IntToStr(S), STA[StrType][TimeLength]) + STA[StrType][s_Sec];
end;

but that means that the consts will always load once, even if the person doesn't call the function or


function MsToTime(MS, StrType: Integer): string;
var
STA: array [1..4] of TVariantArray;
H, M, S,
s_Hour, s_Min, s_Sec, FullTime, TimeLength: Integer;
begin

s_Hour := 0;
s_Min := 1;
s_Sec := 2;
FullTime := 3;
TimeLength := 4;

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

if (not(InRange(StrType, 1, Length(STA)))) then
begin
srl_Warn('MsToTime', 'StrType: ' + IntToStr(StrType) + ' is not a valid value (1, 2, 3, 4 only)', warn_AllVersions);
Exit;
end;

ConvertTime(MS, H, M, S);

Result := '';
if (H > 0) or (STA[StrType][FullTime]) then
Result := Result + PadZ(IntToStr(H), STA[StrType][TimeLength]) + STA[StrType][s_Hour];
if (M > 0) or (STA[StrType][FullTime]) then
Result := Result + PadZ(IntToStr(M), STA[StrType][TimeLength]) + STA[StrType][s_Min];

Result := Result + PadZ(IntToStr(S), STA[StrType][TimeLength]) + STA[StrType][s_Sec];
end;

this will allocate the variable each time the function is called…. I'd guess the first one is better.

~RM

Nava2

2009-04-22 17:04:24

type
TTimeType = (Time_Formal, Time_Short, Time_Abbrev, Time_Bare);

function MsToTime(MS: Integer; TheType: TTimeType): string;
var
STA: array [0..3] 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(StrType) + ''' 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];

ConvertTime(MS, Time[0], Time[1], Time[2]);
for i := 0 to 2 do
if (Time[i] > 0) or (STA[TheType][3]) then
Result := Result + PadZ(IntToStr(Time[i]), STA[TheType][4]) + STA[StrType][i];
end;

My input, combined with Wizzup's incessant need for Constants. :)

E: Committed:
const
Time_Formal = 0;
Time_Short = 1;
Time_Abbrev = 2;
Time_Bare = 3;

{*******************************************************************************
function MsToTime(MS, StrType: Integer): string;
By: ZephyrsFury, Nava2, and Rasta Magician
Description: Takes MS in milliseconds and outputs a string with hours, mins and
seconds. Different styles can be created with different StrType values:
Str Type
0: 2 Hours, 47 Minutes and 28 Seconds
1: 02h, 47m, 28s
2: 2 hr, 47 min, 28 sec
3: 02:47:28
*******************************************************************************}

function MsToTime(MS, TheType: Integer): string;
var
STA: array [0..3] 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(StrType) + ''' 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];

ConvertTime(MS, Time[0], Time[1], Time[2]);
for i := 0 to 2 do
if (Time[i] > 0) or (STA[TheType][3]) then
Result := Result + PadZ(IntToStr(Time[i]), STA[TheType][4]) + STA[StrType][i];
end;