record timestamps and calculate passed time Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
karkas
Posts: 16
Joined: 27 Dec 2017, 09:16

record timestamps and calculate passed time

12 Jun 2021, 19:21

Please help me figure out what I am doing wrong? I want to record the timestamp when noise: sub is triggered, then again when it is acknowledged and finally reveal how long it took. Does storing A_Now into a var, always keep var updated with the current time?

Code: Select all

Noise:
tStart := A_Now
msgbox Alert is active %tStart%

tEnd := A_Now
,tElapsed := tEnd 

MsgBox you clicked ok at %tEnd%
tElapsed += -tStart, seconds

msgbox, %tElapsed% seconds passed between alert and acknowledgement 
reactionlog = `nAlert at`t%tStart%`nacknowledged at`t%tend%`n Total of`t%tElapsed%

FileAppend, %reactionlog% , ReactionTime.log
return
I've tried similarly with FormatTime and cant seem to get that to work correctly either.

Thanks in advance
Last edited by gregster on 12 Jun 2021, 19:27, edited 1 time in total.
Reason: Added provisional topic title. Edit to change.
User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: record timestamps and calculate passed time  Topic is solved

12 Jun 2021, 19:30

One way:

Code: Select all

start := A_Now
Sleep, 1500
end := A_Now
end -= start, Seconds
MsgBox, 64, Seconds elapsed, %end%
With your script:

Code: Select all

Noise:
tStart := A_Now
MsgBox, Alert is active at %tStart%.
tEnd := A_Now
tElapsed := tEnd
tElapsed -= tStart, seconds
MsgBox, %tElapsed% seconds passed between alert and acknowledgement.
Return
Another way:

Code: Select all

Noise:
tStart := A_TickCount
MsgBox, 64, Active, Alert is active.
tEnd := A_TickCount
MsgBox, 64, Seconds elapsed, % seconds := Round((tEnd - tStart) / 1000, 1)
Return
Your question about A_Now is readily answered by doing a short test. It is the instantaneous timestamp and will not be updated if you save it to a variable.

A_TickCount gets you milliseconds. A_Now gets you a timestamp to the unit of one second.

Code: Select all

now := A_Now
then := now
Sleep, 2000
MsgBox, %now%`n%then%
The key to your script: when you use -=, you are subtracting one timestamp from another. When you use +=, you are adding or subtracting some units to or from a timestamp.

A null "ending" defaults to now.

Code: Select all

Noise:
tStart := A_Now, tElapsed := ""
MsgBox, Alert is active at %tStart%.
tElapsed -= tStart, seconds
MsgBox, %tElapsed% seconds passed between alert and acknowledgement.
Return
karkas
Posts: 16
Joined: 27 Dec 2017, 09:16

Re: record timestamps and calculate passed time

14 Jun 2021, 08:54

Thanks so much for the quick response @mikeyww! It obviously works now in 3 seconds when I get one of you masters to take a peek. Apologies for my delayed reply. I have absolutely no idea how I never got that to work, because I literally spent hours reading the documentation and trying all the different ways with formattime, EnvSub vs -=, trying to subtract start from end, EnvAdd, += trying to add a negative start time to the end time (As I read was required in several places in forums/docs). It just always came back with a null value. I was ripping my hair out lol and trying not to ask you guys, because I need to figure these things out on my own to improve my limited skill.
mikeyww wrote:
12 Jun 2021, 19:30
With your script:

Code: Select all

Noise:
tStart := A_Now
MsgBox, Alert is active at %tStart%.
tEnd := A_Now
tElapsed := tEnd
tElapsed -= tStart, seconds
MsgBox, %tElapsed% seconds passed between alert and acknowledgement.
Return
Your question about A_Now is readily answered by doing a short test. It is the instantaneous timestamp and will not be updated if you save it to a variable.
I actually did try that. When I started getting my first nulls, I realized I probably needed to assign a variable to it, and set up a test similar to what you showed, but I was probably still mistakenly trying to "add" a negative start date/time.

So it looks like creating the negative date is what did me in. I just needed to change += -start to -= start. Do I understand that right?

I have one more question if you don't mind.

Code: Select all

tEnd := A_Now
tElapsed := tEnd

Code: Select all

tEnd := A_Now
[color=#FF0000],[/color]tElapsed := tEnd
My understanding of AHK is that it takes about 10ms to execute each line. I was afraid of a slight time difference stored between the tEnd & tElapsed variable. From what I read, placing the comma in front of the 2nd line, should cause them to be executed together rather than sequentially. Is that correct?

If so, I see now, that it was not only unnecessary, since tElapsed was assigned to tEnd anyway, so it doesn't matter even if there was a time difference between when both lines were executed. If fact, would that even store anything in the second variable if they were executed at the same time?
User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: record timestamps and calculate passed time

14 Jun 2021, 09:03

You are correct on both accounts.

The second variable stores whatever was in the first variable. If the first variable was null at the time, then that is what you would get. It is a hypothetical question because commands execute in sequence, not at the same time (standard AHK 1.x is single-threaded).

Re the timestamps: yes, the way that -= and += are set up to work is not intuitive (at least initially!)-- may require reading the documentation to understand the difference (now accomplished!). Nonetheless, the differences are useful in practical terms, to accomplish the most common tasks needed with timestamps and time units, so I appreciate how AHK is designed this way.
karkas
Posts: 16
Joined: 27 Dec 2017, 09:16

Re: record timestamps and calculate passed time

14 Jun 2021, 09:11

Thanks Mikey! You guys are incredible and the key to this thriving community. I love AHK and I'm dying to apply it in so many ways to help me in my career. AHK is my first real attempt to learn to code and I'm noticing this old dog doesn't learn nearly as fast as I did 25 years ago lol.


P.S. Is there no discord server chat anymore? I tried the link at the top of the forums and it is expired and I'd love to keep an eye on coding chat. I'm starting to follow this forum too to help myself learn.
User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: record timestamps and calculate passed time

14 Jun 2021, 09:14

Thanks!

As far as I know, the Discord channel is still running, but I am not one of the regulars there.

For help: see "Contact us" at the bottom of this page.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Rohwedder, ruespe and 356 guests