Return to Snippet

Revision: 29651
at August 2, 2010 18:20 by icebob


Initial Code
function SecondsToTimeString(Seconds: Int64): string;
const
  itemNames: TTimeItemNames = ('year', 'month', 'day', 'h', 'm', 's');
begin
  result := SecondsToTimeString(Seconds, itemNames);
end;

function SecondsToTimeString(Seconds: Int64; const itemNames: TTimeItemNames): string;
const
  divisors: array [0..5] of Int64 = (SecsPerDay * 365, SecsPerDay * 31, SecsPerDay, SecsPerHour, SecsPerMin, 1);

var
  resCount: integer;
  I: Integer;
  C, V: Int64;
begin
  result := '';
  resCount := 0;
  C := Seconds;
  for I := Low(divisors) to High(divisors) do
  begin
    V := C div divisors[I];
    if V > 0 then
    begin
      if resCount > 0 then
        result := result + ' ';
      result := result + IntToStr(V) + itemNames[I];
      Inc(resCount);
      if resCount > 1 then break;
      C := C mod divisors[I];
    end;
  end;
end;

Initial URL

                                

Initial Description
SecondsToTimeString(Seconds: Int64): string;
Convert a seconds integer to time string like: '4m 35s' or '2 month 21 day'

Initial Title
Convert seconds to string ( like '4h 13m' )

Initial Tags

                                

Initial Language
Delphi