Jump to content

Calculations Don't work


LurchMan
 Share

Recommended Posts

Hey Everyone -

I've been pretty bored at work this week so I made a simple little app that counts down to a specified time. I've got everything working except it doesn't calculate days correctly and I can't get stringformat to work correctly.

All help will be greatly appreciated! I'm basically just using this as a way to stay sharp with scripting.

While 1
    If StringRight(GUICtrlRead($time), 2) = "PM" And StringLeft(GUICtrlRead($time), 2) > "12" Then
        $sDateTime = _DateAdd("h", 12, StringRight(GUICtrlRead($date), 4) & "/" & StringLeft(GUICtrlRead($date), 2) & "/" & StringMid(GUICtrlRead($date), 4, 2) & _
        " " & GUICtrlRead($time))
    Else
        $sDateTime = StringRight(GUICtrlRead($date), 4) & "/" & StringLeft(GUICtrlRead($date), 2) & "/" & StringMid(GUICtrlRead($date), 4, 2) & _
        " " & GUICtrlRead($time)
    EndIf
    $iTicks = _DateDiff ("s", _NowCalc(), $sDateTime) * 1000
    _TicksToTime ($iTicks, $ihour, $imin, $isec)
    If $ihour > 23 Then _AddDay ()
    StringFormat("%02i", $iday, $ihour, $imin, $isec)
    GUICtrlSetData($cnt,"Days: " & $iday & @TAB & "Hours: " & $ihour & @TAB & "Mins: " & $imin & @TAB & "Secs: " & $isec)
    If $iDay=0  And $iHour=0 And $iMin=0 And $iSec = 0 Then _Finish()
    Sleep(100)
WEnd

Func _AddDay ()
    Do
        $iday += 1
        $ihour -= 24
        If $ihour < 24 Then Return
    Until Number($ihour) < 24
EndFunc

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Link to comment
Share on other sites

It's hard to fix your script when you don't provide a reproducer.

C:\Users\Jos\Desktop\Dump 4 Nov\dayalskjdlkasd.au3(2,37) : WARNING: $time: possibly used before declaration.
    If StringRight(GUICtrlRead($time)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\Jos\Desktop\Dump 4 Nov\dayalskjdlkasd.au3(3,69) : WARNING: $date: possibly used before declaration.
        $sDateTime = _DateAdd("h", 12, StringRight(GUICtrlRead($date)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\Jos\Desktop\Dump 4 Nov\dayalskjdlkasd.au3(10,34) : WARNING: $ihour: possibly used before declaration.
    _TicksToTime ($iTicks, $ihour,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\Jos\Desktop\Dump 4 Nov\dayalskjdlkasd.au3(10,41) : WARNING: $imin: possibly used before declaration.
    _TicksToTime ($iTicks, $ihour, $imin,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\Jos\Desktop\Dump 4 Nov\dayalskjdlkasd.au3(10,48) : WARNING: $isec: possibly used before declaration.
    _TicksToTime ($iTicks, $ihour, $imin, $isec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\Jos\Desktop\Dump 4 Nov\dayalskjdlkasd.au3(12,31) : WARNING: $iday: possibly used before declaration.
    StringFormat("%02i", $iday,
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\Jos\Desktop\Dump 4 Nov\dayalskjdlkasd.au3(13,24) : WARNING: $cnt: possibly used before declaration.
    GUICtrlSetData($cnt,
~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\Jos\Desktop\Dump 4 Nov\dayalskjdlkasd.au3(2,37) : ERROR: $time: undeclared global variable.
    If StringRight(GUICtrlRead($time)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\Jos\Desktop\Dump 4 Nov\dayalskjdlkasd.au3(4,33) : ERROR: _DateAdd(): undefined function.
        " " & GUICtrlRead($time))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\Jos\Desktop\Dump 4 Nov\dayalskjdlkasd.au3(9,40) : ERROR: _NowCalc(): undefined function.
    $iTicks = _DateDiff ("s", _NowCalc()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\Jos\Desktop\Dump 4 Nov\dayalskjdlkasd.au3(9,53) : ERROR: _DateDiff(): undefined function.
    $iTicks = _DateDiff ("s", _NowCalc(), $sDateTime)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\Jos\Desktop\Dump 4 Nov\dayalskjdlkasd.au3(10,48) : ERROR: _TicksToTime(): undefined function.
    _TicksToTime ($iTicks, $ihour, $imin, $isec)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\Jos\Desktop\Dump 4 Nov\dayalskjdlkasd.au3(14,69) : ERROR: _Finish(): undefined function.
    If $iDay=0  And $iHour=0 And $iMin=0 And $iSec = 0 Then _Finish()
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
C:\Users\Jos\Desktop\Dump 4 Nov\dayalskjdlkasd.au3 - 6 error(s), 7 warning(s)
!>16:15:20 AU3Check ended.rc:2
Edited by Manadar
Link to comment
Share on other sites

#AutoIt3Wrapper_Run_Debug_Mode=n
#include <Date.au3>
#include <DateTimeConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
Opt("GUIOnEventMode", 1)

Global $ihour, $imin, $isec
Global $iday = 0

$LastDate = RegRead ("HKCU\Software\Countdown", "Date")
$LastTime = RegRead ("HKCU\Software\Countdown", "Time")
$LastX = RegRead ("HKCU\Software\Countdown", "X")
$LastY = RegRead ("HKCU\Software\Countdown", "Y")

If $LastDate = "" Then $LastDate = _NowCalc ()
If $LastTime = "" Then $LastTime = _NowCalc ()
If $LastX = "" Then $LastX = -1
If $LastY = "" Then $LastY = -1

#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Countdown", 600, 44, 150, 130)
GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close")
$cnt = GUICtrlCreateLabel("", 40, 14, 350, 17)
$date = GUICtrlCreateDate($LastDate, 350, 8, 90, 21, $WS_TABSTOP)
$time = GUICtrlCreateDate($LastTime, 450, 8, 90, 21, BitOR($DTS_UPDOWN,$DTS_TIMEFORMAT,$WS_TABSTOP))
WinMove ("Countdown", "", $LastX, $Lasty)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    If StringRight(GUICtrlRead($time), 2) = "PM" And StringLeft(GUICtrlRead($time), 2) > "12" Then
        $sDateTime = _DateAdd("h", 12, StringRight(GUICtrlRead($date), 4) & "/" & StringLeft(GUICtrlRead($date), 2) & "/" & StringMid(GUICtrlRead($date), 4, 2) & _
        " " & GUICtrlRead($time))
    Else
        $sDateTime = StringRight(GUICtrlRead($date), 4) & "/" & StringLeft(GUICtrlRead($date), 2) & "/" & StringMid(GUICtrlRead($date), 4, 2) & _
        " " & GUICtrlRead($time)
    EndIf
    $iTicks = _DateDiff ("s", _NowCalc(), $sDateTime) * 1000
    _TicksToTime ($iTicks, $ihour, $imin, $isec)
    If $ihour > 23 Then _AddDay ()
    StringFormat("%02i", $iday, $ihour, $imin, $isec)
    GUICtrlSetData($cnt,"Days: " & $iday & @TAB & "Hours: " & $ihour & @TAB & "Mins: " & $imin & @TAB & "Secs: " & $isec)
    If $iDay=0  And $iHour=0 And $iMin=0 And $iSec = 0 Then _Finish()
    Sleep(100)
WEnd

Func _AddDay ()
    Do
        $iday += 1
        $ihour -= 24
        If $ihour < 24 Then Return
    Until Number($ihour) < 24
EndFunc

Func _Finish ()
    ToolTip("Countdown Complete!", $LastX, $LastY, "", 1, 5)
    Sleep(5000)
    ToolTip("")
    Exit
EndFunc

Func Form1Close()
    $array = WinGetPos("Countdown")
    RegWrite ("HKCU\Software\Countdown", "X", "Reg_SZ",$array[0])
    RegWrite ("HKCU\Software\Countdown", "Y", "Reg_SZ",$array[1])
    RegWrite ("HKCU\Software\Countdown", "Time", "Reg_SZ", GUICtrlRead($time))
    RegWrite ("HKCU\Software\Countdown", "Date", "Reg_SZ", GUICtrlRead($date))
    Exit
EndFunc

^^There's the whole script. I put in just the section that I was having issues with.

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Link to comment
Share on other sites

Usually when people provide a problem reproducer, they simplify the problem. So that it has no GUI anymore, no registry writing, etc. But no matter, I wrote a reproducer myself (effort D:) and saw that you forgot to reset $iDay.

While 1
    If StringRight(GUICtrlRead($time), 2) = "PM" And StringLeft(GUICtrlRead($time), 2) > "12" Then
        $sDateTime = _DateAdd("h", 12, StringRight(GUICtrlRead($date), 4) & "/" & StringLeft(GUICtrlRead($date), 2) & "/" & StringMid(GUICtrlRead($date), 4, 2) & _
        " " & GUICtrlRead($time))
    Else
        $sDateTime = StringRight(GUICtrlRead($date), 4) & "/" & StringLeft(GUICtrlRead($date), 2) & "/" & StringMid(GUICtrlRead($date), 4, 2) & _
        " " & GUICtrlRead($time)
    EndIf
    $iTicks = _DateDiff ("s", _NowCalc(), $sDateTime) * 1000
    _TicksToTime ($iTicks, $ihour, $imin, $isec)
    $iDay = 0 ; <=============== HERE
    If $ihour > 23 Then _AddDay ()
    StringFormat("%02i", $iday, $ihour, $imin, $isec)
    GUICtrlSetData($cnt,"Days: " & $iday & @TAB & "Hours: " & $ihour & @TAB & "Mins: " & $imin & @TAB & "Secs: " & $isec)
    If $iDay=0  And $iHour=0 And $iMin=0 And $iSec = 0 Then _Finish()
    Sleep(100)
WEnd
Link to comment
Share on other sites

Thank you - I always miss the small things

Now the other issue - Why doesn't this work:

StringFormat("%02i", $iday, $ihour, $imin, $isec)

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Link to comment
Share on other sites

Now the other issue - Why doesn't this work:

StringFormat("%02i", $iday, $ihour, $imin, $isec)
Despite being bored, you didn't seem to have time to look up StringFormat() in the help file. You need a format specification for each of those variables you want to pass in:
Global $iday = 1, $ihour = 2, $imin = 3, $isec = 4.5, $sFormat

$sFormat = StringFormat("%02i %02i:%02i:%02i", $iday, $ihour, $imin, $isec)
ConsoleWrite("$sFormat = " & $sFormat & @LF)

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...