Returns time difference as seconds. If seconds is positive, the System clock is slow.. or if it is negative the System clock is ahead.
Call the function with parameter as True to apply change, in which case the script needs to be run elevated.
Otherwise, simply call the function from an un-elevated script only to know the time difference.
Usage: SetSystemTime(True) ; requires elevation
Code: Select all
SetSystemTime(Update:=0) { ; v0.50 by SKAN on D36F/D36F @ tiny.cc/setsystemtime
Local ST, T2:=0, N:=VarSetCapacity(ST,16,0), S1900:=9435484800
UrlDownloadToFile, http://time.nist.gov:37/, % timeFile := A_Temp . "\rfc-868.txt"
If (ErrorLevel)
Return
T1 := DllCall("Ws2_32\ntohl", "UInt",FileOpen(timeFile, "r").ReadUInt(), "UInt") + S1900
DllCall("FileTimeToSystemTime", "Int64P",T1*=10000000, "Ptr",&ST)
DllCall("GetSystemTimeAsFileTime", "Int64P",T2)
N:=Update ? DllCall("SetSystemTime", "Ptr",&ST) : 0
Return Round((T1-T2)/10000000, 0)
}
Notes:
- Time protocol server returns an unsigned 32-bit integer in 'network byte order'
- Ws2_32\ntohl is used to convert the integer to little-endian
- This 32-bit integer is number of seconds elapsed since the year 1900
- Time elapsed between the years 1601 & 1900 is 9435484800 seconds.
- FILETIME = (number of seconds elapsed since the year 1601)*10000000