AutoHotkey Community

It is currently May 27th, 2012, 10:40 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 15 posts ] 
Author Message
PostPosted: March 12th, 2005, 9:03 pm 
Offline

Joined: July 30th, 2004, 9:08 pm
Posts: 81
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject: Time sync on startup
PostPosted: August 9th, 2005, 1:41 am 
Offline

Joined: August 9th, 2005, 1:23 am
Posts: 1
Location: Sydney Australia
The following works for me on broadband using XP's inbuilt w32tm.exe

Code:
;Startup.ahk   
#NoTrayIcon

Sleep,15000                                       ;wait till online
Run, C:\WINDOWS\system32\w32tm.exe /resync /nowait    ;Set System Time

_________________
keitay


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 9th, 2005, 4:44 pm 
Offline

Joined: October 27th, 2004, 1:22 am
Posts: 64
Location: GA
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 8)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 6th, 2006, 9:03 pm 
Offline

Joined: August 24th, 2005, 5:17 pm
Posts: 1237
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 15th, 2006, 3:25 pm 
Offline

Joined: August 24th, 2005, 5:17 pm
Posts: 1237
In case anyone used the script I posted above, I updated it to fix a bug in checking the time since the last update.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 27th, 2008, 3:29 pm 
Offline

Joined: January 21st, 2008, 6:15 pm
Posts: 36
Location: Germany
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


Report this post
Top
 Profile  
Reply with quote  
PostPosted: July 20th, 2008, 2:17 am 
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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: November 6th, 2010, 2:50 am 
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.

Code:
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.


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 10th, 2011, 1:56 pm 
Offline

Joined: November 11th, 2005, 3:13 am
Posts: 202
cool tool, helped me too with my computer's date/time.. :)


Report this post
Top
 Profile  
Reply with quote  
PostPosted: February 6th, 2011, 11:34 am 
@ 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..


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 7th, 2011, 12:06 pm 
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.

http://www.autohotkey.com/docs/Tutorial.htm


If you so want you can compile it to exe with the program provided with the installation of autohotkey. (http://www.autohotkey.com/docs/Scripts.htm#ahk2exe)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 8th, 2011, 1:42 pm 
Offline

Joined: October 5th, 2007, 2:21 am
Posts: 138
Location: Bundaberg (Bundy), Qld, Australia
this is what i use. forget where i pinched this code from as i got it years ago.
Code:
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject: help
PostPosted: February 23rd, 2011, 2:08 pm 
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


Report this post
Top
  
Reply with quote  
 Post subject: Use Hibernate Trigger
PostPosted: January 17th, 2012, 6:44 am 
Offline

Joined: November 1st, 2011, 1:15 am
Posts: 3
I have the same problem with my computer and I have been using Hibernate Trigger for two years.

Download at:

http://www.desimonesystems.com/suspendtrigger/index.php

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 17th, 2012, 5:08 pm 
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.

http://www.autohotkey.com/forum/topic40008.html


Report this post
Top
  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 15 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 12 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group