Jump to content

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

Synchronize clock with Internet Time Servers at startup


  • Please log in to reply
16 replies to this topic
jordis
  • Members
  • 81 posts
  • Last active: Aug 16 2010 11:07 AM
  • Joined: 30 Jul 2004
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,

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

keitay
  • Members
  • 1 posts
  • Last active: Aug 09 2005 12:33 AM
  • Joined: 09 Aug 2005
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


tpatel5
  • Members
  • 64 posts
  • Last active: Jan 30 2006 10:19 PM
  • Joined: 27 Oct 2004
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.

evl
  • Members
  • 1237 posts
  • Last active: Oct 20 2010 11:41 AM
  • Joined: 24 Aug 2005
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.

; 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


evl
  • Members
  • 1237 posts
  • Last active: Oct 20 2010 11:41 AM
  • Joined: 24 Aug 2005
In case anyone used the script I posted above, I updated it to fix a bug in checking the time since the last update.

M3CSL
  • Members
  • 36 posts
  • Last active: May 13 2009 11:05 AM
  • Joined: 21 Jan 2008
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:

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:

net time /setsntp:ptbtime1.ptb.de

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

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

Kustardking
  • Guests
  • Last active:
  • Joined: --
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.

Froix
  • Guests
  • Last active:
  • Joined: --
Just posting to say thanks!

My MOBO's a bit defective and the BIOS resets every other 2 or 3 days and I'm having to reset date every time. "w32tm /resync" can't set date and larger time differences but this one works wonderfully.

D:\Tools>cmdtime3 /m:99999999 /q sync

* CmdTime 3.0 (c) Jury Gerasimov, 2000-2002.
* Adjusting time via Internet Time Servers.
* Contact author: http://www.softshape.com

Connecting time.nist.gov...  OK.

Current time   : 11/6/2003 9:44:50 AM
Suggested time : 11/6/2010 9:44:51 AM
Inaccuracy     : +2557 day(s) 00:00:01

Time is set to 11/6/2010 9:44:51 AM succesfully.


badmojo
  • Members
  • 204 posts
  • Last active: Apr 14 2014 01:08 AM
  • Joined: 11 Nov 2005
cool tool, helped me too with my computer's date/time.. :)

Evano
  • Guests
  • Last active:
  • Joined: --
@ Jardi

I am a novice.

I downloaded CMDTIME3.EXE and copied your script.

Now, to use your script,
a) what file extension I should give for the script file
B) do I need to have some speciifc software component to compile/execute it

Thanks in advance for your help..

  • Guests
  • Last active:
  • Joined: --
Just as a remark ,the original post was from march 2005!

If you want to run the script you have to install autohotkey and name the script with .ahk extension.

<!-- m -->http://www.autohotke...cs/Tutorial.htm<!-- m -->


If you so want you can compile it to exe with the program provided with the installation of autohotkey. (<!-- m -->http://www.autohotke...pts.htm#ahk2exe<!-- m -->)

rodfell
  • Members
  • 138 posts
  • Last active: Jun 26 2011 10:02 PM
  • Joined: 05 Oct 2007
this is what i use. forget where i pinched this code from as i got it years ago.
downloaderror=

gosub downloadurl

If downloaderror

	{

	MsgBox,,Time Sync,Time sync is not available,2

	return

	}

FileReadLine x,%a_temp%\synctime ,2

VarSetCapacity(T,16,0)

DllCall("RtlFillMemory",UInt,&T,  UInt,1,UChar,20 SubStr(x,7,2))

DllCall("RtlFillMemory",UInt,&T+1,UInt,1,UChar,(20 SubStr(x,7,2))>>8)

DllCall("RtlFillMemory",UInt,&T+2,UInt,1,UChar,SubStr(x,10,2))

DllCall("RtlFillMemory",UInt,&T+6,UInt,1,UChar,SubStr(x,13,2))

DllCall("RtlFillMemory",UInt,&T+8,UInt,1,UChar,SubStr(x,16,2))

DllCall("RtlFillMemory",UInt,&T+10,UInt,1,UChar,SubStr(x,19,2))

DllCall("RtlFillMemory",UInt,&T+12,UInt,1,UChar,SubStr(x,22,2))

DllCall("SetSystemTime",Str,T)

PostMessage 0x1E,,,,ahk_class Shell_TrayWnd

MsgBox,,Time Sync,Time updated,1

Return



downloadURL:

UrldownLoadToFile http://128.138.140.44:13 ,%a_temp%\synctime

if not errorlevel 

return

UrldownLoadToFile http://68.216.79.113:13 ,%a_temp%\synctime

if not errorlevel 

return

UrldownLoadToFile http://129.6.15.28:13 ,%a_temp%\synctime

if not errorlevel 

return

downloaderror:=true

return


sam__
  • Guests
  • Last active:
  • Joined: --
Hey guys,

I know this is an old thread, but the CMOS battery in my computer is kaput and I'm having to resync the time at every start up, resume from hibernation and resume from sleep.

This script runs at start up, but is there any way i can make it run from hibernate and sleep resumes? if someone could help me, thatd be dandy

thanks,

sam

Galton
  • Members
  • 3 posts
  • Last active: May 31 2013 04:29 AM
  • Joined: 01 Nov 2011
I have the same problem with my computer and I have been using Hibernate Trigger for two years.

Download at:

http://www.desimones...igger/index.php

it can run any batch file on resuming from Hibernation, including resync the clock.

  • Guests
  • Last active:
  • Joined: --
If you want to use ahk code:

This informs you of a return from hibernation so you can use as trigger for the sync code.

<!-- m -->http://www.autohotke...topic40008.html<!-- m -->