Recent

Author Topic: If I have number of milliseconds that passed how to I convert it to HH:MM:SS?  (Read 12829 times)

vonskie

  • Full Member
  • ***
  • Posts: 184
For example X number of milliseconds is a up time of 30 hours 05 minutes and 15 seconds

No clue please assist.


fred

  • Full Member
  • ***
  • Posts: 201
This seems to work:

Code: Pascal  [Select][+][-]
  1.   writeln(TimeToStr(IncMilliSecond(0, 5000000)));

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Note the format for the underlying FormatDateTime is wrong. "hh"=hours, but 'mm' = MONTHS!.

'nn' = minutes. zzz is milliseconds

The proper format string would be hh:nn:ss:zzz
Code: Pascal  [Select][+][-]
  1. program testdatetime;
  2. uses sysutils;
  3. var
  4.   start:TDateTime;
  5. begin
  6.   start:=Now;
  7.   Sleep(3333); // wait a bit
  8.   Writeln ('Elapsed: ',FormatDateTime('hh:nn:ss:zzz',Now-start));
  9. end.

See http://www.freepascal.org/docs-html/rtl/sysutils/formatdatetime.html and http://www.freepascal.org/docs-html/rtl/sysutils/formatchars.html
« Last Edit: February 26, 2017, 10:59:41 am by Thaddy »
Specialize a type, not a var.

wp

  • Hero Member
  • *****
  • Posts: 11855
In addition to Thaddy's answer about the FormatDateTime function, you must also convert your numbers given in milliseconds to the units used by FormatDateTime which is days. So, you must divide the milliseconds by the number of milliseconds per day, i.e. by 24*60*60*1000 (or you use the constant MSecsPerDay definded in SysUtils).

And another remark for FormatDateTime: The usual symbol "h" limits the hour value to 23. If your time span is longer than one day you must put the "h" in square brackets (like in Excel), and add the parameter [fdoInterval]:

Code: Pascal  [Select][+][-]
  1. function msToHourMinSec(AMilliseconds: Integer): String;
  2. var
  3.   t: Double;
  4. begin
  5.   t := AMilliseconds/ MSecsPerDay;
  6.   Result := FormatDateTime('[h]:nn:ss.zzz', t, [fdoInterval]);
  7.  
  8.   {
  9.   // or: if you want to split off full days
  10.   if t < 1 then
  11.     Result := FormatDateTime('h:nn:ss.zzz', t)
  12.   else
  13.     Result := IntToStr(trunc(t)) + ' days ' + FormatDateTime('h:nn:ss.zzz', t);
  14.   }
  15. end;
« Last Edit: February 26, 2017, 11:40:22 pm by wp »

wp

  • Hero Member
  • *****
  • Posts: 11855
Note the format for the underlying FormatDateTime is wrong. "hh"=hours, but 'mm' = MONTHS!.

'nn' = minutes. zzz is milliseconds
Not necessarily. If the "m" is surrounded by time symbols "h" and or "s" then FormatDateTime guesses that it must be "minutes" instead of "months". But of course, using "n" is on the safe side.
« Last Edit: February 26, 2017, 11:20:45 am by wp »

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Not necessarily. If the "m" is surrounded by time symbols "h" and or "s" then FormatDateTime guesses that it must be "minutes" instead of "months". But of course, using "n" is on the safe side.
Then we have a documentation bug: I was not aware of that "guessing". Is it Delphi compatible? Where is that "guessing" documented? If so probably only in the sourcecode?
Specialize a type, not a var.

wp

  • Hero Member
  • *****
  • Posts: 11855
Delphi 7  and Berlin do the same.

It's not in the docs, not even in the source code. Delphi does not document it either (http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/SysUtils_FormatDateTime@string@TDateTime.html).

[EDIT]
I should be more specific in how this "guessing" is done. The implementation of FormatDateTime (or: DateTimeToString, to be precise) uses to boolean variable "TimeFlag" to decide on whether "m" refers to "month" or "minute". The TimeFlag is set for the codes referring to a time format of the FormatSettings, such aus "t" = FormatSettings.ShortTimeFormat. In this case the context is clear, and there is no "guessing" at all.
« Last Edit: February 26, 2017, 12:38:09 pm by wp »

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Hmm. So it needs a doc update....
Specialize a type, not a var.

ASerge

  • Hero Member
  • *****
  • Posts: 2222
Delphi does not document it either...
Read carefully ;D From url:
Quote
m - Displays the month as a number with a leading zero (01-12). If the mm specifier immediately follows an h or hh specifier, the minute rather than the month is displayed.
The same for Delphi 7 docs.

guest60499

  • Guest
It seems like the question has been answered, but if you wanted to do it yourself, look at the implementation of gmtime.

Manipulation of dates in this vein is done by converting everything into days internally, requiring adjustments based on century and millennium.

fred

  • Full Member
  • ***
  • Posts: 201
Quote
The same for Delphi 7 docs.
The same text shows in the Delphi 5 help file from 1999 :)

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Yes... But the point I was making that there is a bug in OUR documentation, not Delphi's. And I never used m when I meant n.
http://www.freepascal.org/docs-html/rtl/sysutils/formatchars.html

I filed a bug report against the above doc.

OP's question should be answered anyway.
« Last Edit: February 27, 2017, 11:15:37 am by Thaddy »
Specialize a type, not a var.

wp

  • Hero Member
  • *****
  • Posts: 11855

Thaddy

  • Hero Member
  • *****
  • Posts: 14204
  • Probably until I exterminate Putin.
Thanks, I did too, but yours is more eloquent and covers depth.
Specialize a type, not a var.

 

TinyPortal © 2005-2018