Page 1 of 1

Miliseconds to more readable time display

Posted: Sun Feb 19, 2006 1:12 pm
by Roger
Code updated For 5.20+

Having asked so much and returned so few I thought I'd share a piece of code that I currently use in a timer application that I'm making. It is a function that transforms a given number of miliseconds into an array with three values (hours, minutes, seconds). I couldn't find it in the forums, but if it is already there, please delete this thread :P

Code: Select all

Procedure MsToTimeDisplay(ms.l, mode.l)
  If ms.l > 3600000
   hrs.l = Round((ms.l/3600000),0)
   ms.l = ms.l - (3600000*hrs.l)
  EndIf
  If ms.l > 60000
   mins.l = Round((ms.l/60000),0)
   ms.l = ms.l - (60000*mins.l)
  EndIf
  If ms.l > 1000
   sec.l = Round((ms.l/1000),0)
   ms.l = ms.l - (1000*sec.l)
  EndIf
  
  Dim time_s.s(3)
  Dim time_l.l(3)
  
  If hrs.l < 10
    time_s(0) = "0"+Str(hrs.l)
  Else
    time_s(0) = Str(hrs.l)
  EndIf
  time_l(0) = hrs.l
  
  If mins.l < 10
    time_s(1) = "0"+Str(mins.l)
  Else
    time_s(1) = Str(mins.l)
  EndIf
  time_l(1) = mins.l
  
  If sec.l < 10
    time_s(2) = "0"+Str(sec.l)
  Else
    time_s(2) = Str(sec.l)
  EndIf
  time_l(2) = sec.l 
  
  If mode.l = 1
    ProcedureReturn time_s()
  Else
    ProcedureReturn time_l()
  EndIf

EndProcedure
As you can see, the mode determines whether the procedure returns the time as long or as string. Also, the function is quite easy to make more precise. For example, if you want it to display the number of miliseconds left as well you just add:

Code: Select all

; Change the array declaration (add one more field to both)
Dim time_s.s(4)
Dim time_l.l(4)

; Add the following lines
time_s(3) = Str(ms.l)
time_l(3) = ms.l
I didn't need it to display more than hours (days for example), but you can add that very easily as well. Add this before everything else in the procedure:

Code: Select all

If ms.l > 86400000
   days.l = Round((ms.l/86400000),0)
   ms.l = ms.l - (86400000*days.l)
EndIf
And voila, you have the number of days...

Posted: Sun Feb 19, 2006 1:54 pm
by Thomas

Code: Select all

Procedure.s time(t) 
  t = t / 1000 
  s = t % 60
  t = t / 60
  m = t % 60
  t = t / 60
  ProcedureReturn RSet(Str(t), 2, "0") + ":" + RSet(Str(m), 2, "0") + ":" + RSet(Str(s), 2, "0") 
EndProcedure 

Posted: Sun Feb 19, 2006 1:58 pm
by Roger
I knew it was risky business posting something here... :P

Posted: Sun Feb 19, 2006 2:26 pm
by Thomas
:wink:

Posted: Sun Feb 19, 2006 4:35 pm
by Intrigued
Roger wrote:I knew it was risky business posting something here... :P
I'm starting to see the light of it being good to post your code (sloppy, long-winded, bug filled, or highly optimized). Because you sure do get folks to either say "thata' boy (girl)" or "P/U that one's a stinker" of sorts. So, in the end we all can learn from the response to each of our posts.

*thumbs up* and thanks for sharing the code guys!

Posted: Sun Feb 19, 2006 4:41 pm
by Thomas
Should be moved to the Tips & Tricks section, tho.

Posted: Sun Feb 19, 2006 7:13 pm
by Straker
Thomas wrote:Should be moved to the Tips & Tricks section, tho.
Feel free to post it there since its your code.

Posted: Sun Feb 19, 2006 9:07 pm
by Airr
Thomas wrote:

Code: Select all

Procedure.s time(t) 
  t = t / 1000 
  s = t % 60
  t = t / 60
  m = t % 60
  t = t / 60
  ProcedureReturn RSet(Str(t), 2, "0") + ":" + RSet(Str(m), 2, "0") + ":" + RSet(Str(s), 2, "0") 
EndProcedure 
My 2cents :)

Code: Select all

Procedure.s time(msVal)
  ProcedureReturn FormatDate("%hh:%ii:%ss",msVal/1000)
EndProcedure

Debug time(9000000)
Sep Arrays:

Code: Select all

ms=9000000
Dim time_s.s(3)

Procedure.s time(msVal.l,field.l)
ret.s = FormatDate("%hh:%ii:%ss",msVal/1000)
For cnt = 1 To 3
 time_s(cnt) = StringField(ret,cnt,":")
Next
ProcedureReturn time_s(field)
EndProcedure

Debug time(ms,1)
Debug time(ms,2)
Debug time(ms,3)