Wednesday, January 30, 2008

Calculate estimated Download time of a File


{ Add this under your type declaration }

type
TDRec = record
H, M, S: Integer;
end;

const
Count = 6;
BpsArray: array [0..Count] of Integer = (14400, 28800, 33600, 56000, 64000, 128000, 512000);

function CalculateDLTime(const Value, Units, Connection: Integer): TDRec;
var
i, size_bits, filedltimesec, hourmod, HH, MM, SS: Integer;
Rec: TDRec;

function pow(a, b: Integer): Integer;
function sl(nr, times: Integer): Integer;
var
i: Integer;
begin
Result := nr * nr;
for i := 0 to times do Result := Result + nr * nr;
end;
begin
if a > b then
Result := sl(a, b)
else
Result := sl(b, a);
end;
begin
case Units of
1: size_bits := (8 div 1) * Value; // bytes
2: size_bits := (8 div 1) * ((pow(2,10)) div 1) * Value; // kilobytes
3: size_bits := (8 div 1) * ((pow(2,20)) div 1) * Value; // Megabytes
end;

// Calculate
filedltimesec := Round(size_bits) div BpsArray[Connection];

hourmod := filedltimesec mod (60 * 60); // Modulus.
HH := Floor(filedltimesec / (60 * 60));
MM := Floor(hourmod / 60);
SS := Floor(filedltimesec mod 60); // Modulus.

if SS > 0 then Inc(SS);

with Rec do
begin
H := HH;
M := MM;
S := SS;
end;

Result := Rec;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
i: Integer;
Rec: TDRec;
begin
ListView1.Items.Clear;

for i := 0 to Count do
begin
Rec := CalculateDLTime(StrToInt(Edit1.Text), ComboBox1.ItemIndex + 1,i);

with ListView1.Items.Add do
begin
Caption := NameArray[i];
SubItems.Add(IntToStr(Rec.H));
SubItems.Add(IntToStr(Rec.M));
SubItems.Add(IntToStr(Rec.S));
end;
end;
end;

No comments: