This timespan snippet provides time based string conversion from a provided measure of time in milliseconds. These functions can be used to quickly build strings for application messages or user interface elements which need to reflect a certain amount of time elapsed.
Use Timespan$() to convert a number of milliseconds into a formatted time string. For example, 65000 milliseconds will return "00:01:05", that is 0 hours, 1 minute and 5 seconds.
Use the ToSeconds$(), ToMinutes$() and ToHours$() expressions to return the number of seconds, minutes or hours respectively. The ToSecond function requires an additional parameter, the number of decimal points to return; so that a more precise reflection can be returned when expressing small durations of time, such as 0.5 seconds which is 500 milliseconds.
`======================
// Chris Tate - binarymodular.com
// chris.tate@binarymodular.com
// Timespan String Expressions
//==================================================
// Test
Print Timespan$(1000)
Print Timespan$(65000 )
Print Timespan$(1000*61)
Print Timespan$(1000*60*60)
Print Timespan$(1000*60*60*12)
Print Timespan$(1000*60*60*24)
Print
Print ToMinutes$(1000*60)
Print ToMinutes$(1000*90)
Print ToHours$(1000*60*60)
Print ToHours$(1000*60*65)
Print ToSeconds$(1000,0)
Print ToSeconds$(1000*0.5,1)
Print ToSeconds$(1000*5,1)
Print ToSeconds$(1000*65,1)
Wait key
End
//==================================================
Function ToHours$( iTimerMS )
If iTimerMS = 0 Then ExitFunction "0"
t# = iTimerMS
t# = t# / 60 / 60 / 1000
Split String Str$(t#,2), "."
m# = Val( Split Word$(2) )
m# = m# * 0.60606060606
s$ = Str$(t#,0) + ":" + Padleft$( Str$(m#,0), "0" , 2 )
endfunction S$
//==================================================
Function ToMinutes$( iTimerMS )
If iTimerMS = 0 Then ExitFunction "0"
t# = iTimerMS / 60000
Split String Str$(t#,2), "."
m# = Val( Split Word$(2) )
m = m * 0.60606060606
s$ = Str$(t#,0)
endfunction S$
//==================================================
Function ToSeconds$( ms, iDecimalPoints )
t$ = Str$(ms * 0.001, iDecimalPoints)
EndFunction t$
//==================================================
Function TimeSpan$(iTimeMS)
Local sTime$ = "H:M:S"
If iTimeMS <= 0
sTime$ = "00:00:00"
ELse
Local fHours# : fHours# = (iTimeMS * 1.0)/ (60.0*1000.0*60.0)
sTime$ = Replace All$(sTime$,"H", PadLeft$(Str$(fHours#,0),"0",2))
Local fMinutesLeft# : fMinutesLeft# = Val( "0." + Select$( Str$(fHours#,4), ".",2,0) ) * 60.0
sTime$ = Replace All$(sTime$,"M", PadLeft$( Str$(fMinutesLeft#,0),"0",2))
Local fSecondsLeft# : fSecondsLeft# = Val( "0." + Select$( Str$(fMinutesLeft#,4), ".",2,0) ) * 60.0
sTime$ = Replace All$(sTime$,"S", PadLeft$( Str$(fSecondsLeft#,0),"0",2))
Endif
EndFunction sTime$
//=====================================================
Function Select$(sCSV$,sDelimiter$,iEntry,iDefault)
Local t$
Split String sCsv$, sDelimiter$
If Split Count() > 0
If iEntry => 1 And iEntry <= Split Count()
t$ = Split Word$(iEntry)
Else
If Within(iDefault, 1, Split Count())
t$ = Split Word$( Clamp(iDefault,1,Split Count()) )
Else
t$ = ""
EndIf
Endif
Endif
Endfunction t$
//==================================================
Function Within( iVal, iMin, iMax )
If iVal < iMin Then ExitFunction 0
If iVal > iMax Then ExitFunction 0
EndFunction 1