Miliseconds to more readable time display

Share your advanced PureBasic knowledge/code with the community.
Roger
User
User
Posts: 33
Joined: Thu Feb 16, 2006 1:53 pm

Miliseconds to more readable time display

Post 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...
Thomas
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Apr 26, 2003 8:45 pm

Post 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 
Roger
User
User
Posts: 33
Joined: Thu Feb 16, 2006 1:53 pm

Post by Roger »

I knew it was risky business posting something here... :P
Thomas
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Apr 26, 2003 8:45 pm

Post by Thomas »

:wink:
Intrigued
Enthusiast
Enthusiast
Posts: 501
Joined: Thu Jun 02, 2005 3:55 am
Location: U.S.A.

Post 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!
Intrigued - Registered PureBasic, lifetime updates user
Thomas
Enthusiast
Enthusiast
Posts: 112
Joined: Sat Apr 26, 2003 8:45 pm

Post by Thomas »

Should be moved to the Tips & Tricks section, tho.
Straker
Enthusiast
Enthusiast
Posts: 701
Joined: Wed Apr 13, 2005 10:45 pm
Location: Idaho, USA

Post by Straker »

Thomas wrote:Should be moved to the Tips & Tricks section, tho.
Feel free to post it there since its your code.
User avatar
Airr
User
User
Posts: 49
Joined: Tue Oct 04, 2005 4:29 am
Contact:

Post 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)
"Programming is like Poetry. Sometimes the words just escape you..." -me, to my manager.
Post Reply