Einzelnen Beitrag anzeigen

thomasschaf
(Gast)

n/a Beiträge
 
#1

Vorstellung des Codes zur übersichtlichen Anzeige von Zeiten

  Alt 25. Mär 2012, 23:26
Hallo,

häufig geht man mit Zeiten in Millisekunden um, wie z.B. bei GetTickCount.
Um diese übersichtlich darzustellen, habe ich mal eine Funktion geschrieben.

Ich bitte um Feedback.

Delphi-Quellcode:
//notwendige Hilfs-Funktionen:
function EndsWith(Text, f: string): boolean;
begin
  Result := False;
  if Length(Text) < Length(f) then
    Exit;
  if CopyL(Text, (Length(Text) - Length(f) + 1), Length(Text)) = f then
    Result := True;
end;

function HintenEntfernen(Text, H: string): string;
begin
  Result := Text;
  if EndsWith(Text, H) then
    Result := Copy(Text, 1, Length(Text) - Length(H));
end;
Delphi-Quellcode:
function ZeitString(msec: integer): string;
type
  Einheit = record
    Bez: string;
    Mul: integer;
    Bek: integer;
    Res: integer;
  end;
const
  p = ', ';
var
  n: array of Einheit;
  c: integer = 1;
  i, j, u, a: integer;

  procedure SetN(ABez: string; AMul: integer);
  begin
    SetLength(n, c + 2);
    n[c].Bez := ABez;
    n[c].Mul := AMul;
    Inc(c);
  end;

begin
  Result := '';

  SetN('ms', 1);
  SetN('sec', 1000);
  SetN('min', 60);
  SetN('hrs', 60);
  SetN('day', 24);
  //...

  j := 1;
  for i := 0 to High(n) do
  begin
    if n[i].Bez = 'then
      Continue;
    j := j * n[i].Mul;
    n[i].Bek := j;
  end;

  u := msec;
  for i := High(n) downto 0 do
  begin
    if n[i].Bez = 'then
      Continue;

    n[i].Res := u div n[i].Bek;
    u := u mod n[i].Bek;
  end;

 for i := High(n) downto 0 do
  begin
    if n[i].Bez = 'then
      Continue;

    a := n[i].Res;
    if (a <> 0) then
      Result := Result + IntToStr(a) + ' ' + n[i].Bez + p;
  end;

  Result := HintenEntfernen(Result, p);
end;
Hoffentlich ist alles nachvollziehbar, sonst nachfragen.

Grüße
  Mit Zitat antworten Zitat