0

I'm using the code How to show percent done, elapsed time and estimated time progress?

I would like to replace the numbers by "seconds", "minutes" and "hours", that is, instead of "0:01:37", I would like to display: "1 minute 37 seconds". Is it possible?

Community
  • 1
  • 1
Nico Z
  • 997
  • 10
  • 33
  • Do you mean that instead of times like "3:20:15" you want to show "3 hours, 20 minutes and 15 seconds"? Or just "3 hours" and only later when the time is like "0:20:15", you will want to display "20 minutes"? – Martin Prikryl Feb 01 '17 at 13:00
  • If possible, I would like to replace the numbers by seconds, minutes and hours, that is, instead of 0:01:37, I would like to display: 1 minute 37 seconds. – Nico Z Feb 01 '17 at 13:11
  • So what's stopping you? Change the last line to display whatever you want; you have the values and the format string. Alter the format string. – Ken White Feb 01 '17 at 14:16

2 Answers2

2

Just change the format string being provided for Format to whatever you want to display.

function TicksToStr(Value: DWORD): string;
var
  I: DWORD;
  Hours, Minutes, Seconds: Integer;
begin
  I := Value div 1000;
  Seconds := I mod 60;
  I := I div 60;
  Minutes := I mod 60;
  I := I div 60;
  Hours := I mod 24;
  Result := Format('%.2d hours, %.2d minutes, %.2d seconds', [Hours, Minutes, Seconds]);
end;
Ken White
  • 123,280
  • 14
  • 225
  • 444
1

Bit smarter implementation that skips zeros and handles plural:

procedure AddTime(var S: string; Count: Integer; L: string);
begin
  if Count > 0 then
  begin
    if S <> '' then
    begin
      S := S + ' ';
    end;
    if Count > 1 then L := L + 's';
    S := S + Format('%d %s', [Count, L]);
  end;
end;

function TicksToStr(Value: DWORD): string;
var
  I: DWORD;
  Hours, Minutes, Seconds: Integer;
begin
  I := Value div 1000;
  Seconds := I mod 60;
  I := I div 60;
  Minutes := I mod 60;
  I := I div 60;
  Hours := I mod 24;

  AddTime(Result, Hours, 'hour');
  AddTime(Result, Minutes, 'minute');
  AddTime(Result, Seconds, 'second');
  if Result = '' then Result := '-'; { no time }
end;
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • What is not displayed correctly? Please learn to describe your problem! Is it about 0 seconds remaining? – Martin Prikryl Feb 01 '17 at 19:31
  • 1
    I've edited my answer already - Replace the `'-'` with whatever you want to display for zero. – Martin Prikryl Feb 01 '17 at 19:37
  • Another question, Is it possible to reduce or adjust the time so that the zero does not extend as much? That is, I see that time zero is maintained for a long time (Sorry for my bad english). – Nico Z Feb 01 '17 at 21:08
  • This has nothing to do with this question. Anyway, I do not think it's possible. – Martin Prikryl Feb 02 '17 at 06:15