AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Synchronize clock with Internet Time Servers at startup

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
jordis



Joined: 30 Jul 2004
Posts: 78

PostPosted: Sat Mar 12, 2005 9:03 pm    Post subject: Synchronize clock with Internet Time Servers at startup Reply with quote

Hi,
I just created a tiny simple script to synchronize the comptuer's date/time with an Internet Time Server...
It uses CMDTIME 3, a small cmd-line application which I posted here

As I say in that post,
Quote:
I was looking for a way to synchronize my clock with an Internet Time Server, but the most of the utilities I found wanted to stay resident in the System Tray... Rather than synchonizing at given periods of time, for me it's enough to synchronize the clock once at Windows startup (without having to have a dedicated program sitting on the tray all the time!)


This is the script I created to perform the synchronization:
Code:
; 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.

; Full name and path to cmdtime3 -> CUSTOMIZE AS NEEDED
cmdtimepath=%ProgramFiles%\cmdtime\cmdtime3.exe

; Check if CMDTIME3.EXE exists in the given path
ifnotexist, %cmdtimepath%
{
   msgbox, Unable to find CMDTIME in:`n%cmdtimepath%
   exitapp
}

Loop
{
   ; Delete previous CDMTIME.TXT temp file
   FileDelete, %temp%\CMDTIME.txt

   ; Run the CMDTIME3 command -> CUSTOMIZE AS NEEDED
   RunWait, cmd /c "%cmdtimepath%" /Q SYNC>%temp%\CMDTIME.txt,,hide

   ; Read the CMDTIME.TXT temp file
   FileRead, cmdtimeresult, %temp%\CMDTIME.txt

   ; If the file does not contain "Error" nor "Cannot", then everything was OK
   if cmdtimeresult not contains Error,Cannot
      break     
   
   ; Otherwise, it probably means that the computer is still starting up...
   Sleep, 5000
}


Take into account that this script should NOT be used (as such) by users using a DIAL-UP connection to internet. The script assumes that an Internet connection is available.

For my purposes, I added a shortcut to this script in the Windows "Startup" program group, so my clock is synchronized at every boot.
If the Internet connection is not yet available when the script runs, it waits for 5 seconds and tries again...

These CMDTIME3 switches ( /Q SYNC ) work for me. You should customize those to suit your purposes (you can specify a variety of command-line options, including your favourite Internet Time Servers).

Another thing: probably administrator's rights are needed in order to change the system time... I'm not really sure...

regards
jordi
Back to top
View user's profile Send private message
keitay



Joined: 09 Aug 2005
Posts: 1
Location: Sydney Australia

PostPosted: Tue Aug 09, 2005 1:41 am    Post subject: Time sync on startup Reply with quote

The following works for me on broadband using XP's inbuilt w32tm.exe

;Startup.ahk
#NoTrayIcon

Sleep,15000 ;wait till online
Run, C:\WINDOWS\system32\w32tm.exe /resync /nowait ;Set System Time
_________________
keitay
Back to top
View user's profile Send private message
tpatel5



Joined: 27 Oct 2004
Posts: 65
Location: GA

PostPosted: Tue Aug 09, 2005 4:44 pm    Post subject: Reply with quote

I don't know if you know this, but
for microsoft xp automatically synchronize with the internet time if you duble click clock in the lower righ corner of the system tray menu, go to internet time, and select the chek box if it is uncheck.
_________________
-Tru Cool
Back to top
View user's profile Send private message
evl



Joined: 24 Aug 2005
Posts: 1238

PostPosted: Mon Feb 06, 2006 9:03 pm    Post subject: Reply with quote

I added a couple of things, mostly a TrayTip popup, to the 1st script so here it is if anyone's interested.

It is intended to be placed in the same directory as cmdtime3.exe and a shortcut to the script placed in the startup folder.

I also changed a couple of things from the first script that people may want to change or comment out:
- The Runwait line was changed to a UK server.
- I put a 2 minute sleep at the start so it didn't run while the computer was still booting up.
- I added a check to only run it after 7 days had passed so that it didn't run every single time.

Code:

; 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.

#SingleInstance

; Full name and path to cmdtime3 -> CUSTOMIZE AS NEEDED
cmdtimepath=cmdtime3.exe ; assume in same directory as this script by default

Sleep, 120000 ; optional 2 minute sleep - e.g. wait until after computer is done booting up

; Only run once a week at minimum
FileGetTime, Last_Run, %temp%\CMDTIME.txt
Last_Run -= %A_Now%, Minutes
Last_Run := 0 - Last_Run ; convert to positive number
If Last_Run between 1 and 10000 ; 10,000 = approx 7 days (in mins). If 0 then file doesn't exist.
  ExitApp

; Check if CMDTIME3.EXE exists in the given path
Ifnotexist, %cmdtimepath%
  {
  Msgbox, Unable to find CMDTIME in:`n%A_ScriptDir%
  Exitapp
  }

Loop, 20 ; arbitrary maximum to prevent infinite loop if some unforseen problem occurs
  {
  ; Delete previous CDMTIME.TXT temp file
  FileDelete, %temp%\CMDTIME3_temp.txt

  ; Run the CMDTIME3 command -> CUSTOMIZE SERVER AS NEEDED
  ; see http://support.microsoft.com/default.aspx?scid=kb;EN-US;q262680#top for other servers too
  RunWait, cmd /c "%cmdtimepath%" SYNC ntp2a.mcc.ac.uk>%temp%\CMDTIME.txt,,hide

  ; Read the CMDTIME.TXT temp file
  FileRead, cmdtime_result, %temp%\CMDTIME.txt

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

  ; Otherwise, it probably means that the computer is still starting up...
  Sleep, 10000
  }

Time_Is_Set_To__Start_Pos := InStr(cmdtime_result, "Time is set to")
Time_Is_Set_To__End_Pos := InStr(cmdtime_result, " succesfully", false, Time_Is_Set_To__Start_Pos)
Time_Is_Set_To__Count := Time_Is_Set_To__End_Pos - Time_Is_Set_To__Start_Pos
StringMid, Time_Is_Set_To, cmdtime_result, %Time_Is_Set_To__Start_Pos%, Time_Is_Set_To__Count
StringReplace, Time_Is_Set_To, Time_Is_Set_To,Time is set to ,Time set to  :
StringReplace, Time_Is_Set_To, Time_Is_Set_To,Successfully,

Inaccuracy__Start_Pos := InStr(cmdtime_result, "Inaccuracy     : ")
Inaccuracy__End_Pos := InStr(cmdtime_result, "`n", false, Inaccuracy__Start_Pos)
Inaccuracy__Count := Inaccuracy__End_Pos - Inaccuracy__Start_Pos
StringMid, Inaccuracy, cmdtime_result, %Inaccuracy__Start_Pos%, Inaccuracy__Count
StringReplace, Inaccuracy, Inaccuracy,Inaccuracy,Adjusted

TrayTip, Time:, %Inaccuracy%`n%Time_Is_Set_To%
Sleep, 10000 ; give a chance to read traytip before exiting
Back to top
View user's profile Send private message
evl



Joined: 24 Aug 2005
Posts: 1238

PostPosted: Wed Feb 15, 2006 3:25 pm    Post subject: Reply with quote

In case anyone used the script I posted above, I updated it to fix a bug in checking the time since the last update.
Back to top
View user's profile Send private message
M3CSL



Joined: 21 Jan 2008
Posts: 35
Location: Germany

PostPosted: Sun Jan 27, 2008 3:29 pm    Post subject: Reply with quote

You don't need AHK for time update, windows xp handles this job automatically. To update time every 24 hours just create a .reg file with this content and execute it:

Code:
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\W32Time\TimeProviders\NtpClient]
"SpecialPollInterval"=dword:00015180


To set a new timeserver for sync, just execute this line:

Code:
net time /setsntp:ptbtime1.ptb.de


To modfiy the entrys in timedate.cpl just run this in a reg file:

Code:
Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\DateTime\Servers]
@="0"
"0"="ptbtime1.ptb.de"
"1"="ptbtime2.ptb.de"


CU
Back to top
View user's profile Send private message
Kustardking
Guest





PostPosted: Sun Jul 20, 2008 2:17 am    Post subject: Use XP built in w32tm /resync Reply with quote

I know this post is old, but if anyone lands here you might find that the following command-line is the most efficient and least breaky way to go...

w32tm /resync


Put that in a startup script and you're done.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group