Jump to content

problem with @hour & @min macros ?


Recommended Posts

Hello

My purpose is to make script which will set the original PC time back if it is changed matching a difference +5 or -5 minutes.

While 1
$time = @HOUR & @MIN
$txttime = @HOUR & ":" & @MIN & ":" & @SEC
Sleep(2000)
$timenew = @HOUR & @MIN
$txttimenew = @HOUR & ":" & @MIN & ":" & @SEC
If $timenew > $time + 5 OR $timenew < $time - 5 Then      ;difference in minutes
    If $timenew > $time Then
        RunWait(@ComSpec & " /c " & "time " & $txttimenew, "", @SW_HIDE)
    ElseIf $timenew < $time Then 
        RunWait(@ComSpec & " /c " & "time " & $txttime, "", @SW_HIDE)
    EndIf
    Msgbox(0,"", "warning, time changed")
EndIf
Wend

This one works but I see my messagebox appear each time the minutes are 00 (at 16:00, 17:00, 18:00 ...), even if the time has not been changed, I cant understand why.

Someone can help ?

Thanks

Link to comment
Share on other sites

Start times need to be outside the loop

$time = @HOUR & @MIN
$txttime = @HOUR & ":" & @MIN & ":" & @SEC

While 1
    Sleep(2000)
    $timenew = @HOUR & @MIN
    $txttimenew = @HOUR & ":" & @MIN & ":" & @SEC
    If $timenew > $time + 5 Or $timenew < $time - 5 Then ;difference in minutes
        If $timenew > $time + 5 Then
            RunWait(@ComSpec & " /c " & "time " & $txttimenew, "", @SW_HIDE)
        ElseIf $timenew < $time -5 Then
            RunWait(@ComSpec & " /c " & "time " & $txttime, "", @SW_HIDE)
        EndIf
        MsgBox(0, "", "warning, time changed")
    EndIf
WEnd

.... not tested

8)

EDIT Found (+5) and (-5) errors

Edited by Valuater

NEWHeader1.png

Link to comment
Share on other sites

Start times need to be outside the loop

this script must run continuously to 'save' the PC time against small hands trouble, and keep it up to date. "Dad, I'm not to bed because it's early, look at the screen watch !"

If a change is done, The script resets the PC time to which it was just before the change

I tried this

#Include <Date.au3>

While 1
$date1 = _NowCalc()
$time1 = @HOUR & ":" & @MIN & ":" & @SEC
Sleep(2000)
$date2 = _NowCalc()
If _DateDiff( 'n', $date1, $date2)>5 OR _DateDiff( 'n', $date2, $date1)>5 Then 
    RunWait(@ComSpec & " /c " & "time " & $time1, "", @SW_HIDE)
    Msgbox(0,"", "time changed")
EndIf
Wend

but it makes the PC lag a little, so maybe the use of simple macros will be lighter

Link to comment
Share on other sites

Well here I don't even allow for the 5 or 10 minutes. You just leave it run.

$sFile = @TempDir & "\time.txt"
;;;;  See Note for the next line
$sUrl = "http://www.worldtimeserver.com/current_time_in_CA-BC.aspx"
While 1
If @SEC <3 AND (@MIN = 0 OR Mod(@MIN, 5) = 0) Then
    $sSrc = BinaryToString(InetRead($sURL, 1))
    _Compare($sSrc)
EndIf
WEnd

Func _Compare($s_Src)
    Local $bValid = False, $aSet
    If $s_Src Then
        Local $aTime = StringRegExp($s_Src, "(?i)(?m:^).+\s*(\d+:\d+\s+[ap]m)(?:\v+|$).+", 1)
        If @Extended Then
            Local $sNetTime = $aTime[0]

            RunWait(@ComSpec & " /c time /t > " & $sFile, "", @SW_HIDE)
            Local $sSysTime = StringRegExpReplace(FileRead($sFile), "(?i)\s*0?([1-9]+.*?m)(?:\v+|$)", "$1", 0)
            $bTimeValid = ($sNetTime = $sSysTime)
        EndIf
        Local $aDay = StringRegExp($s_Src, "(?i)(?m:^).+\s*([a-z]{3}).*day[,\s]+([a-z]{3})[a-z\s]+(\d+),\s*(\d{4})(?:\v+|$)", 1)
        If @Extended Then
            $aDay[1] = _Date_MonthToVal($aDay[1])
            Local $sNetDay = $aDay[0] & " " & $aDay[1] & "/" & $aDay[2] & "/" & $aDay[3]
            RunWait(@ComSpec & " /c date /t > " & $sFile, "", @SW_HIDE)
            Local $sSysDay = StringStripWS(FileRead($sFile), 7)
            $bDateValid = ($sNetDay = $sSysDay)
            EndIf
        EndIf
        FileDelete($sFile)
        If $bTimeValid AND $bDateValid Then $bValid = True
        If NOT $bValid Then

            $aSet = StringRegExp($aTime[0], "(?i)(\d+):(\d+)\s+([a-z]+)", 1)
            If $aSet[2] = "PM" Then
                If $aSet[0] >= 1 Then $aSet[0] += 12
            EndIf
            _SetLocalTime(Number($aSet[0]), Number($aSet[1]))
            ;;This part is optional AND NASTY. Uncomment the lines to use it.  You could also preset a time for this to occur
            ;;MsgBox(262160,"Warning","You have 20 seconds to finish what you are doing before the computer shuts down.", 5)
            ;;$sTime = TimerInit()
            ;;If TimerDiff($sTime) >= 20000 Then Shutdown(12)
        EndIf
EndFunc

Func _Date_MonthToVal($sStr)
    If NOT StringRegExp($sStr, "(?i)^[a-z]+$", 0) Then Return
    $sStr = StringLeft($sStr, 3)

    Local $aMths[12] = ["jan", "feb", "mar", "Apr", "may", "jun", "jul", "Aug", "sep", "oct", "nov", "dec"]
    For $i = 0 To UBound($aMths) -1
        If $aMths[$i] = $sStr Then
        Return StringFormat("%02u", $i+1)
        EndIf
    Next
EndFunc

Func _SetLocalTime($Hour, $Min, $Sec = 0, $Day = 0, $Month = 0, $Year = 0);;  Thanks to LxP

    Local $Error = 0
    Local $Return = False

; Check parameters
    If $Hour < 0 Or $Hour > 23 Then $Error = 1
    If $Min < 0 Or $Min > 59 Then $Error = 1
    If $Sec < 0 Or $Sec > 59 Then $Error = 1
    If @NumParams > 3 And ($Day < 1 Or $Day > 31) Then $Error = 1
    If @NumParams > 4 And ($Month < 1 Or $Month > 12) Then $Error = 1
    If @NumParams > 5 And ($Year < 1601 Or $Year > 30827) Then $Error = 1

    If Not $Error Then
    ; Create the necessary structure
        Local $Struct = DLLStructCreate('Short;Short;Short;Short;Short;Short;Short;Short')
        Local $StructPtr = DLLStructGetPtr($Struct)
    ; Get the current date if it is not being fully set
        If @NumParams < 6 Then
            DLLCall('Kernel32', 'None', 'GetLocalTime', 'Ptr', $StructPtr)
            If @Error Then $Error = 2
        EndIf
        If Not $Error Then
        ; Write the data to the structure
            DLLStructSetData($Struct, 5, $Hour)
            DLLStructSetData($Struct, 6, $Min)
            DLLStructSetData($Struct, 7, $Sec)
            DLLStructSetData($Struct, 8, 0)
            If @NumParams > 3 Then DLLStructSetData($Struct, 4, $Day)
            If @NumParams > 4 Then DLLStructSetData($Struct, 2, $Month)
            If @NumParams > 5 Then DLLStructSetData($Struct, 1, $Year)
        ; SetLocalTime must be called twice for DST purposes
            DLLCall('Kernel32', 'None', 'SetLocalTime', 'Ptr', $StructPtr)
            Local $Result = DLLCall('Kernel32', 'Int', 'SetLocalTime', 'Ptr', $StructPtr)
            If @Error Then
                $Error = 2
            Else
                $Struct = 0
                $Return = ($Result[0] <> 0)
            EndIf
        EndIf
        $Struct = 0
        ;DLLStructDelete($Struct)
    EndIf

    SetError($Error)
    Return $Return

EndFunc

NOTE: $sURL will have to be edited unless you are in the Pacific Time Zone.

To edit the line go to

http://www.worldtimeserver.com/index.aspx

In the "Current Times" box, Enter the name of a major city in yoout time zone. That will redirect you to the proper page. Replace the value of $sURL with the URL of the page you were re-directed to.

EDIT: left a test line in the code

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Thanks a lot GEOSoft, there are many things for me to learn from your script

Is it necessary to make a complicated script for this task ? Here is the one I'm currently using, it's much more simple but it works up to now. Can I get trouble with it ?

#Include <Date.au3>
While 1
$date1 = _NowCalc()
Global $heure1 = @HOUR & ":" & @MIN & ":" & @SEC
Sleep(2000)
$date2 = _NowCalc()
If _DateDiff( 'n', $date1, $date2)>5 OR _DateDiff( 'n', $date2, $date1)>5 Then 
    _ResetTime()
    ;.....
EndIf
Wend

Func _ResetTime()
Local $strURL, $time0
$strURL = "http://www.heure.com/heures.php?timez=1"
If Ping("www.google.fr", 500)=0 Then 
    $time0 = $heure1
Else
    Local $oTime
    $oTime = ObjCreate("winhttp.winhttprequest.5.1")
    $oTime.Open("GET",$strURL)
    $oTime.Send()
    If $oTime.Status = 200 Then $text = $oTime.Responsetext 
    $oTime = 0
    $time0 = StringReplace($text, "heure|", "")
EndIf
RunWait(@ComSpec & " /c " & "time " & $time0, "", @SW_HIDE)
EndFunc
Link to comment
Share on other sites

Use whatever works best for you.

The site I used for a time check posts the time in 12 hour format and the month as alpha so I had to convert those to work properly with Windows. The whole portion for the date can be left out. I put that in to make the script easily expandable in case at some point it was decided to also check if the date had been changed as well. I have another function, _Date_DayToNum() for converting an alpha day to the numeric value, if it was ever required. I also decided there was no sense in using any of the standard _Date*() functions when all I needed was to get the system time from the command console with time /t and compare it to the time on the web site..

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Thank you for these explanations , it's very clear

I have a last question : is there a way to get the seconds with the /c time /t command ?

With this :

$sFile = @TempDir & "\time.txt"
RunWait(@ComSpec & " /c time /t > " & $sFile, "", @SW_HIDE)
$data1 = FileRead($sFile)
msgbox(0,"",$data1)

I get HH:MM but no seconds, and I could find the info nowhere

Link to comment
Share on other sites

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Thank you for these explanations , it's very clear

I have a last question : is there a way to get the seconds with the /c time /t command ?

With this :

$sFile = @TempDir & "\time.txt"
RunWait(@ComSpec & " /c time /t > " & $sFile, "", @SW_HIDE)
$data1 = FileRead($sFile)
msgbox(0,"",$data1)

I get HH:MM but no seconds, and I could find the info nowhere

Not with the /t switch, that returns HH:MM in 12 hour format (at least for me). I think it could be possble with time and no switches. You would have to read the time to a variable and then send the HH:MM back to the command because it's sitting there waiting for a response. Then you can extract seconds from the variable.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

MvGulik, I tried your way using /c time /t to get rid of the obligation of sending back the time

On my XP-SP3 system it works clearly slower than GEOsoft's way

Something like this should work

$sFile = @TempDir & "\time.txt"
While 1
RunWait(@ComSpec & " /c time /t > " & $sFile, "", @SW_HIDE)
$data1 = StringReplace(FileRead($sFile),":", "")
sleep(10000)
RunWait(@ComSpec & " /c time /t > " & $sFile, "", @SW_HIDE)
$data2 = StringReplace(FileRead($sFile),":", "")
If $data1 - $data2 > 5 OR $data2 - $data1 > 5 Then 
    RunWait(@ComSpec & " /c " & "time " & $data1, "", @SW_HIDE)
    ;....
EndIf
Wend

Except at midnight (23:59 => 00:00) :(

So... maybe NowCalc() is not so bad

Link to comment
Share on other sites

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Actually this works to get the seconds. I didn't bother with the fractions of seconds, that's a simple change in the expression. You can speed it up changing

ProcessWaitClose($hPid, 2) to ProcessWaitClose($hPid, 1). I just allowed some extra time.

$hPid = Run(@ComSpec & " /c time ", "", @SW_HIDE, 0x02)
If ProcessWaitClose($hPid, 2)
$sCurrTime = StdoutRead($hPid)
$iSec = StringRegExpReplace($sCurrTime, "(?i)(?s).+:(\d+).+", "$1")
MsgBox(0, "Result", $iSec)

Edit: BrainDead

This is better anyway.

$hPid = Run(@ComSpec & " /c time ", "", @SW_HIDE, 0x02)
ProcessWaitClose($hPid, 2)
$sTime = StdoutRead($hPid)
$aTime = StringRegExp($sTime, "(\d+)", 3)
If NOT @Error Then
    $Hour = $aTime[0]
    $Min = $aTime[1]
    $Sec = $aTime[2]
    $Sec_Hund = $aTime[3]
    MsgBox(0, "Time", "Hour: " & $Hour & @CRLF & "Minutes: " & $Min & @CRLF & _
    "Seconds: " & $Sec & @CRLF & "Hundredths: " & $Sec_Hund)
EndIf

$hPid = Run(@ComSpec & " /c date ", "", @SW_HIDE, 0x02)
ProcessWaitClose($hPid, 2)
$sDate = StringRegExpReplace(StdoutRead($hPid), "(?i)(?s).+\s(\d.+\d).+", "$1")
MsgBox(0, "Date", $sDate)

If I get a chance later I'll change the Compare function to suit this.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

GEOsoft, your script is a very elegant way to get the seconds

For the comparison (minutes only) I found this way to cheat the 23:59 to 00:00 time change

It should work don't you think so ?

$sFile = @TempDir & "\time.txt"
RunWait(@ComSpec & " /c time /t > " & $sFile, "", @SW_HIDE)
$data1 = StringReplace(FileRead($sFile),":", "")
sleep(10000)
RunWait(@ComSpec & " /c time /t > " & $sFile, "", @SW_HIDE)
$data2 = StringReplace(FileRead($sFile),":", "")

$diff = Abs($data1 - $data2)
If $diff  > 1 AND $diff  < 2359 Then ...

Thus I don't need NowCalc() & Date*() any more

Link to comment
Share on other sites

GEOsoft, your script is a very elegant way to get the seconds

For the comparison (minutes only) I found this way to cheat the 23:59 to 00:00 time change

It should work don't you think so ?

$sFile = @TempDir & "\time.txt"
RunWait(@ComSpec & " /c time /t > " & $sFile, "", @SW_HIDE)
$data1 = StringReplace(FileRead($sFile),":", "")
sleep(10000)
RunWait(@ComSpec & " /c time /t > " & $sFile, "", @SW_HIDE)
$data2 = StringReplace(FileRead($sFile),":", "")

$diff = Abs($data1 - $data2)
If $diff  > 1 AND $diff  < 2359 Then ...

Thus I don't need NowCalc() & Date*() any more

That should beat that problem. I'm using some functions that I already had in some personal UDFs and I'm not changing them so you get them the way they are.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

GEOsoft, your script is a very elegant way to get the seconds

For the comparison (minutes only) I found this way to cheat the 23:59 to 00:00 time change

It should work don't you think so ?

$sFile = @TempDir & "\time.txt"
RunWait(@ComSpec & " /c time /t > " & $sFile, "", @SW_HIDE)
$data1 = StringReplace(FileRead($sFile),":", "")
sleep(10000)
RunWait(@ComSpec & " /c time /t > " & $sFile, "", @SW_HIDE)
$data2 = StringReplace(FileRead($sFile),":", "")

$diff = Abs($data1 - $data2)
If $diff  > 1 AND $diff  < 2359 Then ...

Thus I don't need NowCalc() & Date*() any more

You should never (never, never) start another process to do the job for you if you can do it in your memory space.

That's going against the main principles of the operating system you use.

New Technology.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

whatever Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

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