Sign in to follow this  
Followers 0
2tim3_16

Trouble with Countdown clock

6 posts in this topic

#1 ·  Posted (edited)

I've written a countdown clock with a progress bar, but I'm having some trouble when it reaches the end of the countdown. Rather than setting the progress bar to 100 and changing the text to "The time has come!", it starts the countdown over at -2 days. Any suggestions? :D

; ----------------------------------------------------------------------------
;
; AutoIt Version: 3.1.0
; Author:        2tim3_16
; Date:           07/03/2006
;
; Script Function:
;   Counts down to a given date and time.
;
; ----------------------------------------------------------------------------

#include <Date.au3>
#NoTrayIcon

; ----------------------------------------------------------------------------
; Title and Subtitle:
;   Text must be in "quotes."
; ----------------------------------------------------------------------------
$Title = "The Wedding Day is Coming :)"
$SubTitle = "Total Time Remaining"

; ----------------------------------------------------------------------------
; Window Position:
;   xPos = Position from left side of the screen (in pixels)
;   yPos = position from top of the screen (in pixels)
; ----------------------------------------------------------------------------
$xPos = -1; Default setting (centered)
$yPos = -1; Default setting (centered)

; ----------------------------------------------------------------------------
; Window Options:
;   -1 = Always on top, with title (default)
;   1 = Borderless, titleless window
;   2 = Without "always on top" attribute
;   16 = Window can be moved
; ----------------------------------------------------------------------------
$Opt = 18; Without "always on top" attribute, window can be moved

; ----------------------------------------------------------------------------
; Start and End Dates:
;   Dates must be in the format YYYY/MM/DD HH:MM:SS for use in calculations.
; ----------------------------------------------------------------------------
$StartDate = "2006/01/05 13:00:00"
$EndDate = "2006/08/05 10:00:00"

; ----------------------------------------------------------------------------
; Time Calculations:
; ----------------------------------------------------------------------------
$Days = _DateDiff("D", _NowCalc(), $EndDate)                        ; Total whole days remaining
$Hours = _DateDiff("H", _NowCalc(), $EndDate)                       ; Total whole hours remaining
$Hours2 = _DateDiff("H", _DateAdd("D", $Days, _NowCalc()), $EndDate) ; Hours remaining after $Days
$Minutes = _DateDiff("N", _NowCalc(), $EndDate)                     ; Total whole minutes remaining
$Minutes2 = _DateDiff("N", _DateAdd("H", $Hours, _NowCalc()), $EndDate); Minutes remaining after $Hours
$Seconds = _DateDiff("S", _NowCalc(), $EndDate)                     ; Total seconds remaining
$Seconds2 = _DateDiff("S", _DateAdd("N", $Minutes, _NowCalc()), $EndDate); Seconds remaining after $Minutes
$Total = _DateDiff("S", $StartDate, $EndDate)                       ; Total seconds of engagement (for use in calculating $Percentage)
$Completed = _DateDiff("S", $StartDate, _NowCalc())                 ; Total seconds completed (for use in calculating $Percentage)

; ----------------------------------------------------------------------------
; Progress Bar:
; ----------------------------------------------------------------------------
ProgressOn($Title, $SubTitle, $Days & " Days, " & $Hours2 & " Hours, " & $Minutes2 & " Minutes, " & $Seconds2 & " Seconds", -1, -1, $Opt)
$Percentage = $Completed/$Total*100; Percent complete

ProgressSet($Percentage)
For $i = $Percentage to $Completed step 1/$Seconds
    sleep(1000)
        $Days = _DateDiff("D", _NowCalc(), $EndDate)
        $Hours = _DateDiff("H", _NowCalc(), $EndDate)
        $Hours2 = _DateDiff("H", _DateAdd("D", $Days, _NowCalc()), $EndDate)
        $Minutes = _DateDiff("N", _NowCalc(), $EndDate)
        $Minutes2 = _DateDiff("N", _DateAdd("H", $Hours, _NowCalc()), $EndDate)
        $Seconds = _DateDiff("S", _NowCalc(), $EndDate)
        $Seconds2 = _DateDiff("S", _DateAdd("N", $Minutes, _NowCalc()), $EndDate)
        $Total = _DateDiff("S", $StartDate, $EndDate)
        $Completed = _DateDiff("S", $StartDate, _NowCalc())
    ProgressSet( $i, $Days & " Days, " & $Hours2 & " Hours, " & $Minutes2 & " Minutes, " & $Seconds2 & " Seconds")
Next
ProgressSet(100 , $SubTitle, "The time has come!")
sleep(10000)
ProgressOff()
Edited by 2tim3_16

Share this post


Link to post
Share on other sites



Hi,

first of all. There should be an easier way to compare dates. I think take the seconds a calculate with them is better.

Maybe this gives you some ideas.

#include<Date.au3>
HotKeySet("{esc}", "end")

While 1
    ToolTip("Date" & @CRLF & "============" & @CRLF & _counter("2006/06/09", "18:00:00") & " left", _
            @DesktopWidth - 100, 30)
    Sleep(1)
WEnd

; "YYYY/MM/DD[ HH:MM:SS]"
Func _counter($s_Date, $s_time)
    If _DateIsValid($s_Date & " " & $s_time) Then
        Local $NumberOfSeconds = _DateDiff("s", _NowCalc(), $s_Date & " " & $s_time)
        Return StringFormat("Days   = %.02d" & @CRLF & "Hours   = %.02d" & @CRLF & "Min = %.02d" & @CRLF & "Sec = %.02d" & @CRLF & _
                "Msec   = %.03d", $NumberOfSeconds / 86400, Mod($NumberOfSeconds / 3600, 24), Mod(($NumberOfSeconds / 60), 60), _
                Mod($NumberOfSeconds, 60), 1000 - _MSec())
    Else
        Return -1
    EndIf
EndFunc  ;==>_counter

Func _MSec()
    Local $stSystemTime = DllStructCreate('ushort;ushort;ushort;ushort;ushort;ushort;ushort;ushort')
    DllCall('kernel32.dll', 'none', 'GetSystemTime', 'ptr', DllStructGetPtr($stSystemTime))
    $sMilliSeconds = StringFormat('%03d', DllStructGetData($stSystemTime, 8))
    $stSystemTime = 0
    Return $sMilliSeconds
EndFunc  ;==>_MSec

Func end()
    Exit (0)
EndFunc  ;==>end

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Mega,

Thanks for your reply. That gives a countdown, but I'd like to have a progress bar showing the percentage of time past, with a specific start time and end time. Some of the other guys at work really like what I've got so far, but none of them want the countdown to start over at -2 days when it completes. :D And, I'd like it so that they can easily edit the text and start and end dates to suit their particular wants/needs.

Surely this is possible?

Share this post


Link to post
Share on other sites

I got it! I added the following If statement to my loop, and the script now does exactly what I want it to.

If $Days = 0 Then
     If $Hours2 = 0 Then
          If $Minutes2 = 0 Then
               If $Seconds2 = 0 Then
                    ExitLoop
               EndIf
          EndIf
     EndIf
EndIf

Share this post


Link to post
Share on other sites

Hi,

what this line?

If $Days = 0 And $Hours2 = 0 And $Minutes2 = 0 And $Seconds2 = 0 Then ExitLoop

instead of all the others.

So long,

Mega


Scripts & functions Organize Includes Let Scite organize the include files

Yahtzee The game "Yahtzee" (Kniffel, DiceLion)

LoginWrapper Secure scripts by adding a query (authentication)

_RunOnlyOnThis UDF Make sure that a script can only be executed on ... (Windows / HD / ...)

Internet-Café Server/Client Application Open CD, Start Browser, Lock remote client, etc.

MultipleFuncsWithOneHotkey Start different funcs by hitting one hotkey different times

Share this post


Link to post
Share on other sites

Hi,

what this line?

If $Days = 0 And $Hours2 = 0 And $Minutes2 = 0 And $Seconds2 = 0 Then ExitLoop

instead of all the others.

So long,

Mega

Thanks! I guess I should have known that. :">

Share this post


Link to post
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
Sign in to follow this  
Followers 0