Jump to content

Trouble getting start and end times for a task and the difference between the two. - (Moved)


Recommended Posts

I have been searching Help and the forums but can't find exactly what I am looking for. I'm using AutoIT to execute a task and I want to capture the start time of the task, the end time, and the amount of time it took (down to a hundredth of a second). I want the results in the mm:ss:ms format.

I tried using TimerInit and TimerDiff which gives me the amount of time in milliseconds the task took, and I found a way to convert it to the format I want but it is a bit bulky. 

$time1 = TimerInit()
Run ("Notepad.exe")
sleep(1200)
WinWaitActive("Untitled - Notepad")
$timet = TimerDiff($time1)
$time2 = $time1 + $timet
$hour = Floor($time1 / 3600000)
$remanH = Mod($time1, 3600000)
$min = Floor($remanH / 60000)
$remanM = Mod($remanH, 60000)
$sec = Floor($remanM / 1000)
$remanS = Mod($remanM, 1000)
$msec = Floor($remanS)
$Task1a = $min & ":" & $sec & ":" & $msec
$hour = Floor($time2 / 3600000)
$remanH = Mod($time2, 3600000)
$min = Floor($remanH / 60000)
$remanM = Mod($remanH, 60000)
$sec = Floor($remanM / 1000)
$remanS = Mod($remanM, 1000)
$msec = Floor($remanS)
$Task1b = $min & ":" & $sec & ":" & $msec
$hour = Floor($timet / 3600000)
$remanH = Mod($timet, 3600000)
$min = Floor($remanH / 60000)
$remanM = Mod($remanH, 60000)
$sec = Floor($remanM / 1000)
$remanS = Mod($remanM, 1000)
$msec = Floor($remanS)
$Task1c = $min & ":" & $sec & ":" & $msec

I also tried capturing the start and end times using code below when the task begins and ends but haven't figured out how to do a calculation between the start and end times to get the actual time the task took:

$task1a = (@Hour & ':' & @Min & ':' & @Sec & ':' & @MSec)

Either way I would like to drop the thousandth millisecond and just have the 2 digits but not sure how to do that.

Can anyone offer a suggestion on a better to collect my times?

Link to comment
Share on other sites

Link to comment
Share on other sites

I already included the pertinent parts of my efforts in my original post that I have tried as I test this out. I will include all of what I have tried so far but there isn't much more than I already shared.

One attempt:

$task1a = (@Hour & ':' & @Min & ':' & @Sec & ':' & @MSec)
Run ("Notepad.exe")
WinWaitActive("Untitled - Notepad")
sleep(1000)
$task1b = (@Hour & ':' & @Min & ':' & @Sec & ':' & @MSec)
$task1c = $task1a - $task1b
MsgBox(0, "Time start", "Start " & $task1a & " Stop " & $task1b & " time " & $task1c)
WinClose("Untitled - Notepad")

and then this:

$time1 = TimerInit() ; Begin the timer and store the handle in a variable.
Run ("Notepad.exe")
sleep(1200)
WinWaitActive("Untitled - Notepad")
$timet = TimerDiff($time1) ; Find the difference in time from the previous call of TimerInit. The variable we stored the TimerInit handlem is passed as the "handle" to TimerDiff.
$time2 = $time1 + $timet
$hour = Floor($time1 / 3600000)
$remanH = Mod($time1, 3600000)
$min = Floor($remanH / 60000)
$remanM = Mod($remanH, 60000)
$sec = Floor($remanM / 1000)
$remanS = Mod($remanM, 1000)
$msec = Floor($remanS)
$Task1a = $min & ":" & $sec & ":" & $msec
$hour = Floor($time2 / 3600000)
$remanH = Mod($time2, 3600000)
$min = Floor($remanH / 60000)
$remanM = Mod($remanH, 60000)
$sec = Floor($remanM / 1000)
$remanS = Mod($remanM, 1000)
$msec = Floor($remanS)
$Task1b = $min & ":" & $sec & ":" & $msec
$hour = Floor($timet / 3600000)
$remanH = Mod($timet, 3600000)
$min = Floor($remanH / 60000)
$remanM = Mod($remanH, 60000)
$sec = Floor($remanM / 1000)
$remanS = Mod($remanM, 1000)
$msec = Floor($remanS)
$Task1c = $min & ":" & $sec & ":" & $msec
MsgBox($MB_SYSTEMMODAL, "Time Difference", "start " & $Task1a & " end " & $Task1b & " diff " & $Task1c)
WinClose("Untitled - Notepad")

 

Link to comment
Share on other sites

here :

Local $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.
;do something here
; Local $iTime = TimerDiff($hTimer) ; Find the difference in time from the previous call of TimerInit.
Local $iTime = 4657891 ; for testing purpose here
ConsoleWrite("Time in ms " & $iTime & @CRLF)
$iTime = Int($iTime/1000) ; make it in secs
Local $hour = Floor($iTime / 3600)
ConsoleWrite("Number of hours = " & $hour & @CRLF)
$iTime -= $hour * 3600
Local $min = Floor($iTime / 60)
ConsoleWrite("Number of mins = " & $min & @CRLF)
Local $sec = Mod($iTime, 60)
ConsoleWrite("Number of secs = " & $sec & @CRLF)

 

Link to comment
Share on other sites

  • Developers

Moved to the appropriate AutoIt General Help and Support forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

Nine, thanks for the response. Your code gives me the length of time my task takes but I also want to capture the start and end times. You said using the value of TimerInit as my start time isn't a good idea but I'm not sure how else to get it. Does the below look like a bad idea?

Local $hTimer = TimerInit() ; Begin the timer and store the handle in a variable.
ConsoleWrite("Start Time in ms " & $hTimer & @CRLF) ; Start time
sleep(900)
Local $iTime = TimerDiff($hTimer) ; Find the difference in time from the previous call of TimerInit.
$eTime = $hTimer + $iTime ; Add TimerInit TimerDiff results to generate End time.
;Local $iTime = 4657891 ; for testing purpose here
ConsoleWrite("Time in ms " & $iTime & @CRLF)
ConsoleWrite("End Time in ms " & $eTime & @CRLF)

 

Link to comment
Share on other sites

31 minutes ago, PeterlFF said:

but I also want to capture the start and end times

In what terms ?  hour:min:sec:msec ?  If it is in ms what would the anchor ?

Link to comment
Share on other sites

Local $sStart = @MIN & ":" & @SEC & ":" & Int(Number(@MSEC)/100)
ConsoleWrite($sStart & @CRLF)

Notice that all the date and time macros return a string.  If you require to use them in arithmetical expression, it is recommended to convert them in number beforehand. 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...