; #INDEX# ======================================================================================================================= ; Title ............: _ObjStopwatch UDF ; Description ......: Create a stopwatch for timing activities ; Date .............: 08/29/2022 ; AutoIt Version ...: 3.3.16.0 ; ; Remarks ..........: - This UDF relies on AutoItObject_Internal.au3 ; ; Author(s) ........: kurtykurtyboy ; ; Credits ..........: genius257 (AutoItObject_Internal UDF) ; ; Functions ; _ObjStopwatch ...: Create a new stopwatch object ; ; Properties ; ; Methods ; - start .........: start (or restart) the timer and reset the accumulated timer value ; - stop ..........: stop/pause the timer ; - lap ...........: start a new lap timer and return the previous lap time ; - reset .........: reset the timer ; - elapsed .......: get the total stopwatch time since last start ; ; Functions [INTERNAL USE ONLY] ; _ObjStopwatch_start ; _ObjStopwatch_stop ; _ObjStopwatch_lap ; _ObjStopwatch_reset ; _ObjStopwatch_elapsed ; _ObjStopwatch_formatTime ; =============================================================================================================================== #include-once #include "AutoItObject_Internal.au3" ; #FUNCTION# ========================================================================================================= ; Name...........: _ObjStopwatch ; Description ...: Create a new stopwatch object ; Syntax.........: _ObjStopwatch ( ) ; Parameters ....: none ; Return values .: the stopwatch object ; Author ........: kurtykurtyboy ;===================================================================================================================== Func _ObjStopwatch() Local $oIDispatch = IDispatch() $oIDispatch.__defineGetter("start", _ObjStopwatch_start) $oIDispatch.__defineGetter("stop", _ObjStopwatch_stop) $oIDispatch.__defineGetter("lap", _ObjStopwatch_lap) $oIDispatch.__defineGetter("reset", _ObjStopwatch_reset) $oIDispatch.__defineGetter("elapsed", _ObjStopwatch_elapsed) ;internal use only $oIDispatch.timer = -1 $oIDispatch.time = 0 $oIDispatch.laptimer = -1 $oIDispatch.laptime = 0 Return $oIDispatch EndFunc ;==>_ObjStopwatch ; #FUNCTION# ========================================================================================================= ; Name...........: _ObjStopwatch_start ; Description ...: Start (or restart) the timer and reset the accumulated timer value ; Syntax.........: _ObjStopwatch_start ( ) ; Parameters ....: none ; Return values .: none ; Author ........: kurtykurtyboy ;===================================================================================================================== Func _ObjStopwatch_start($AccessorObject) ;start the timer If $AccessorObject.parent.timer <> -2 Then $AccessorObject.parent.time = 0 EndIf $AccessorObject.parent.timer = TimerInit() ;reset the lap timer If $AccessorObject.parent.laptimer <> -2 Then $AccessorObject.parent.laptime = 0 EndIf $AccessorObject.parent.laptimer = $AccessorObject.parent.timer EndFunc ;==>_ObjStopwatch_start ; #FUNCTION# ========================================================================================================= ; Name...........: _ObjStopwatch_stop ; Description ...: stop/pause the timer ; Syntax.........: _ObjStopwatch_stop ( ) ; Parameters ....: none ; Return values .: none ; Author ........: kurtykurtyboy ;===================================================================================================================== Func _ObjStopwatch_stop($AccessorObject) ;stop and backup the timer If $AccessorObject.parent.timer >= 0 Then $AccessorObject.parent.time = $AccessorObject.parent.time + TimerDiff($AccessorObject.parent.timer) $AccessorObject.parent.laptime = $AccessorObject.parent.laptime + TimerDiff($AccessorObject.parent.laptimer) EndIf ;turn off the timers $AccessorObject.parent.timer = -2 $AccessorObject.parent.laptimer = -2 EndFunc ;==>_ObjStopwatch_stop ; #FUNCTION# ========================================================================================================= ; Name...........: _ObjStopwatch_reset ; Description ...: stop and reset the timer ; Syntax.........: _ObjStopwatch_reset ( ) ; Parameters ....: none ; Return values .: none ; Author ........: kurtykurtyboy ;===================================================================================================================== Func _ObjStopwatch_reset($AccessorObject) ;start the timer $AccessorObject.parent.time = 0 $AccessorObject.parent.timer = -1 ;reset the lap timer $AccessorObject.parent.laptime = 0 $AccessorObject.parent.laptimer = -1 EndFunc ;==>_ObjStopwatch_reset ; #FUNCTION# ========================================================================================================= ; Name...........: _ObjStopwatch_lap ; Description ...: start a new lap timer and return the previous lap time ; Syntax.........: _ObjStopwatch_lap ( [ $iFormat = 0 [, $bNewLap = True ]] ) ; Parameters ....: $iFormat = desired format ; - 0 = ms with decimal (same as TimerDiff()) ; - 1 = hh:mm:ss ; - 2 = hh:mm:ss:00ms ; - 3 = hh:mm:ss.0 ; $bNewLap = Return the current lap time, and reset lap timer ; Return values .: lap time (in selected format) ; Author ........: kurtykurtyboy ;===================================================================================================================== Func _ObjStopwatch_lap($AccessorObject) Local $time, $timeElapsed If $AccessorObject.parent.laptimer < 0 Then $timeElapsed = 0 Else $timeElapsed = TimerDiff($AccessorObject.parent.laptimer) EndIf $time = $AccessorObject.parent.laptime + $timeElapsed If $AccessorObject.arguments.length < 2 Or ($AccessorObject.arguments.length = 2 And $AccessorObject.arguments.values[1]) Then If $AccessorObject.parent.laptimer >= 0 Then $AccessorObject.parent.laptimer = TimerInit() EndIf $AccessorObject.parent.laptime = 0 EndIf If $AccessorObject.arguments.length > 0 Then $time = _ObjStopwatch_formatTime($time, $AccessorObject.arguments.values[0]) EndIf Return $time EndFunc ;==>_ObjStopwatch_lap ; #FUNCTION# ========================================================================================================= ; Name...........: _ObjStopwatch_elapsed ; Description ...: Get the total stopwatch time since last start ; Syntax.........: _ObjStopwatch_elapsed ( [$iFormat = 0] ) ; Parameters ....: $iFormat = desired format ; - 0 = ms with decimal (same as TimerDiff()) ; - 1 = hh:mm:ss ; - 2 = hh:mm:ss:00ms ; - 3 = hh:mm:ss.0 ; Return values .: total run time (in selected format) ; Author ........: kurtykurtyboy ;===================================================================================================================== Func _ObjStopwatch_elapsed($AccessorObject) Local $time, $timeElapsed If $AccessorObject.parent.timer < 0 Then $timeElapsed = 0 Else $timeElapsed = TimerDiff($AccessorObject.parent.timer) EndIf $time = $AccessorObject.parent.time + $timeElapsed If $AccessorObject.arguments.length = 0 Then ;~ Return $time Else $time = _ObjStopwatch_formatTime($time, $AccessorObject.arguments.values[0]) EndIf Return $time EndFunc ;==>_ObjStopwatch_elapsed ; #FUNCTION# ==================================================================================================================== ; Name...........: _ObjStopwatch_formatTime ; Description ...: Convert ms ticks from TimerDiff() to desired format ; Syntax.........: _ObjStopwatch_formatTime ( $iTicks, $iFormat ) ; Parameters ....: $iTicks = ms return from TimerDiff() ; $iFormat = desired format ; - 0 = ms with decimal (same as TimerDiff()) ; - 1 = hh:mm:ss ; - 2 = hh:mm:ss:0ms ; - 3 = hh:mm:ss.0 ; Author ........: kurtykurtyboy ; partially taken from Date.au3 _TicksToTime Marc ; =============================================================================================================================== Func _ObjStopwatch_formatTime($iTicks, $iFormat) Local $iHours, $iMins, $iSecs, $iMs, $sTime $iMs = $iTicks If Number($iTicks) > 0 Then $iTicks = Int($iTicks / 1000) $iHours = Int($iTicks / 3600) $iTicks = Mod($iTicks, 3600) $iMins = Int($iTicks / 60) $iSecs = Mod($iTicks, 60) $iSecs = Mod($iTicks, 60) $iMs = $iMs - ($iHours * 3600000 + $iMins * 60000 + $iSecs * 1000) ; If $iHours = 0 then $iHours = 24 ElseIf Number($iTicks) = 0 Then $iHours = 0 $iTicks = 0 $iMins = 0 $iSecs = 0 $iMs = 0 EndIf Switch $iFormat Case 0 ; ms with decimal $sTime = $iMs Case 1 ; hh:mm:ss $sTime = StringFormat("%02d:%02d:%02d", $iHours, $iMins, $iSecs) Case 2 ; hh:mm:ss:00ms $sTime = StringFormat("%02d:%02d:%02d:%03d", $iHours, $iMins, $iSecs, Int($iMs)) Case 3 ; hh:mm:ss.0 $sTime = StringFormat("%02d:%02d:%04.1f", $iHours, $iMins, $iSecs + $iMs / 1000) EndSwitch Return $sTime EndFunc ;==>_ObjStopwatch_formatTime