Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Sync System Clock with External Time Server


  • Please log in to reply
19 replies to this topic
scriptor
  • Members
  • 668 posts
  • Last active: Feb 15 2016 01:02 AM
  • Joined: 20 Jun 2013

A little while back I had mentioned that my system clock had gone bonkers on me. It just changes to a random time, at any given moment. I had found a few solutions, which included running 3rd party software such as Wim's World Clock or Atomic Clock Sync. These programs checked an online time server at any interval you choose (every minute, for example) and would sync your system clock to that time server's clock. In essence, it keeps your clock accurate at all times.

 

Well sure enough, these programs have ceased to work, and I'm now back to my clock going crazy on me again. I don't know why they stopped working, I've tried everything for days and days to get them going again but no luck. (Uninstall/re-install, etc etc). I also don't know what's wrong with my clock, but it's not likely a battery issue because my computer is still relatively new and everything else is ok. I don't know if this is malware or something but it's the most stubborn problem I've ever encountered on this PC. 

 

My question - does anyone know of a way for AHK to connect to these external time servers and update the system's clock to match? I could write a script with a timer that would check the server every minute or so, and then change the clock to that server's time. But this is a little over my head at the moment, and I'm not even sure it could be done. 

 

Would anyone happen to know?



CesarF
  • Members
  • 13 posts
  • Last active: Aug 31 2016 10:11 PM
  • Joined: 17 Sep 2007

not really an answer,

but I'm afraid those servers would label your IP as an attacker because of your frequency

 

if your computer is AC plugged, battery shouldn't be an issue, please confirm.

if that's so change battery, otherwise report with the hardware vendor



scriptor
  • Members
  • 668 posts
  • Last active: Feb 15 2016 01:02 AM
  • Joined: 20 Jun 2013

thanks, I'm not sure what AC plugged means, but I can say that I've never even looked at the battery. My motherboard is an Asus Z87-A if that helps. 

 

Actually my clock has remained stable the past 3 or 4 hours, which has not been the case with this problem at all. It would never last more than 10 minutes without a change, so fingers crossed here



T_Lube
  • Members
  • 640 posts
  • Last active: Sep 09 2016 02:19 AM
  • Joined: 16 Oct 2014

may I recommend?:

ie := ComObjCreate("InternetExplorer.Application")
ie.Navigate("www.timeanddate.com")
While (ie.busy || or ie.Readystate <> 4)
	Sleep 100
msgbox % ie.Document.getElementbyId("clk_hm").innertext

I am sure that you can set up the timer and refresh and of course the re-direct of the "innertext" as well as set the time and off of that.



jNizM
  • Members
  • 928 posts
  • Last active: Jan 12 2018 09:23 AM
  • Joined: 01 Aug 2012
Stop Time Service
net stop w32time
Change Time Servers (ptbtime1.ptb.de,ptbtime2.ptb.de are official german time servers - you can use ntp.pool.org)
w32tm /config /syncfromflags:manual /manualpeerlist:ptbtime1.ptb.de,ptbtime2.ptb.de
Accept the new Time Servers
w32tm /config /reliable:yes
Sync with Time Servers
w32tm /config /update
Start Time Service
net start w32time
---------------------------
Check the configuration
w32tm /query /configuration

[AHK] 1.1.27.04 x64 Unicode | [WIN] 10 Pro (Version 1709)
My GitHub Profile | Donations are appreciated if I could help you

scriptor
  • Members
  • 668 posts
  • Last active: Feb 15 2016 01:02 AM
  • Joined: 20 Jun 2013

may I recommend?:

ie := ComObjCreate("InternetExplorer.Application")
ie.Navigate("www.timeanddate.com")
While (ie.busy || or ie.Readystate <> 4)
	Sleep 100
msgbox % ie.Document.getElementbyId("clk_hm").innertext

I am sure that you can set up the timer and refresh and of course the re-direct of the "innertext" as well as set the time and off of that.

 

 

this script is accurately returning the correct time, and it shows it in a messagebox. However, it isn't changing the system clock to reflect it. I could always retrieve the time as a variable in the messagebox using WindowSpy, but then I'm unsure how I would take that variable and apply it to the system clock - is there a way to do that?



T_Lube
  • Members
  • 640 posts
  • Last active: Sep 09 2016 02:19 AM
  • Joined: 16 Oct 2014

yes:

netTime := ie.Document.getElementbyId("clk_hm").innertext
RunWait, %comspec% /c time %netTime%.%A_Sec%,, Hide


scriptor
  • Members
  • 668 posts
  • Last active: Feb 15 2016 01:02 AM
  • Joined: 20 Jun 2013

T_Lube - THANK YOU. This is awesome, it works. I'll set this up on a timer and it looks like it will *finally* do the trick. 

 

One last thing - the first script was returning the time as AM or PM accurately, but this time it's reversing the AM/PM. So for example if it's 5:30pm, it's changing the clock to 5:30am. Can it be changed back to the proper AM/PM? Thank you again.



SnowFlake_FlowSnake
  • Members
  • 845 posts
  • Last active: Jan 24 2016 05:24 PM
  • Joined: 08 Oct 2012

thank you also guys now i don't need my bios battery anymore


  • Download link of my scripts on Autohotkey.com 2/10/2015 [DOWNLAND]
  • Contact Info:  https://github.com/floowsnaake //  FloowSnaake(A)gmail.com
  • IF you need Help send me a PM,Email or Post on Github

  • Quote by tank  Posted 29 September 2015 - 06:14 PM

  • "Eventually i will find a way to convert the DB back to PHPBB3. but i dont have the bandwidth right now. No one that has tried has had success. It is the Only way i can keep this open is if i could successfully convert it."

scriptor
  • Members
  • 668 posts
  • Last active: Feb 15 2016 01:02 AM
  • Joined: 20 Jun 2013

I managed to find a workaround on the forums. I just added a bit of code that adds 12 hours to the time and now it adjusts the time accurately in terms of AM/PM. I put it in a timer and now it updates the time every 5 seconds. The only problem now is that every 5 seconds, it reverses the AM/PM again (because of the bottom part of the script where it adds the 12 hours to fix the initial problem) Either way - it's reporting the time correctly if we can ignore the am/pm [art. Thanks again to all who helped out :)

 

Here's the working code (no thanks to me but everyone else):


#NoTrayIcon
#Persistent

SetTimer, Check, 10000
Return

Check:
ie := ComObjCreate("InternetExplorer.Application")
ie.Navigate("www.timeanddate.com")
While (ie.busy || or ie.Readystate <> 4)
	Sleep 100
;msgbox % ie.Document.getElementbyId("clk_hm").innertext
DllCall("SetSystemTime", Str,T)

netTime := ie.Document.getElementbyId("clk_hm").innertext
RunWait, %comspec% /c time %netTime%.%A_Sec%,, Hide

RunWait, Taskkill /f /im iexplore.exe, , Hide ;this closes the instance of ie that was used to check the time. Potential problem - does it close this exact instance of ie.explore, or might it close other instances that are running simultaneously but not related to this script?


;enable the following section if you wish to add 12 hours to the reported time so it shows the am/pm part correctly. Problem is it will toggle the am/pm every 10 seconds when it performs the check. Best to ignore the am/pm and just look at the time itself. You can change the clock settings in Windows so it does not show AM/PM and shows only the time and that will resolve this issue

 /* ;remove this line to enable the section below
HourstoAdd = 12
Result += %HourstoAdd%,Hours
FormatTime,Out,%Result%,time
RunWait, %ComSpec% /c Time %Out%,,Hide
return
*/ ;remove this line to enable the section above


T_Lube
  • Members
  • 640 posts
  • Last active: Sep 09 2016 02:19 AM
  • Joined: 16 Oct 2014

You may want to make the timer something like every minute instead of 10 seconds. You should also be able to work out a method for always getting the correct time by referencing the am/pm on the same webpage. Maybe something a bit more robust. Untested so take it easy ;) :

Global ie := ComObjCreate("InternetExplorer.Application") ; set as super-global for use in functions
OnExit, CloseIE
setTime(navTimeAndGet())
SetTimer, UpdateClock, 30000 ; 30 seconds
Return

UpdateClock:
setTime(refreshTime())
return

;FUNCTIONS
refreshTime() {
	ieMethWait("Refresh")
	Return parseTime()
}

navTimeAndGet() {
	ieMethWait("Navigate", "www.timeanddate.com")
	Return parseTime()
}
parseTime() {
	rawTime := ie.Document.getElementbyId("clk_hm").innertext
	am_pm := ie.Document.getElementbyId("clk_am").innertext
	oTime := StrSplit(rawTime, ":")
	If (oTime[1] < 12 && inStr(am_pm, "pm")
		retTime := (oTime[1]+12) . ":" . oTime[2]
	Else if (oTime[1] = 12 && inStr(am_pm, "am")
		retTime := "00:" . oTime[2]
	Else
		retTime := rawTime
	Return retTime
}
ieMethWait(Method, Arg="") {
	ie[Method](Arg)
	While (ie.busy || or ie.Readystate <> 4)
		Sleep 100
	Return
}
setTime(newTime) {
	RunWait, %comspec% /c time %newTime%.%A_Sec%,, Hide
	Return ErrorLevel
}

 ; LABELS
CloseIE:
Try 
	ErrorLevel := ie.Quit()
Catch
	ExitApp


Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

Can this script be really used as a bios battery?

thank you also guys now i don't need my bios battery anymore



Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

This is also one of my regularly-used scripts:

; http://www.autohotkey.com/board/topic/2618-synchronize-clock-with-internet-time-servers-at-startup/

#SingleInstance Force
#NoEnv
SendMode Input
#Persistent
SetWorkingDir %A_ScriptDir%

; This script uses CMDTIME3 to synchronize the computer's date&time via Internet Time Servers.
; Get CMDTIME from: www.softshape.com/download
; The script writes the output of CMDTIME3 to a file CMDTIME.TXT in the machine's TEMP directory, etc.

myTemp = C:\Documents and Settings\HP_Administrator\Local Settings\Temp
cmdtimedir = C:\Documents and Settings\HP_Administrator\My Documents\AutoHotkey\Time_Synch

; Full name and path to cmdtime3 -> CUSTOMIZE AS NEEDED
cmdtimepath = cmdtime3.exe ; Assume in same directory as this script by default
;cmdtimepath = C:\Documents and Settings\HP_Administrator\My Documents\AutoHotkey\Time_Synch\cmdtime3.exe

Loop, 15 ; Arbitrary maximum to prevent infinite loop if some unforseen problem occurs
  {
  ; Delete previous CDMTIME.TXT temp file
  ;FileDelete, %temp%\CMDTIME.txt
  ;FileDelete, %myTemp%\CMDTIME.txt
  ;FileDelete, %cmdtimedir%\CMDTIME.txt

  ; Run the CMDTIME3 command -> CUSTOMIZE SERVER AS NEEDED
  ;RunWait, cmd /c "%cmdtimepath%" SYNC ntp2a.mcc.ac.uk>%temp%\CMDTIME.txt,,hide
  ;RunWait, %COMSPEC% /c CMDTIME3 SYNC /Q > "%myTemp%\CMDTIME.txt",,hide
  ;#######################################################################################################
  ;Winter time:												 #
  ;RunWait, %COMSPEC% /c CMDTIME3 SYNC /Q > "%cmdtimedir%\CMDTIME.txt",,hide ; Overwrites CMDTIME.txt	 #
  ;Summer time: Daylight saving time (DST): Add 60 minutes:						 #
  RunWait, %COMSPEC% /c CMDTIME3 SYNC /Q +60 > "%cmdtimedir%\CMDTIME.txt",,hide ; Overwrites CMDTIME.txt #
  ;#######################################################################################################
  ;;RunWait, Sync_Time.bat
  ; Read the CMDTIME.TXT temp file
  ;FileRead, cmdtime_result, %temp%\CMDTIME.txt
  FileRead, cmdtime_result, %cmdtimedir%\CMDTIME.txt

  ; If the file does not contain "Error" nor "Cannot", then everything was OK
  If cmdtime_result not contains Error,Cannot
    Break

  Sleep, 1000
  If (A_Index >= 15)
  MsgBox, Could not synch time!
  }
MsgBox % cmdtime_result
ExitApp


T_Lube
  • Members
  • 640 posts
  • Last active: Sep 09 2016 02:19 AM
  • Joined: 16 Oct 2014

No it cannot be used as a bios battery! that is just Snow Flake being sarcastic. He's just pointing out that instead of solving the actual problem, which might be the bios battery, we are writing a script. :)



scriptor
  • Members
  • 668 posts
  • Last active: Feb 15 2016 01:02 AM
  • Joined: 20 Jun 2013

Thanks, I've changed the script to run every 30 seconds or even every minute or so. I left the script running last night, as my computer doesn't go into hibernate mode overnight - everything runs as normal through the night - I just turn off the monitor until the morning.

 

This morning, the clock was messed up again, even though I was running the script on a timer. This I cannot figure out. So I re-loaded the script, and sure enough it's working again. It seems to lag or stop working at random times as well, in this instance it stopped functioning sometime overnight.

 

So I've changed the script so that script #1 is just the timer, and it's set to run the syncronization script instead. So in other words it looks like this:

SetTimer, Check, 60000
Return

Check:
Run, D:\MySyncronizationScript.ahk
Return

 

And then, "MySyncronizationScript" is as follows:

ie := ComObjCreate("InternetExplorer.Application")
ie.Navigate("www.timeanddate.com")
While (ie.busy || or ie.Readystate <> 4)
	Sleep 100
;msgbox % ie.Document.getElementbyId("clk_hm").innertext
DllCall("SetSystemTime", Str,T)



netTime := ie.Document.getElementbyId("clk_hm").innertext
RunWait, %comspec% /c time %netTime%.%A_Sec%,, Hide

RunWait, Taskkill /f /im iexplore.exe, , Hide

return

and I'm hoping this time it'll work, fingers crossed.