Jump to content
Sign in to follow this  
Info

SecondsToTime()

Recommended Posts

Info

$i = 3665.65;1 hour, 1 minute, 5seconds, 65 milliseconds

MsgBox(0, "", _SecondsToTime($i))

Func _SecondsToTime($seconds, $msCount = 2)
    Local $h, $m
    $h = _AddZero(Int($seconds / 3600))
    $seconds -= 3600 * $h
    $m = _AddZero(Int($seconds / 60))
    $seconds -= 60 * $m
    If $msCount = 0 Then
        $seconds = _AddZero(Int($seconds))
    ElseIf $msCount = 1 Then
        $seconds = _AddZero(Int($seconds)) & "." & StringTrimLeft(_AfterDot($seconds, $msCount), StringInStr(_AfterDot($seconds, $msCount), "."))
    Else
        $seconds = _AddZero(Int($seconds)) & "." & _AddZero(StringTrimLeft(_AfterDot($seconds, $msCount), StringInStr(_AfterDot($seconds, $msCount), ".")))
    EndIf
    Return $h & ":" & $m & ":" & $seconds
EndFunc   ;==>_SecondsToTime

Func _AddZero($data)
    Return StringFormat("%02d", $data)
EndFunc   ;==>_AddZero

Func _AfterDot($data, $after)
    Return StringLeft($data, StringInStr($data, ".") + $after)
EndFunc   ;==>_AfterDot

Maybe someone will search for this in the future. :blink:

Edited by Info

Share this post


Link to post
Share on other sites
wraithdu

Not debating usefullness here, but for code clarity:

Func _AddZero($data)
    While StringLen($data) < 2
        $data = "0" & $data
    WEnd
    Return $data
EndFunc   ;==>_AddZero

Share this post


Link to post
Share on other sites
trancexx

Not debating usefullness here...

Cool.

Maybe

StringFormat("%02d", $data)

♡♡♡

.

eMyvnE

Share this post


Link to post
Share on other sites
wraithdu

One-upped again... dammit :blink:

Share this post


Link to post
Share on other sites
Malkey

Good luck in understanding the resultant StringFormat control of "%05.2f" for 2 decimal places, and "%02.0f" for no decimal places. And the role that "($iDecPlaces > 0)" plays. It is simple if you know.

$i = 3665.65 ; 1 hour, 1 minute, 5seconds, 650 milliseconds

MsgBox(0, "", _SecondsToTime($i, 2))


Func _SecondsToTime($secs, $iDecPlaces = 0)
    If $iDecPlaces < 0 Then $iDecPlaces = 0
    Return StringFormat("%02d:%02d:%0" & (2 + ($iDecPlaces > 0) + $iDecPlaces) & "." & $iDecPlaces & "f", _ ; ($iDecPlaces > 0) allows for decimal point.
            Int($secs / 3600), _ ; Hours
            Int(Mod($secs, 3600) / 60), _ ; Minutes
            $secs - (Int($secs / 3600) * 3600 + Int(Mod($secs, 3600) / 60) * 60)) ; Seconds
EndFunc ;==>_SecondsToTime
Edited by Malkey

Share this post


Link to post
Share on other sites
gazai

Excellent script! I was about searching for such function a few days ago.

One suggest: what about adding a reverse function to it (time-to-seconds) to make it perfect?

Frankly, I tried and failed. :blink:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×