How to calculate time differences in milliseconds

Ask gaming related questions (AHK v1.1 and older)
GoneFishing
Posts: 126
Joined: 20 May 2022, 16:17

How to calculate time differences in milliseconds

Post by GoneFishing » 05 Dec 2022, 14:44

I need to calculate the difference in seconds between two time stamps from a log file. I can extract the timestamps required but I am not sure how I go about calculating the difference in seconds.
I also need to send a message (I have the code for that once I get there from here) if the gap between the two times exceeds 20 seconds or more.

My code so far is as follows. Obviously the TimeDiff variable won't calculate anything at the moment. How do I get the output of DTS1 and DTS2 in seconds (whole seconds) so I can perform the calculation correctly ? I've included what the output of DTS1 & DTS2 is

Code: Select all

FullName := A_ScriptDir "\Client.txt"
FileRead, AllLines, %FullName%
StringSplit, Line, AllLines, `n ; Line0 = nr of lines, Line1 is first line, Line2 is 2nd line, etc.
PosDTS1Line := Line0 -5
PosDTS2Line := Line0 -1
DTS1 := SubStr(Line%PosDTS1Line%, 12,8)  ; Date and Time stamp
DTS2 := Substr(Line%PosDTS2Line%, 12,8)
TimeDiff := (DTS2-DTS1)

DTS1 12:56:45 (hh:mm:ss)
DTS2 12:56:45 (hh:mm:ss)

Found a solution that works as required

Code: Select all

FullName := A_ScriptDir "\Client.txt"
FileRead, AllLines, %FullName%
StringSplit, Line, AllLines, `n ; Line0 = nr of lines, Line1 is first line, Line2 is 2nd line, etc.
PosDTS1Line := Line0 -5
PosDTS2Line := Line0 -1
DTS1 := SubStr(Line%PosDTS1Line%, 12,8)  ; Date and Time stamp
DTS2 := Substr(Line%PosDTS2Line%, 12,8)
;MsgBox % DTS1
T1 := StrSplit(DTS1, ":")
T2 := StrSplit(DTS2, ":")
TS1 := T1.1 * 3600 + T1.2 * 60 + T1.3 
TS2 := T2.1 * 3600 + T2.2 * 60 + T2.3
TimeDiff := (TS2-TS1)
MsgBox % "Time 1 is " TS1 " seconds" "`nTime 2 is " TS2 " seconds" "`nThe Time difference is " TimeDiff " seconds"
Last edited by GoneFishing on 05 Dec 2022, 15:06, edited 1 time in total.

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: How to calculate time differences in milliseconds

Post by Chunjee » 05 Dec 2022, 14:56

GoneFishing wrote:
05 Dec 2022, 14:44

Code: Select all

StringSplit, Line, AllLines, `n ; Line0 = nr of lines, Line1 is first line, Line2 is 2nd line, etc.

StringSplit is depreciated.
Deprecated: This command is not recommended for use in new scripts. Use the StrSplit function instead.
https://www.autohotkey.com/docs/commands/StrSplit.htm

User avatar
Chunjee
Posts: 1400
Joined: 18 Apr 2014, 19:05
Contact:

Re: How to calculate time differences in milliseconds

Post by Chunjee » 05 Dec 2022, 14:58

GoneFishing wrote:
05 Dec 2022, 14:44
DTS1 12:56:45 (hh:mm:ss)
DTS2 12:56:45 (hh:mm:ss)
12:56:45 minus 12:56:45 is zero

User avatar
mikeyww
Posts: 26593
Joined: 09 Sep 2014, 18:38

Re: How to calculate time differences in milliseconds

Post by mikeyww » 05 Dec 2022, 15:42

:arrow: EnvSub
TimeUnits: If present, this parameter directs the command to subtract Value from Var as though both of them are date-time stamps in the YYYYMMDDHH24MISS format. TimeUnits can be either Seconds, Minutes, Hours, or Days (or just the first letter of each of these).

Post Reply

Return to “Gaming Help (v1)”