Jump to content

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

Atomic Time Sync.


  • Please log in to reply
15 replies to this topic
maestrith
  • Members
  • 786 posts
  • Last active: Apr 10 2019 01:28 PM
  • Joined: 17 Sep 2005
It has taken me about 2 days worth of coding, and re-coding, but I think I finally got it.
regread, current, HKEY_LOCAL_MACHINE, SYSTEM\CurrentControlSet\Control\TimeZoneInformation, standardname
RegRead, Difference, HKEY_LOCAL_MACHINE, SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\%current%, Display
stringmid, count1, difference, 5,3
Stringmid, count2, difference, 9,2
urldownloadtofile, http://132.163.4.101:14, time.txt
FileReadLine, time, time.txt, 2
StringSplit, time, time, %A_Space%
stringreplace, date, time2, -,,all
stringreplace, time, time3, :,,all
  if date is not number
   {
    msgbox There was a problem with the time synchronization`nPlease try again at a later time.
    exitapp
   }
  today = 20%date%%time%
  today += %count1%, hh
  today += %count2%, mm
  formattime, temp, %today%
  stringmid, year, today, 3,2
  stringmid, month, today, 5,2
  stringmid, day, today, 7,2
  stringmid, hour, today, 9,2
  stringmid, min, today, 11,2
  stringmid, sec, today, 13,2
FileAppend, date %day%-%month%-%year%`ntime %hour%:%min%:%sec%.00, %a_scriptdir%\temptime.bat ; Create the batch file that actually changes the time
loop, %USERPROFILE%\Local Settings\Temporary Internet Files\132.163*.*, 1, 1
{
 filedelete, %A_LoopFileFullPath% ; if you don't do this, it will keep the same time as you last ran the program.
}
filedelete, %a_scriptdir%\time.txt
runwait, %a_scriptdir%\temptime.bat,, hide ;run it :)
FileDelete, %a_scriptdir%\temptime.bat ; start with a fresh file
Let me know if there is something that I could do better, easier, or whatever.

Side note:
this does not work with Jerusalem, or kuala, lumpur singapore. I will update that after I get a nap :)

toralf
  • Moderators
  • 4035 posts
  • Last active: Aug 20 2014 04:23 PM
  • Joined: 31 Jan 2005
Ok, you asked for it.

here are my comments:
a) The loop at the beginning should be more specificly name the files that get deleted, so that only the correct once get removed. 132*.* is a little bit too general.

b) I would place this line
filedelete, %a_scriptdir%\time.txt
right after the time when the file isn't needed any more. So that no junk is left on the disc. I know that it is easier for debuging to delete it at the next run to have it this way.

c) The lines
StringMid, variant, difference, 5, 6 
stringleft, count1, variant, 3 
Stringright, count2, variant, 2
Could be replaced with just two StringMid.

d) The code will onyl continue after URLDownlaodToFile, when URLDownlaodToFile is done downlaoded. So these lines
urldownloadtofile, http://132.163.4.101:14, time.txt 
read: 
FileReadLine, time, time.txt, 2 
if time = 
 { 
  goto read ; loop until the file downloads 
 }
do not work. 1) the loop will only be executed after the file is doenloaded. 2) if the file couldn't be downloaded it will contain some other text then the desired one. Thus you have to check for other things to make sure you got the correct file. E.g. FileSize, the file content or the length of the file content

e) If the download fails, the code should give feedback to user and exit.

f) I believe that you do not need to create a batch file to run "date". I guess you could use the run command directly in the script to execute that comand.

Happy scripting.
Ciao
toralf
 
I use the latest AHK version (1.1.15+)
Please ask questions in forum on ahkscript.org. Why?
For online reference please use these Docs.

maestrith
  • Members
  • 786 posts
  • Last active: Apr 10 2019 01:28 PM
  • Joined: 17 Sep 2005
The things we tend to overlook when in the middle of writing a script :) after I make the adjustments to the script I will have it delete the temp file right away. Now that I think about it, some sort of varification is needed for the download of the file...maybe a restart of the script or something might be a good alternative. I will do my best to fix it so that it is a bit more user friendly. The original intent for the script is for a "lockdown" code that I am writing for my kids computers to make sure that after boot, the time is right so that the triggers for "bedtime" and what not are accurate. As far as the temp file, the actual name of it is something like 132.163.4.[1] or something, similar, and it's not something that I could figure out how to phrayse it to delete it...I changed it to 132.163*.*, that should narrow it down a bit.

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
In case it's useful, you can also change the system date and time via DllCall as shown in the examples at the bottom of the DllCall page.

maestrith
  • Members
  • 786 posts
  • Last active: Apr 10 2019 01:28 PM
  • Joined: 17 Sep 2005
I have read over the dllcall page, and I have a quick question in regard to the time setting application of it. Is the DLL available in windows, or is it something that I would have to write myself to interface with the windows clock. Thanks in advance.

Chris
  • Administrators
  • 10727 posts
  • Last active:
  • Joined: 02 Mar 2004
SetSystemTime() should be available in all versions of Windows.

humigeri
  • Members
  • 11 posts
  • Last active: Apr 07 2006 08:07 AM
  • Joined: 09 Feb 2006
Thanks for the script! Very, very useful!!

Although that time zone registry read didn't work for me.

I am having Hungarian Windows, and it is the translated name of the timezone stored in SYSTEM\CurrentControlSet\Control\TimeZoneInformation\standardname entry. ("Közép-európai téli idő")
Meanwhile the subkeys of SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones are identified by their English names. ("Central European Standard Time") :)

Fortunately, its "Std" string stores the Hungarian name, so a loop can help to find the appropriate subkey:
Loop, HKEY_LOCAL_MACHINE, SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones, 2
{
  RegRead, lStd, HKEY_LOCAL_MACHINE, %A_LoopRegSubKey%\%A_LoopRegName%, Std 
  if lStd = %current%
    RegRead, Difference, HKEY_LOCAL_MACHINE, %A_LoopRegSubKey%\%A_LoopRegName%, Display 
}


gguueesstt
  • Guests
  • Last active:
  • Joined: --
@maestrith
Good script!
I want change the time for my zone (-2h)
I tray to change the numbers of count1, difference, x,x
but no result :oops:
stringmid, count1, difference, 5,3
Stringmid, count2, difference, 9,2 
How to proced???
Thank you in advance!

humigeri
  • Members
  • 11 posts
  • Last active: Apr 07 2006 08:07 AM
  • Joined: 09 Feb 2006

I want change the time for my zone (-2h)
I tray to change the numbers of count1, difference, x,x
but no result :oops:


No, that is a string related command, not a subtract operator. It tries to cut out a part of a string. For example cut the "-10" from the string "(GMT-10:00) Hawaii".

I guess you are facing the same problem as I. The problem can come from the registry, where your computer may store the informations of time zones in a different way than maestrith's comp does.

Why don't you to try my code in my last (and the very first here as well:) ) post.

So instead of
RegRead, Difference, HKEY_LOCAL_MACHINE, SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones\%current%, Display

use this one:

Loop, HKEY_LOCAL_MACHINE, SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones, 2
{
  RegRead, lStd, HKEY_LOCAL_MACHINE, %A_LoopRegSubKey%\%A_LoopRegName%, Std
  if lStd = %current%
    RegRead, Difference, HKEY_LOCAL_MACHINE, %A_LoopRegSubKey%\%A_LoopRegName%, Display
}

I hope it will help

gguueessttt
  • Guests
  • Last active:
  • Joined: --
Thank you, Humigeri, I try but make the same result;
now I think is better copy in the temptime.bat date (must be ordered 14 02 2006) and time (eg 13:08:44 (-2)) but I am stuck....

my time.txt:
53780 06-02-14 13:08:44 00 0 0 206.7 UTC(NIST) * 

urldownloadtofile, http://132.163.4.101:14, time.txt
FileDelete, %a_scriptdir%\temptime.bat

FileReadLine, time, time.txt, 2
StringSplit, time, time, %A_Space%
;MsgBox, %time%.
StringSplit, word_array, time, %A_Space%, 
;MsgBox, %word_array3%.
MsgBox, %word_array2%.
?????? How to....???
Somebody can help me please?

humigeri
  • Members
  • 11 posts
  • Last active: Apr 07 2006 08:07 AM
  • Joined: 09 Feb 2006
Well, gguueessttt, I think I don't understand your problem exactly.

You succeed to change your system time, but it gains 2 hours, or you cannot change the time at all?

What did you changed compared to meastrith's script?

ggguesstt
  • Guests
  • Last active:
  • Joined: --
Hi, Humigeri.
I'm apologize for my poor explication and English.

Whit your modify script the result is: (the same
13:24

time.txt
53781 06-02-15 13:24:33 00 0 0 832.7 UTC(NIST) *

I need 11:24

Thank you!

humigeri
  • Members
  • 11 posts
  • Last active: Apr 07 2006 08:07 AM
  • Joined: 09 Feb 2006
Well, if nothing can help, you should replace
today += %count1%, hh
with
today += -2, hh

I guess, your time zone doesn't change so frequently, so i think it's nothing wrong with this bit of hardcoding.

Anyway, I would be interested in what is your registry value of "HKEY_LOCAL_MACHINE, SYSTEM\CurrentControlSet\Control\TimeZoneInformation, standardname", and what kind of subkeys are defined under "HKEY_LOCAL_MACHINE, SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones". Those values must cause your problem, definitely.

guuuesssttt
  • Guests
  • Last active:
  • Joined: --
Hi Humigeri,
Now run fine, thank you very much!

My registry value of "HKEY_LOCAL_MACHINE, SYSTEM\CurrentControlSet\Control\TimeZoneInformation, standardname",
is "Hora oficial do Brasil", but I'm interested at -2

Thank you again.
Tchau!

mhe
  • Members
  • 40 posts
  • Last active: Dec 04 2015 10:29 PM
  • Joined: 18 Oct 2007
Im not sure what's wrong, my level of scripting is pretty rudimentary, im sure it's something easy.

The url http://132.163.4.101:14 seems to work fine, but no txt file is created.

"Error at line 19.

Line Text: if date is no number
Error: This line does not contain a recognized action

The program will exit."