Get Current Micro/Nano seconds

Get help with using AutoHotkey and its commands and hotkeys
omareg94
Posts: 70
Joined: 27 Jun 2016, 22:46

Get Current Micro/Nano seconds

15 Jan 2017, 13:36

I know that to get current millisecond I can use the variable A_MSec.
How could I get current microsecond and current nanosecond?
User avatar
Nextron
Posts: 1364
Joined: 01 Oct 2013, 08:23
Location: Netherlands OS: Win7 x64 AHK: Unicode x32

Re: Get Current Micro/Nano seconds

15 Jan 2017, 14:25

Code: Select all

DllCall("GetSystemTimeAsFileTime","Int64*",T)

MsgBox % T
Gets the number of 100-nanosecond intervals since 1600-ish.
User avatar
jeeswg
Posts: 6648
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Get Current Micro/Nano seconds

15 Jan 2017, 14:36

Whew! Nice to know someone else knew.
Wowsers, what do you need this for?

Code: Select all

q:: ;get current time including nanoseconds
vIntervals := 0
DllCall("kernel32\GetSystemTimeAsFileTime", "Int64*",vIntervals)
;1 interval = 100 nanoseconds

;date and nanoseconds
vDate := 1601
EnvAdd, vDate, % vIntervals//10000000, S
vNano := Format("{:07}00", Mod(vIntervals, 10000000))

;date and milliseconds for comparison as a check
vDate2 := A_Now " " A_MSec

;nanoseconds
vNano2 := vIntervals "00"

MsgBox, % "      " vNano2 "`r`n" vDate " " vNano "`r`n" vDate2
return
Last edited by jeeswg on 15 Jun 2019, 20:17, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
jeeswg
Posts: 6648
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Get Current Micro/Nano seconds

15 Jan 2017, 14:53

For people's attention, concatenating A_Now and A_MSec, can give
incorrect results as they are calculated at separate times.

Code: Select all

q:: ;test - date and milliseconds (UTC)
vIntervals := 0
DllCall("kernel32\GetSystemTimeAsFileTime", "Int64*",vIntervals)
vDate := 1601
EnvAdd, vDate, % vIntervals//10000000, S
vMSec := Format("{:03}", Mod(vIntervals//10000, 1000))
MsgBox, % vDate " " vMSec " (reliable)`r`n" A_NowUTC " " A_MSec " (unreliable)"
return

w:: ;test - date and milliseconds (local)
vIntervals1 := vIntervals2 := 0
DllCall("kernel32\GetSystemTimeAsFileTime", "Int64*",vIntervals1)
DllCall("kernel32\FileTimeToLocalFileTime", "Int64*",vIntervals1, "Int64*",vIntervals2)

vDate := 1601
EnvAdd, vDate, % vIntervals2//10000000, S
vMSec := Format("{:03}", Mod(vIntervals2//10000, 1000))
MsgBox, % vDate " " vMSec " (reliable)`r`n" A_Now " " A_MSec " (unreliable)"
return
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], Bral, Google [Bot], MannyKSoSo and 165 guests