Page 1 of 1

How to get the current millisecond level unix time?

Posted: 17 May 2019, 12:34
by afe
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.

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

Posted: 17 May 2019, 15:58
by TheDewd

Code: Select all

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

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

Posted: 17 May 2019, 16:11
by just me

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

Posted: 18 May 2019, 06:42
by afe
I mean, get the current unix timestamp, and the unit is milliseconds instead of seconds.

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

Posted: 18 May 2019, 07:02
by teadrinker

Code: Select all

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

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

Posted: 18 May 2019, 09:39
by afe
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.

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

Posted: 18 May 2019, 09:50
by afe
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?

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

Posted: 18 May 2019, 12:40
by teadrinker
afe wrote: By the way, time -= 1970, s Is this syntax legal?
Why not?

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

Posted: 25 Jul 2019, 09:36
by afe
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

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

Posted: 25 Jul 2019, 10:40
by jeeswg

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 ""
}

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

Posted: 25 Jul 2019, 11:12
by iPhilip
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. :)

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

Posted: 30 Jan 2023, 10:21
by erbanku
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)

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

Posted: 30 Jan 2023, 19:53
by Chunjee
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