SetSystemTime() : Updates system time with time returned from a Time Server

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

SetSystemTime() : Updates system time with time returned from a Time Server

15 Jun 2020, 10:41

SetSystemTime(Update)
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
My Scripts and Functions: V1  V2
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: SetSystemTime() : Updates system time with time returned from a Time Server

15 Jun 2020, 12:17

Do you work with any other languages and you know a handful of functions? Or do you just find some functions from a list or something and decide to create a function or script based off of it? lol

Also, couldn't you use a UrlDownloadToVar-like scriptlet instead of downloading a file to the disk itself?

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: SetSystemTime() : Updates system time with time returned from a Time Server

15 Jun 2020, 12:51

Delta Pythagorean wrote: Do you work with any other languages and you know a handful of functions? Or do you just find some functions from a list or something and decide to create a function or script based off of it? lol
Are you really asking a question here?.. or just trying to be a troll?.
All the recent functions I have been posting are being written for my un-elevated startup script except this function which is called from my elevated startup script.
Delta Pythagorean wrote:Also, couldn't you use a UrlDownloadToVar-like scriptlet instead of downloading a file to the disk itself?
What will be the advantage of it?.
If you don't delete rfc-868.txt, you could compare the file creation timestamp against the timestamp inside to determine the bias anytime.
I need the bias to apply it to UserLastLogon() which doesn't reflect any System time change.
My Scripts and Functions: V1  V2
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: SetSystemTime() : Updates system time with time returned from a Time Server

15 Jun 2020, 15:20

garry wrote:another script from user tmplinshi to set time
Thanks for the link @garry.
I wonder why @tmplinshi uses SetLocalTime()
The example in docs shows how to convert LocalTime-Stamp to SystemTime.
My Scripts and Functions: V1  V2
garry
Posts: 3764
Joined: 22 Dec 2013, 12:50

Re: SetSystemTime() : Updates system time with time returned from a Time Server

15 Jun 2020, 15:41

@SKAN . thank you for the link > https://www.autohotkey.com/docs/commands/DllCall.htm#ExSystemTime
Change the system's clock to the specified date and time
maybe I don't understand ... not want set time , want synchronize time ...
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: SetSystemTime() : Updates system time with time returned from a Time Server

15 Jun 2020, 15:42

@SKAN, please keep posting these useful scripts. :thumbup: Ignore the haters, although I can't imagine there are many of them.
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: SetSystemTime() : Updates system time with time returned from a Time Server

15 Jun 2020, 16:44

garry wrote:maybe I don't understand ... not want set time , want synchronize time ...
Of course, we would want synchronize time.
What I meant was:
You form a local date-time-stamp by downloading it from some server and then pass it to the function available in doc.
Anyways! I wouldn't recommend calling SetLocalTime(). You should call SetSystemTime() with a proper UTC timestamp.
Here is the modified example for you @garry

Code: Select all

#NoEnv
#SingleInstance, Force

timeFile := A_Temp . "\rfc-867.txt"
UrlDownloadToFile, http://time.nist.gov:13/, %timeFile%
If (ErrorLevel)
  {
   MsgBox Server busy. Try 4 seconds later!
   ExitApp
  } 

FileRead, Time, %timeFile%
MsgBox  % Time  ; would look like this=>  59015 20-06-15 21:05:14 50 0 0 186.3 UTC(NIST) * 

Time := "20" . SubStr(Time,8,17)
MsgBox  % Time  ; would look like this=> 2020-06-15 21:10:38

StringReplace, Time, Time, -,, All
StringReplace, Time, Time, :,, All
StringReplace, Time, Time, %A_Space%,, All

MsgBox % Time   ; would look like this=> 20200615211038

; I am copying rest of the code from documentation example
; Since time is already UTC, I have removed "to UTC" conversion part

YYYYMMDDHHMISS := Time ; reassigning variable so I don`t have to edit the doc example

VarSetCapacity(SystemTime, 16, 0)  ; This struct consists of 8 UShorts (i.e. 8*2=16).

Int := SubStr(YYYYMMDDHHMISS, 1, 4)  ; YYYY (year)
NumPut(Int, SystemTime, 0, "UShort")
Int := SubStr(YYYYMMDDHHMISS, 5, 2)  ; MM (month of year, 1-12)
NumPut(Int, SystemTime, 2, "UShort")
Int := SubStr(YYYYMMDDHHMISS, 7, 2)  ; DD (day of month)
NumPut(Int, SystemTime, 6, "UShort")
Int := SubStr(YYYYMMDDHHMISS, 9, 2)  ; HH (hour in 24-hour time)
NumPut(Int, SystemTime, 8, "UShort")
Int := SubStr(YYYYMMDDHHMISS, 11, 2) ; MI (minute)
NumPut(Int, SystemTime, 10, "UShort")
Int := SubStr(YYYYMMDDHHMISS, 13, 2) ; SS (second)
NumPut(Int, SystemTime, 12, "UShort")

DllCall("SetSystemTime", "Ptr", &SystemTime)  
My Scripts and Functions: V1  V2
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: SetSystemTime() : Updates system time with time returned from a Time Server

15 Jun 2020, 16:45

boiler wrote:@SKAN, please keep posting these useful scripts.
Thank you. :thumbup: :)
My Scripts and Functions: V1  V2
User avatar
Delta Pythagorean
Posts: 627
Joined: 13 Feb 2017, 13:44
Location: Somewhere in the US
Contact:

Re: SetSystemTime() : Updates system time with time returned from a Time Server

16 Jun 2020, 04:20

SKAN wrote:
15 Jun 2020, 12:51
Are you really asking a question here?.. or just trying to be a troll?.
I try my best not to be a troll. Sorry if that seemed to be in a rude way, I'm just extremely impressed in your work.
I may not have the best social skills, but I at least try to be nice :)

[AHK]......: v2.0.12 | 64-bit
[OS].......: Windows 11 | 23H2 (OS Build: 22621.3296)
[GITHUB]...: github.com/DelPyth
[PAYPAL]...: paypal.me/DelPyth
[DISCORD]..: tophatcat

User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: SetSystemTime() : Updates system time with time returned from a Time Server

16 Jun 2020, 06:42

SKAN wrote:
15 Jun 2020, 12:51
Are you really asking a question here?.. or just trying to be a troll?.
Delta Pythagorean wrote:
16 Jun 2020, 04:20
I try my best not to be a troll. Sorry if that seemed to be in a rude way, I'm just extremely impressed in your work.
I may not have the best social skills, but I at least try to be nice :)
Thanks for being a good sport :thumbup:
To answer your questions.
Delta Pythagorean wrote:
15 Jun 2020, 12:17
Do you work with any other languages and you know a handful of functions? Or do you just find some functions from a list or something and decide to create a function or script based off of it? lol
I love and use "Pelles C", since its Windows only. I started creating a massive static lib with many API calls simplified.. I'm not sure if I will ever complete it though.
Almost all AV software kills if I compile an exe aound 30-50 KiB...
.. and yes!. I occasionally list exported functions from all SYSTEM32 dlls and search for function names.

:)
My Scripts and Functions: V1  V2
User avatar
jNizM
Posts: 3183
Joined: 30 Sep 2013, 01:33
Contact:

Re: SetSystemTime() : Updates system time with time returned from a Time Server

16 Jun 2020, 07:05

SKAN wrote:
16 Jun 2020, 06:42
I occasionally list exported functions from all SYSTEM32 dlls and search for function names.
Then you're gonna love this ;)
https://www.autohotkey.com/boards/viewtopic.php?t=34262
[AHK] v2.0.5 | [WIN] 11 Pro (Version 22H2) | [GitHub] Profile
User avatar
SKAN
Posts: 1551
Joined: 29 Sep 2013, 16:58

Re: SetSystemTime() : Updates system time with time returned from a Time Server

16 Jun 2020, 07:11

jNizM wrote:Then you're gonna love this ;)
https://www.autohotkey.com/boards/viewtopic.php?t=34262
Of course! Well written app! :D :thumbup:
My Scripts and Functions: V1  V2

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 244 guests