Jump to content

TimeSync - Synchronizes System Time with Internet Time Server


jennico
 Share

Recommended Posts

do you know how often windows synchronizes the time in your pc (if you did not turn it off) ?

Once a week ! - this is far to rare.

do you mistrust third party applications that set the clock for you ?

then here is a solution: (and i think it has not been done before in AutoIt language):

(and, as always in my scripts, no includes, no problems ever.)

TimeSync

#cs
    ;#=#INDEX#===================================================#
    ;#  Title .........: TimeSync.au3
    ;#  Description ...: Synchronizes System Time with Internet Time Server
    ;#  Date ..........: 23.11.08
    ;#  Version .......: 1.4
    ;#  Author ........: jennico (jennicoattminusonlinedotde)
    ;#                   ©  2008 by jennico
    ;#===========================================================#
#ce

Opt("TrayOnEventMode",1)
Opt("TrayAutoPause",0)
Opt("TrayMenuMode",1)

$ini="TimeSync.ini"
$start=@WindowsDir&"\Media\start.wav"
$sys="short Y;short M;short Dow;short D;short H;short Mi;short S;short MS"
$system=DllStructCreate($sys)

Dim $ser[5]=["64.125.78.85","128.138.140.44","132.163.4.102","132.163.4.101","129.6.15.29"]
Dim $update[9]=[1440,720,540,360,180,60,30,15,1]
Dim $set[10]

$mode=IniRead($ini,"Settings","Mode",8)
$tip=IniRead($ini,"Settings","Tip",1)

SoundPlay($start)

TraySetClick(8)

TraySetIcon("mmcndmgr.dll",-15)

TraySetToolTip("TimeSync  ©  2008 by jennico")

TraySetOnEvent(-7,"_Sound")

TrayCreateItem("Show TrayTip")
    If $tip=1 Then TrayItemSetState(-1,1)
    TrayItemSetOnEvent(-1,"_Tip")
TrayCreateItem("")
$settings=TrayCreateMenu("Settings")
    $set[0]=TrayCreateItem("Once a Day",$settings,-1,1)
        TrayItemSetOnEvent(-1,"_Set")
    TrayCreateItem("",$settings)
    $set[1]=TrayCreateItem("Every 12 Hours",$settings,-1,1)
        TrayItemSetOnEvent(-1,"_Set")
    $set[2]=TrayCreateItem("Every 9 Hours",$settings,-1,1)
        TrayItemSetOnEvent(-1,"_Set")
    $set[3]=TrayCreateItem("Every 6 Hours",$settings,-1,1)
        TrayItemSetOnEvent(-1,"_Set")
    $set[4]=TrayCreateItem("Every 3 Hours",$settings,-1,1)
        TrayItemSetOnEvent(-1,"_Set")
    $set[5]=TrayCreateItem("Every Hour",$settings,-1,1)
        TrayItemSetOnEvent(-1,"_Set")
    TrayCreateItem("",$settings)
    $set[6]=TrayCreateItem("Every 30 Minutes",$settings,-1,1)
        TrayItemSetOnEvent(-1,"_Set")
    $set[7]=TrayCreateItem("Every 15 Minutes",$settings,-1,1)
        TrayItemSetOnEvent(-1,"_Set")
    $set[8]=TrayCreateItem("Every Minute",$settings,-1,1)
        TrayItemSetOnEvent(-1,"_Set")
    TrayCreateItem("",$settings)
    $set[9]=TrayCreateItem("Just on Trayclick",$settings,-1,1)
        TrayItemSetOnEvent(-1,"_Set")
TrayItemSetState($set[$mode],1)
TrayCreateItem("")
TrayCreateItem("Exit")
    TrayItemSetOnEvent(-1,"_Exit")

If $mode<9 Then AdlibEnable("_Adlib",$update[$mode]*60000)

_Adlib()

While 1
    Sleep(100)
WEnd

Func _Adlib()
    Do
        Dim $best=0,$min=500
        For $i=0 To 4
            $ping=Ping($ser[$i],500)
            If $ping>0 And $ping<$min Then Dim $best=$i,$min=$ping
        Next
        $o=ObjCreate("InternetExplorer.Application")
        $o.navigate("http://"&$ser[$best]&":13")
        While String($o.readyState)<>"complete" And $o.readyState<>4
            Sleep(100)
        WEnd
        While String($o.document.readyState)<>"complete" And $o.document.readyState<>4
            Sleep(100)
        WEnd
        $data=StringSplit($o.document.body.innerText," ")
        $o.quit()
        $o=0
    Until $data[0]=10
    $tSystemTime=$system
    $date=StringSplit($data[2],"-")
    $time=StringSplit($data[3],":")
    DllStructSetData($tSystemTime,"M",$date[2])
    DllStructSetData($tSystemTime,"D",$date[3])
    DllStructSetData($tSystemTime,"H",$time[1])
    DllStructSetData($tSystemTime,"S",$time[3])
    DllStructSetData($tSystemTime,"Mi",$time[2])
    DllStructSetData($tSystemTime,"MS",$data[7])
    DllStructSetData($tSystemTime,"Y","20"&$date[1])
    DllCall("Kernel32.dll","int","SetSystemTime","ptr",DllStructGetPtr($tSystemTime))
;   DllCall("Kernel32.dll","int","SetSystemTime","ptr",DllStructGetPtr($tSystemTime))
    If $tip=1 Then TrayTip("TimeSync","Sytem Time has been updated. ["&$best&"]",5,1)
EndFunc

Func _Set()
    For $i=0 To 9
        $j=$set[$i]
        If $j=@TRAY_ID Then
            If $i=9 Then
                AdlibDisable()
            Else
                AdlibEnable("_Adlib",$update[$i]*60000)
            EndIf
            $mode=$i
        Else
            TrayItemSetState($j,4)
        EndIf
    Next
    SoundPlay($start)
EndFunc

Func _Tip()
    SoundPlay($start)
    $tip=($tip=0)*1
    If $tip=1 Then TrayItemSetState(@TRAY_ID,1)
EndFunc

Func _Sound()
    SoundPlay($start)
    _Adlib()
EndFunc

Func _Exit()
    IniWrite($ini,"Settings","Mode",$mode)
    IniWrite($ini,"Settings","Tip",$tip)
    SoundPlay($start)
    Exit
EndFunc

Enjoy.

j.

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

@HeffeD How do you set it to be daily? I have XP SP3 and it sets once a week.

@jennico - Nice script, thanks.

Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

@ResNullius :

this is interesting. i couldn't check daylight setting.

in canada still daylight ? then please uncomment line

;   DllCall("Kernel32.dll","int","SetSystemTime","ptr",DllStructGetPtr($tSystemTime))

or try "SetLocalTime" instead of "SetSystemTime".

i really would like to know how this can be done. the Date.au3 include is not clear at this point. please check out _Date_Time_SetLocalTime() and _Date_Time_SetSystemTime() in Date.au3.

; The system uses UTC internally. When you call SetLocalTime, the system uses the current time zone information to perform the

; conversion, incuding the daylight saving time setting. The system uses the daylight saving time setting of the current time,

; not the new time you are setting. This is a "feature" according to Microsoft. In order to get around this, we have to call

; the function twice. The first call sets the internal time zone and the second call sets the actual time.

i cannot try it out because there's is no daylight season any more here in Europe.

plz tell me your results.

j.

Or: you find canadian time servers and add them to the list or you correct the hour calculation in the script.

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

@martin: 7 days is default

you can use w32tm.exe or

1. HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services \W32Time\TimeProviders\NtpClient

2. Select SpecialPollInterval

3. Change decimal value from 604800 to some other value in seconds. EX: 172800 (2 days)

to change the interval.

j.

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

i've been searching msdn for the daylight issue.

it is very confusing. "SetSystemTime" is said to set time to UCT while "SetLocalTime" sets to local time including daylight settings but i tried that and it's not true. "SetSytemTime" is obviously correct for setting up local time.

another point: vista and xp have different methods for the daylight correction.

i don't think i'll be able to understand all these implementations, maybe someone else can give a correct solution.

j.

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

@martin: 7 days is default

you can use w32tm.exe or

ZITAT

1. HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services \W32Time\TimeProviders\NtpClient

2. Select SpecialPollInterval

3. Change decimal value from 604800 to some other value in seconds. EX: 172800 (2 days)

to change the interval.

j.

sure it did not come with sp2. you tweaked it some time.

j.

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

then please uncomment line

;   DllCall("Kernel32.dll","int","SetSystemTime","ptr",DllStructGetPtr($tSystemTime))
didn't make a difference

or try "SetLocalTime" instead of "SetSystemTime".

This sets it 3 hours early

Or: you find canadian time servers and add them to the list or you correct the hour calculation in the script.

the only one I can consistently check is on port 13 is timelord.uregina.ca and it returns info as:

16 NOV 2008 14:39:20 CST

Which doesn't parse properly, so fails

You might also want to look into the w32tm command http://technet.microsoft.com/en-us/library...imes_tools_dyax

Edited by ResNullius
Link to comment
Share on other sites

don't know what is wrong. before i posted the example i worked the method out: here.

so i thought it works for everyone. :mellow: i'm sorry.

i will have to wait until summer, it makes no sense working on that without being able to try it out.

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

sure it did not come with sp2. you tweaked it some time.

Yes, I did see your post on how to change it, but I can guarantee you that I never did this. I bought my system new from Dell and it has done this from the day I took possession of it. Maybe they change it on their systems? I don't know, I just assumed everyones system did it daily.

Link to comment
Share on other sites

once again i had to edit the code. _InetGetSource fails too often and tends to block the script. the good old dirty IE stuff is more reliable.

j.

Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

updated the script once more. one of the five servers did not return correct time, had to exchange the ip.

maybe that this was the reason for the "Canadian problem".

j.

Edited by jennico
Spoiler

I actively support Wikileaks | Freedom for Julian Assange ! | Defend freedom of speech ! | Fight censorship ! | I will not silence.OixB7.jpgDon't forget this IP: 213.251.145.96

 

Link to comment
Share on other sites

updated the script once more. one of the five servers did not return correct time, had to exchange the ip.

maybe that this was the reason for the "Canadian problem".

j.

"Canadian problem" solved.

Looks like it's working great now jennico.

Thanks!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...