How to get the current millisecond level unix time? Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
afe
Posts: 615
Joined: 06 Dec 2018, 04:36

How to get the current millisecond level unix time?

Post by afe » 17 May 2019, 12:34

Hello,

How to get the current millisecond level unix time?

Code: Select all

Now := A_Now
EnvSub, Now, 19700101000000, Seconds

Now := Now * 1000 + A_MSec
This algorithm seems to be inaccurate.
Last edited by afe on 18 May 2019, 09:35, edited 2 times in total.

User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: How to get the current millisecond level unix time?  Topic is solved

Post by TheDewd » 17 May 2019, 15:58

Code: Select all

Time := A_NowUTC
EnvSub, Time, 19700101000000, Seconds
MsgBox, %Time%


afe
Posts: 615
Joined: 06 Dec 2018, 04:36

Re: How to get the current millisecond level unix time?

Post by afe » 18 May 2019, 06:42

I mean, get the current unix timestamp, and the unit is milliseconds instead of seconds.

teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to get the current millisecond level unix time?

Post by teadrinker » 18 May 2019, 07:02

Code: Select all

time := A_NowUTC
time -= 1970, s
MsgBox, % time . "000"

afe
Posts: 615
Joined: 06 Dec 2018, 04:36

Re: How to get the current millisecond level unix time?

Post by afe » 18 May 2019, 09:39

TheDewd wrote:
17 May 2019, 15:58

Code: Select all

Time := A_NowUTC
EnvSub, Time, 19700101000000, Seconds
MsgBox, %Time%
This algorithm is very good. But not milliseconds.
My code is wrong.

Thanks.

afe
Posts: 615
Joined: 06 Dec 2018, 04:36

Re: How to get the current millisecond level unix time?

Post by afe » 18 May 2019, 09:50

teadrinker wrote:
18 May 2019, 07:02

Code: Select all

time := A_NowUTC
time -= 1970, s
MsgBox, % time . "000"

Our ideas are very similar. But I want to finally attach A_MSec instead of 000. But no matter what, it seems that both have errors.

I should have asked the following code to be less precise.

Code: Select all

Now := A_NowUTC
EnvSub, Now, 19700101000000, Seconds

Now := Now * 1000 + A_MSec

By the way, time -= 1970, s Is this syntax legal?

teadrinker
Posts: 4325
Joined: 29 Mar 2015, 09:41
Contact:

Re: How to get the current millisecond level unix time?

Post by teadrinker » 18 May 2019, 12:40

afe wrote: By the way, time -= 1970, s Is this syntax legal?
Why not?

afe
Posts: 615
Joined: 06 Dec 2018, 04:36

Re: How to get the current millisecond level unix time?

Post by afe » 25 Jul 2019, 09:36

You are right.

I improved the code. Is this error reduced?

Code: Select all

Start := A_TickCount
m := A_MSec
r := A_NowUTC
r -= 19700101000000, s
Elapsed := A_TickCount - Start

msgbox , %  r * 1000 + m + Elapsed
Elapsed = 0, not needed at all.

Code: Select all

r := A_NowUTC
r -= 19700101000000, s
msgbox , %  r * 1000 + A_MSec

User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to get the current millisecond level unix time?

Post by jeeswg » 25 Jul 2019, 10:40

Code: Select all

q:: ;test date now Unix
;note: A_MSec is used for testing, but JEE_DateNowUnix("ms") should be more reliable
MsgBox, % JEE_DateNowUnix("s") " " A_MSec "`r`n" JEE_DateNowUnix("ms")
return

JEE_DateNowUnix(vFormat:="")
{
	local
	;equivalent to code below
	;if (vFormat = "") || (vFormat = "s")
	;	return DateDiff(A_NowUTC, 1970, "Seconds")

	vIntervals := 0
	DllCall("kernel32\GetSystemTimeAsFileTime", "Int64*",vIntervals)

	;note: 116444736000000000 is 1 Jan 1970 UTC as a FILETIME
	if (vFormat = "") || (vFormat = "s")
		return (vIntervals - 116444736000000000) // 10000000
	else if (vFormat = "ms")
		return (vIntervals - 116444736000000000) // 10000
	return ""
}
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA

iPhilip
Posts: 814
Joined: 02 Oct 2013, 12:21

Re: How to get the current millisecond level unix time?

Post by iPhilip » 25 Jul 2019, 11:12

Here is an alternative:

Code: Select all

r := A_NowUTC
r -= 19700101000000, s
msgbox , %  (r * 1000 + A_MSec) "`n" GetSystemTimeAsUnixTime()

; https://stackoverflow.com/a/46024468

GetSystemTimeAsUnixTime() {
   static UNIX_TIME_START := 0x019DB1DED53E8000  ; January 1, 1970 (start of Unix epoch) in "ticks"
        , TICKS_PER_SECOND := 10000000  ; A tick is 100ns
   
   DllCall("GetSystemTimeAsFileTime", "Int64*", UTC_Ticks)  ; Returns ticks in UTC
   Return (UTC_Ticks - UNIX_TIME_START) / TICKS_PER_SECOND
}
Edit: Thank you @jeeswg for that trick. :)
Windows 10 Pro (64 bit) - AutoHotkey v2.0+ (Unicode 64-bit)

erbanku
Posts: 7
Joined: 07 Oct 2021, 23:04

Re: How to get the current millisecond level unix time?

Post by erbanku » 30 Jan 2023, 10:21

TheDewd wrote:
17 May 2019, 15:58

Code: Select all

Time := A_NowUTC
EnvSub, Time, 19700101000000, Seconds
MsgBox, %Time%
Thanks, converted to AutoHotkey V2 version (as I need to modify)

Code: Select all

Time := A_NowUTC
Time := DateDiff(Time, 19700101000000, "Seconds")
MsgBox(Time)

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

Re: How to get the current millisecond level unix time?

Post by Chunjee » 30 Jan 2023, 19:53

https://biga-ahk.github.io/biga.ahk/#/?id=now
Only accurate to the current second. Please feel free to modify for millisecond accuracy.

Code: Select all

A := new biga() ; requires https://github.com/biga-ahk/biga.ahk

A.now()
; => 1636159584000

Post Reply

Return to “Ask for Help (v1)”