Sign in to follow this  
Followers 0
Kovacic

Simple timer logic issue

11 posts in this topic

#1 ·  Posted (edited)

Hi all! I am perplexed by something that should be simple. I am trying to make a small timer which will become part of a larger script, but I'm stuck on comparing the 2 times..

When you run the script, you hit OK on a message box, that's when the current system time and day of the year is saved to a variable.. when you hit OK again, the timer is stopped and the date and time are compared. I'm not using Timerinit() because I want the exact system time the timer started, and the exact system time the timer stopped. Then the time is compared and you get the difference.

This is easy as long as I am subtracting 10 seconds from 15 seconds, but what if the start timeer got second: 45 and the top timer got second: 12?

I'm sure this is cakewalk for anyone that got higher than a C in math, but I didnt. 

Can anyone shed some light on this for a simpleton like myself?

Msgbox(0,"START","Click to start timer")
$a = timestamp()


Msgbox(0,"START","Click to stop timer")
$b = timestamp()


compare($a,$b)


Func timestamp()
    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 @YDay & ':' & @Hour & ':' & @Min & ':' & @Sec & ':' & $sMilliSeconds
EndFunc


Func compare($start,$stop)
$Comp_a = StringSplit($start,":",2) 
$Comp_b = StringSplit($stop,":",2)
$DayDif  = $comp_b[0] - $comp_a[0]
$HourDif = $comp_b[1] - $comp_a[1]
$MinDif  = $comp_b[2] - $comp_a[2]
$SecDif  = $comp_b[3] - $comp_a[3]
If $comp_b[4] > $comp_a[4] then
$MSecDif = $comp_b[4] - $comp_a[4]
else
$MSecDif = $comp_a[4] - $comp_b[4]
endif

Msgbox(0,"hey you, yeah you!", "Elapse Time:" & @CRLF & "Days: " & $DayDif & @CRLF & "Hours: " & $HourDif & @CRLF & "Minutes: " & $MinDif & @CRLF & "Seconds: " & $SecDif & @CRLF & "Miliseconds: " & $MSecDif  )


endfunc

Thanks in advance!!

 

EDIT:

Would this logic work?

;example

$start = 45
$stop = 12
 
$val1 = $Start - 60 ; val1 becomes 15
$Mydiff =  $stop + $val1
Edited by Kovacic

C0d3 is P0etry( ͡° ͜ʖ ͡°)

Share this post


Link to post
Share on other sites



Kovacic,

Why not let AutoIt do all the work for you? ;)

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <Date.au3>

$hGUI = GUICreate("Test", 500, 500)

$cStart = GUICtrlCreateButton("Start", 10, 10, 80, 30)
$cStop = GUICtrlCreateButton("Stop", 10, 50, 80, 30)
GUICtrlSetState($cStop, $GUI_DISABLE)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cStart
            $sStart = _NowCalc()
            GUICtrlSetState($cStart, $GUI_DISABLE)
            GUICtrlSetState($cStop, $GUI_ENABLE)
        Case $cStop
            $sStop = _NowCalc()
            GUICtrlSetState($cStop, $GUI_DISABLE)
            GUICtrlSetState($cStart, $GUI_ENABLE)

            $iDays = _DateDiff("D", $sStart, $sStop)
            $sStart = _DateAdd("D", $iDays, $sStart)
            $iHours = _DateDiff("h", $sStart, $sStop)
            $sStart = _DateAdd("h", $iHours, $sStart)
            $iMins = _DateDiff("n", $sStart, $sStop)
            ConsoleWrite($sStart & @CRLF)
            $sStart = _DateAdd("n", $iMins, $sStart)
            ConsoleWrite($sStart & @CRLF)
            $iSecs = _DateDiff("s", $sStart, $sStop)

            MsgBox($MB_SYSTEMMODAL, "Time Diff:", $iDays & " days, " & $iHours & ":" & $iMins & ":" & $iSecs)

    EndSwitch

WEnd
Much easier! :)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

Kovacic,

Why not let AutoIt do all the work for you? ;)

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <Date.au3>

$hGUI = GUICreate("Test", 500, 500)

$cStart = GUICtrlCreateButton("Start", 10, 10, 80, 30)
$cStop = GUICtrlCreateButton("Stop", 10, 50, 80, 30)
GUICtrlSetState($cStop, $GUI_DISABLE)

GUISetState()

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cStart
            $sStart = _NowCalc()
            GUICtrlSetState($cStart, $GUI_DISABLE)
            GUICtrlSetState($cStop, $GUI_ENABLE)
        Case $cStop
            $sStop = _NowCalc()
            GUICtrlSetState($cStop, $GUI_DISABLE)
            GUICtrlSetState($cStart, $GUI_ENABLE)

            $iDays = _DateDiff("D", $sStart, $sStop)
            $sStart = _DateAdd("D", $iDays, $sStart)
            $iHours = _DateDiff("h", $sStart, $sStop)
            $sStart = _DateAdd("h", $iHours, $sStart)
            $iMins = _DateDiff("n", $sStart, $sStop)
            ConsoleWrite($sStart & @CRLF)
            $sStart = _DateAdd("n", $iMins, $sStart)
            ConsoleWrite($sStart & @CRLF)
            $iSecs = _DateDiff("s", $sStart, $sStop)

            MsgBox($MB_SYSTEMMODAL, "Time Diff:", $iDays & " days, " & $iHours & ":" & $iMins & ":" & $iSecs)

    EndSwitch

WEnd
Much easier! :)

M23

 

 

That was what I planned originally but I need to use milliseconds, but it looks like _DateDiff only handles as small as seconds.. Good call though

Edited by Kovacic

C0d3 is P0etry( ͡° ͜ʖ ͡°)

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Kovacic,

Then we have to be creative: :)

Case $cStart
    $sStart = _NowCalc()
    $iStartMS = @MSEC
    ConsoleWrite($sStart & "." & $iStartMS & @CRLF)
    
    GUICtrlSetState($cStart, $GUI_DISABLE)
    GUICtrlSetState($cStop, $GUI_ENABLE)
Case $cStop
    $sStop = _NowCalc()
    $iStopMS = @MSEC
    ConsoleWrite($sStop & "." & $iStopMS & @CRLF)
    
    GUICtrlSetState($cStop, $GUI_DISABLE)
    GUICtrlSetState($cStart, $GUI_ENABLE)
    
    $iDays = _DateDiff("D", $sStart, $sStop)
    $sStart = _DateAdd("D", $iDays, $sStart)
    $iHours = _DateDiff("h", $sStart, $sStop)
    $sStart = _DateAdd("h", $iHours, $sStart)
    $iMins = _DateDiff("n", $sStart, $sStop)
    $sStart = _DateAdd("n", $iMins, $sStart)
    $iSecs = _DateDiff("s", $sStart, $sStop)

    ; See if Secs values are the same
    $iMSecs = $iStopMs - $iStartMS
    If StringRight($sStart, 2) <> StringRight($sStop, 2) Then
        If $iStartMS > $iStopMs Then
            $iMSecs = 1000 - $iStartMS + $iStopMS
            $iSecs -= 1
        EndIf
    EndIf

    MsgBox($MB_SYSTEMMODAL, "Time Diff:", $iDays & " days, " & $iHours & ":" & $iMins & ":" & $iSecs & "." & $iMSecs)
Checking the calculated time against the values written in the console it seems to be correct - over to you for more testing. :)

M23

Edit: Not quite - still working on it. :(

2nd Edit: Try now. :)

Edited by Melba23

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

Kovacic,

Then we have to be creative: :)

Case $cStart
    $sStart = _NowCalc()
    $iStartMS = @MSEC
    ConsoleWrite($sStart & "." & $iStartMS & @CRLF)
    
    GUICtrlSetState($cStart, $GUI_DISABLE)
    GUICtrlSetState($cStop, $GUI_ENABLE)
Case $cStop
    $sStop = _NowCalc()
    $iStopMS = @MSEC
    ConsoleWrite($sStop & "." & $iStopMS & @CRLF)
    
    GUICtrlSetState($cStop, $GUI_DISABLE)
    GUICtrlSetState($cStart, $GUI_ENABLE)
    
    $iDays = _DateDiff("D", $sStart, $sStop)
    $sStart = _DateAdd("D", $iDays, $sStart)
    $iHours = _DateDiff("h", $sStart, $sStop)
    $sStart = _DateAdd("h", $iHours, $sStart)
    $iMins = _DateDiff("n", $sStart, $sStop)
    $sStart = _DateAdd("n", $iMins, $sStart)
    $iSecs = _DateDiff("s", $sStart, $sStop)

    ; See if Secs values are the same
    $iMSecs = $iStopMs - $iStartMS
    If StringRight($sStart, 2) <> StringRight($sStop, 2) Then
        If $iStartMS > $iStopMs Then
            $iMSecs = 1000 - $iStartMS + $iStopMS
            $iSecs -= 1
        EndIf
    EndIf

    MsgBox($MB_SYSTEMMODAL, "Time Diff:", $iDays & " days, " & $iHours & ":" & $iMins & ":" & $iSecs & "." & $iMSecs)
Checking the calculated time against the values written in the console it seems to be correct - over to you for more testing. :)

M23

Edit: Not quite - still working on it. :(

2nd Edit: Try now. :)

 

 

Looking much better, im looking at the Ms now, heres what I got while testing:

Start: 2014/07/03 12:21:54.200
Stop: 2014/07/03 12:21:55.879
 
 
 
Diff reported by script:  Seconds: 1   Msec: 1679
 
 
Actual diff: Seconds: 1 Msec: 679
 
I think my brain hurts lol
 
EDIT:
 
one of our calculations is close... im about to build a counting GUI to test the theory:
 
If $stop[1] >= $Start[1] then
$HourDif = $stop[1] - $Start[1]
else
$HourDif =  $Start[1] + ($stop[1] - 60)
endif
Edited by Kovacic

C0d3 is P0etry( ͡° ͜ʖ ͡°)

Share this post


Link to post
Share on other sites

I find that when comparing times it's much easier to convert both times to msec's subtract one from the other, and then turn that result into hh:mm:ss:ms rather than trying to do the math on the raw data.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

I find that when comparing times it's much easier to convert both times to msec's subtract one from the other, and then turn that result into hh:mm:ss:ms rather than trying to do the math on the raw data.

 

that was one of my earlier thoughts, however I will need to grab the current time and day, then a later time and day, and compare the two. The only way I can break that down to Msec is if I grab the current MS since the beginning of the year and calculate since then.


C0d3 is P0etry( ͡° ͜ʖ ͡°)

Share this post


Link to post
Share on other sites

Kovacic,

A couple of glasses of good red wine later - this might be the solution (with a big shout to BrewManNH for his suggestion above): ;)

Case $cStop
    $sStop = _NowCalc()
    $iStopMS = @MSEC
    ConsoleWrite($sStop & "." & $iStopMS & @CRLF)

    GUICtrlSetState($cStop, $GUI_DISABLE)
    GUICtrlSetState($cStart, $GUI_ENABLE)

    $iStartMs =  (Number(StringRight($sStart, 2)) * 1000) + $iStartMS
    $iStopMs = (Number(StringRight($sStop, 2)) * 1000) + $iStopMS
    $iDiffMs = $iStopMs - $iStartMS

    $iDays = _DateDiff("D", $sStart, $sStop)
    $sStart = _DateAdd("D", $iDays, $sStart)
    $iHours = _DateDiff("h", $sStart, $sStop)
    $sStart = _DateAdd("h", $iHours, $sStart)
    $iMins = _DateDiff("n", $sStart, $sStop)
    $sStart = _DateAdd("n", $iMins, $sStart)
    $iSecs = Int($iDiffMs / 1000)
    $iMSecs = $iDiffMs - ($iSecs * 1000)

    ConsoleWrite($iSecs & "." & $iMSecs & @CRLF)
I cannot break it after a good few tests - your turn. :D

M23

1 person likes this

Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

also this could do

MsgBox(0, "START", "Click to start timer")
$a = timestamp()

MsgBox(0, "STOP", "Click to stop timer")
$b = timestamp()

compare($a, $b)

Func timestamp()
    Return @YDAY & ':' & @HOUR & ':' & @MIN & ':' & @SEC & ':' & @MSEC
EndFunc   ;==>timestamp


Func compare($start, $stop)
    $Comp_a = StringSplit($start, ":", 2)
    $Comp_b = StringSplit($stop, ":", 2)
    Local $comp_c[5]

    ; start
    $Comp_a[0] = $Comp_a[0] * 86400000 ; Day
    $Comp_a[1] = $Comp_a[1] * 3600000 ; Hour
    $Comp_a[2] = $Comp_a[2] * 60000 ; Mins
    $Comp_a[3] = $Comp_a[3] * 1000 ; Sec

    ; stop
    $Comp_b[0] = $Comp_b[0] * 86400000 ; Day
    $Comp_b[1] = $Comp_b[1] * 3600000 ; Hour
    $Comp_b[2] = $Comp_b[2] * 60000 ; Mins
    $Comp_b[3] = $Comp_b[3] * 1000 ; Sec

    Local $started = $Comp_a[0] + $Comp_a[1] + $Comp_a[2] + $Comp_a[3] + $Comp_a[4]
    Local $stopped = $Comp_b[0] + $Comp_b[1] + $Comp_b[2] + $Comp_b[3] + $Comp_b[4]

    Local $elapsed = $stopped - $started ; elapsed time in milliseconds

    ; rebuild "days/time"
    $comp_c[0] = Int($elapsed / 86400000) ; days
    $elapsed -= $comp_c[0] * 86400000
    $comp_c[1] = Int($elapsed / 3600000) ; hours
    $elapsed -= $comp_c[1] * 3600000
    $comp_c[2] = Int($elapsed / 60000) ; mins
    $elapsed -= $comp_c[2] * 60000
    $comp_c[3] = Int($elapsed / 1000) ; secs
    $elapsed -= $comp_c[3] * 1000
    $comp_c[4] = $elapsed ; milliseconds

    Local $duration = $comp_c[0] & ":" & StringFormat("%02s", $comp_c[1]) & ":" & StringFormat("%02s", $comp_c[2]) & ":" & StringFormat("%02s", $comp_c[3]) & ":" & StringFormat("%03s", $comp_c[4])

    MsgBox(0, $duration, "Started at " & $start & @CRLF & "Stopped at " & $stop & @CRLF & "Elapse Time : " & @CRLF & " Days : " & $comp_c[0] & @CRLF & " Hours : " & $comp_c[1] & @CRLF & " Minutes : " & $comp_c[2] & @CRLF & " Seconds : " & $comp_c[3] & @CRLF & " Miliseconds : " & $comp_c[4])
EndFunc   ;==>compare

p.s.

is there a glass of wine for me too? :)


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Between those two answers, I think that solved it!! Thanks!!!!!!

WINE (or beer) FOR ALL!

Edited by Kovacic

C0d3 is P0etry( ͡° ͜ʖ ͡°)

Share this post


Link to post
Share on other sites

a cold beer for me, thanks :P


small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

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

  • Similar Content

    • TotalAuto
      By TotalAuto
      Howdy All
      I've gone around and around in circles wasting hours trying to fix this problem to no avail so I thought I would try posting for help given there are some real smart people in this forum.
      Background
      I have 5 separate .au3 scripts that all do different basic tasks. For example:
      1.au3 = opens Chrome browser to obtain information from a specific website then closes
      2.au3 = opens an Excel workbook to run a macro then saves and closes
      3.au3 = Open two seperate workbooks and transfers data from one to the other then saves and closes
      4.au3 = .......etc
      5.au3 = .......etc
      These 5 separate scripts, doing completely different tasks, need to each run once per day at 5 different designated times within a day and then close. For example:
      1.au3 = 10am
      2.au3 = 12pm
      3.au3 = 2pm
      4.au3 = 4pm
      5.au3 = 5pm
      Problem
      I can not get these different scripts to reliably run at their designated times. When I manually run these scripts individually by using 'Go F5' they work perfectly. If I convert these scripts to an .exe and manually run each one they work about 98% of time.
      How can I schedule these different .au3 files or associated .exe files to reliably run each day at different times?
      Attempts To Fix The Issue
      1. I have tried converting each .au3 script into their own .exe file and use Window's Task Scheduler to run these .exe files at their designated time but the success rate  of running each .exe in full, to completion, without error is very low at about 30%. Things that commonly go wrong include a browser not opening but movement still continues on the desk top. Other times things just do not happen at all.
      2. I have Windows 7 64 bit and have tried creating and running both (x86) and (x64) .exe files but again reliability problems exist;
      3. I have also tried moving each .exe file to the My Documents folder as I read someone that keeping your files on your desktop ties your files to individual Windows user profiles which can cause AutoIt operating errors. Not sure if this is correct but thought I would try it anyway. It did not fix my issue.
      4. I installed a program called AlwaysUp which supposedly always reliably runs in the background and will reliably execute your AutoIt script or .exe files. Still have significant reliability issues.
      Therefore, can someone please provide advice on how I can get my 5 different .au3 files to run at 5 designated times through the day, every day, without fail? Overtime I will be creating more and more different .au3 files to run certain tasks throughout the day and do not want to create one large AutoIt file that continually runs and attempts to do all the different tasks itself.
      Any help is very appreciated.
      Thanks
      Dan
       
       
    • jannikrendl
      By jannikrendl
      Hey guys,
      I would like to create a simple Timer which starts counting down if a key is pressed and after running out of time making a sound. Afterwards it should reset and wait again for the same key to be pressed to start again.
       
      I am new to autoit and really don't know how to get started...
      How I would try to do it:
      while(1) WEnd HotKeySet("{F9}", "_Timer") Func _Timer() ;FUNCTION HERE but I don't know what to do there :& EndFunc Best regards and thanks in advance
       
      JR
    • argumentum
      By argumentum
      I wanted a timer, could not find what I wanted and wrote this. Posted for the next lazy one, like me, copy'n'paste kind of ppl.
      And added an "Alarm", ( at times I need it to trigger at a time and not, in so many minutes )
      Also added an "insist/replay" ( since I'm coding, might as well have the feature )

      #NoTrayIcon #include <TrayConstants.au3> #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Opt("MustDeclareVars", 1) ; removes the chance for misspelled variables causing bugs. Opt("TrayMenuMode", 3) ; The default tray menu items will not be shown and items are not checked when selected. These are options 1 and 2 for TrayMenuMode. Opt("TrayOnEventMode", 1) ; Enable TrayOnEventMode. Opt("GUIOnEventMode", 1) ; OnEvent functions notifications Global $seconds = 0, $g_sec, $g_min, $g_hr, $started = False, $sec = 99, $aTimeHold[3], $g_IsAlarm = 0, $iAppTitle, $sAppTitle = timerTitle() $iAppTitle = @extended #Region ### START Koda GUI section ### Form=\\WDMYCLOUDEX4100\Public\[FMIS]\au3_Shared_projects\timerThing.kxf Global $Form1 = GUICreate($sAppTitle, 245, 74, @DesktopWidth - 275 - (40 * $iAppTitle), 20 + (40 * $iAppTitle), -1, BitOR($WS_EX_TOPMOST, $WS_EX_WINDOWEDGE)) Global $idHour = GUICtrlCreateInput("00", 5, 4, 49, 32, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER)) Global $idMin = GUICtrlCreateInput("00", 58, 4, 49, 32, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER)) Global $idSec = GUICtrlCreateInput("00", 110, 4, 49, 32, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER)) Global $idStart = GUICtrlCreateButton("Start Timer", 163, 6, 75, 61, $BS_MULTILINE) Global $idTitle = GUICtrlCreateInput("", 4, 40, 129, 28, BitOR($GUI_SS_DEFAULT_INPUT, $ES_CENTER, $ES_WANTRETURN)) Global $idAlarm = GUICtrlCreateCheckbox("", 137, 53, 17, 19, BitOR($GUI_SS_DEFAULT_CHECKBOX, $BS_RIGHTBUTTON)) Global $idInsist = GUICtrlCreateCheckbox("", 137, 37, 17, 19, BitOR($GUI_SS_DEFAULT_CHECKBOX, $BS_RIGHTBUTTON)) GUISetState(@SW_SHOW) #EndRegion ### END Koda GUI section ### GUICtrlSetFont($idHour, 14, 800) GUICtrlSetFont($idMin, 14, 800) GUICtrlSetFont($idSec, 14, 800) GUICtrlSetFont($idStart, 12, 800) GUICtrlSetFont($idTitle, 12, 800) GUISetOnEvent($GUI_EVENT_CLOSE, "Form1Close") GUICtrlSetOnEvent($idStart, "idStartClick") GUICtrlSetOnEvent($idTitle, "idTitleChange") GUICtrlSetOnEvent($idAlarm, "idAlarmEvent") TraySetOnEvent($TRAY_EVENT_PRIMARYDOUBLE, "OnTray_PRIMARYDOUBLE") GUICtrlSetTip($idStart, 'press ENTER or click' & @CR & @CR & 'once started, on close,' & @CR & 'will move to the tray area.') GUICtrlSetTip($idHour, 'Hours' & @CR & @CR & 'enter the hours' & @CR & 'and press ENTER') GUICtrlSetTip($idMin, 'Minutes' & @CR & @CR & 'enter the minutes' & @CR & 'and press ENTER') GUICtrlSetTip($idSec, 'Seconds' & @CR & @CR & 'enter the seconds' & @CR & 'and press ENTER') GUICtrlSetTip($idTitle, 'Type a title for this' & @CR & 'timer and press ENTER') GUICtrlSetTip($idAlarm, 'Swap between' & @CR & 'Timer and Alarm' & @CR & 'functionality') GUICtrlSetTip($idInsist, 'Replay the alert' & @CR & 'every 10 sec.' & @CR & 'for 10 min.') ;~ If Not FileGetSize("timerThing.wav") Then FileInstall("timerThing.wav", "timerThing.wav") ; if you wanna include your WAV file Global $hForm1 = WinGetHandle($Form1) Global $idDummy = GUICtrlCreateDummy() Global $aAccelKeys[1][2] = [["{Enter}", $idDummy]] GUISetAccelerators($aAccelKeys) GUICtrlSetOnEvent($idDummy, "On_idDummy") While 1 Sleep(50) If Not $started Then ContinueLoop If $sec = @SEC Then ContinueLoop $sec = @SEC Countdown() WEnd Func Form1Close() If $started Then TraySetState(1) WinSetState($hForm1, "", @SW_HIDE) Else GUIDelete() Exit EndIf EndFunc ;==>Form1Close Func Countdown() ; mod. from https://www.autoitscript.com/forum/topic/135227-simple-counter/?do=findComment&comment=950305 If $g_IsAlarm Then If Int(GUICtrlRead($idHour)) = Int(@HOUR) And Int(GUICtrlRead($idMin)) = Int(@MIN) And Int(GUICtrlRead($idSec)) = Int(@SEC) Then $seconds = -1 Else Return EndIf EndIf $g_sec = Mod($seconds, 60) $g_min = Mod($seconds / 60, 60) $g_hr = Floor($seconds / 60 ^ 2) If $seconds < -3 Then Return TrayToolTip() $seconds -= 1 If $seconds = -2 Then GUICtrlSetState($idInsist, $GUI_DISABLE) If GUICtrlRead($idInsist) = 1 Then $seconds = -5 InsistPlayTheWAV() AdlibRegister("InsistPlayTheWAV", 10000) Else playTheWAV() EndIf Return EndIf If $seconds < -1 Then Return GUICtrlSetData($idHour, StringFormat("%02i", $g_hr)) GUICtrlSetData($idMin, StringFormat("%02i", $g_min)) GUICtrlSetData($idSec, StringFormat("%02i", $g_sec)) EndFunc ;==>Countdown Func playTheWAV() OnTray_PRIMARYDOUBLE() WinActivate($hForm1) If Not FileGetSize(@ScriptDir & "\timerThing.wav") Then SoundSetWaveVolume(25) SoundPlay(@WindowsDir & "\media\tada.wav") Else SoundSetWaveVolume(5) SoundPlay(@ScriptDir & "\timerThing.wav") EndIf EndFunc ;==>playTheWAV Func InsistPlayTheWAV() Local Static $t = 0 If $t = 0 Then $t = TimerInit() playTheWAV() If TimerDiff($t) > 600000 Then AdlibUnRegister("InsistPlayTheWAV") $t = 0 EndIf EndFunc ;==>InsistPlayTheWAV Func TrayToolTip() Local $title = $sAppTitle If GUICtrlRead($idTitle) Then $title = GUICtrlRead($idTitle) TraySetToolTip('' & _ $title & @CR & _ GUICtrlRead($idHour) & ":" & GUICtrlRead($idMin) & ":" & GUICtrlRead($idSec) & @CR & @CR & _ 'Double' & @CR & _ 'Click to' & @CR & _ 'restore' & @CR & @CR & ' ') EndFunc ;==>TrayToolTip Func idStartClick() Local $title = $sAppTitle If GUICtrlRead($idTitle) Then $title = GUICtrlRead($idTitle) WinSetTitle($hForm1, "", $title) $sec = 99 $started = Not $started Switch $started Case True GUICtrlSetData($idStart, StringReplace(GUICtrlRead($idStart), "Start", "STOP")) $seconds = (Int(GUICtrlRead($idHour)) * 60 * 60) + (Int(GUICtrlRead($idMin)) * 60) + Int(GUICtrlRead($idSec)) $aTimeHold[0] = StringFormat("%02i", GUICtrlRead($idHour)) $aTimeHold[1] = StringFormat("%02i", GUICtrlRead($idMin)) $aTimeHold[2] = StringFormat("%02i", GUICtrlRead($idSec)) GUICtrlSetState($idStart, $GUI_FOCUS) GUICtrlSetState($idHour, $GUI_DISABLE) GUICtrlSetState($idMin, $GUI_DISABLE) GUICtrlSetState($idSec, $GUI_DISABLE) GUICtrlSetState($idAlarm, $GUI_DISABLE) Case Else GUICtrlSetData($idStart, StringReplace(GUICtrlRead($idStart), "STOP", "Start")) GUICtrlSetData($idHour, $aTimeHold[0]) GUICtrlSetData($idMin, $aTimeHold[1]) GUICtrlSetData($idSec, $aTimeHold[2]) GUICtrlSetState($idHour, $GUI_ENABLE) GUICtrlSetState($idMin, $GUI_ENABLE) GUICtrlSetState($idSec, $GUI_ENABLE) GUICtrlSetState($idAlarm, $GUI_ENABLE) GUICtrlSetState($idInsist, $GUI_ENABLE) GUICtrlSetState($idStart, $GUI_FOCUS) EndSwitch EndFunc ;==>idStartClick Func On_idDummy() Local $i, $s = ControlGetFocus($hForm1) $i = Int(StringRight($s, 1)) If $s = "Button1" Then Return ControlClick($hForm1, "", "Button1") If StringInStr($s, "Edit") Then Switch $i Case 1, 2, 3 Send("{Tab}") EndSwitch EndIf EndFunc ;==>On_idDummy Func idTitleChange() Local $title = $sAppTitle If GUICtrlRead($idTitle) Then $title = GUICtrlRead($idTitle) WinSetTitle($hForm1, "", $title) EndFunc ;==>idTitleChange Func timerTitle() Local $n, $i = 0 For $n = 1 To 1000 If Not WinExists(" Timer thing [" & $n & "]") Then AutoItWinSetTitle(" Timer thing [" & $n & "]") $i = $n - 1 If 40 * $n > @DesktopHeight - 150 Then $i = 1 Return SetError(0, $i, " Timer " & $n) EndIf Next Return SetError(1, $i, " Timer ???") EndFunc ;==>timerTitle Func OnTray_PRIMARYDOUBLE() TraySetState(2) WinSetState($hForm1, "", @SW_SHOW) EndFunc ;==>OnTray_PRIMARYDOUBLE Func idAlarmEvent() ConsoleWrite('+ Func idAlarmEvent() : ' & GUICtrlRead($idAlarm) & @CRLF) If GUICtrlRead($idAlarm) = 1 Then $g_IsAlarm = 1 GUICtrlSetData($idStart, StringReplace(GUICtrlRead($idStart), "Timer", "Alarm")) $sAppTitle = StringReplace($sAppTitle, "Timer", "Alarm") Else $g_IsAlarm = 0 GUICtrlSetData($idStart, StringReplace(GUICtrlRead($idStart), "Alarm", "Timer")) $sAppTitle = StringReplace($sAppTitle, "Alarm", "Timer") EndIf idTitleChange() EndFunc ;==>idAlarmEvent  
    • argumentum
      By argumentum
      I was looking for something like this, in the forum and my HDD. Bad hair day. Anyway,
      Here is the code
      If Not StringInStr($CmdLineRaw, "/ErrorStdOut") Then Exit MsgBox(262144, @ScriptName, "...please run this example from editor.", 3) #include <Date.au3>; for _EstimatedTime() #include <InetConstants.au3>; for _EstimatedTime_Example_2() ;=============================================================================== ; ; Function Name: _EstimatedTime() ; Description: calculate estimated time of completion ; Parameter(s): $a - holds the data ; $iCurrentCount - Current count ; $iTotalCount - Total count ; Requirement(s): #include <Date.au3> ; Return Value(s): false on incomplete data for assessment ; Author(s): argumentum ; Note(s): read the comments ; ;=============================================================================== Func _EstimatedTime(ByRef $a, $iCurrentCount = "", $iTotalCount = "") If $iCurrentCount & $iTotalCount = "" Then ; initialize $a = "" Dim $a[12] $a[5] = TimerInit() ; handle for TimerDiff() $a[0] = "00:00:00" $a[1] = "00:00:00" $a[2] = _NowCalc() ; starting date and time $a[3] = "" $a[4] = "0" Else If $iTotalCount = 0 Then Return False If $iCurrentCount > $iTotalCount Then $iCurrentCount = $iTotalCount _TicksToTime(Int(TimerDiff($a[5])), $a[6], $a[7], $a[8]) _TicksToTime(Int((TimerDiff($a[5]) / $iCurrentCount) * ($iTotalCount - $iCurrentCount)), $a[9], $a[10], $a[11]) $a[0] = StringFormat("%02i:%02i:%02i", $a[6], $a[7], $a[8]) ; elapsed time $a[1] = StringFormat("%02i:%02i:%02i", $a[9], $a[10], $a[11]) ; estimated total time $a[3] = _DateAdd("s", Int((TimerDiff($a[5]) / $iCurrentCount) * ($iTotalCount) / 1000), $a[2]) ; estimated date and time of completion $a[4] = Int($iCurrentCount / $iTotalCount * 100) ; percentage done EndIf Return True EndFunc ;==>_EstimatedTime _EstimatedTime_Example_1() Func _EstimatedTime_Example_1() ConsoleWrite(@CRLF & "+ Example 1" & @CRLF) ConsoleWrite("elapsed " & @TAB & "estimated" & @TAB & "starting date time " & @TAB & "estimated date time" & @CRLF) ConsoleWrite(" _NowCalc() = " & _NowCalc() & @CRLF) Local $n, $sec = @SEC, $aETTF ; "Estimated Time To Finish" data holder _EstimatedTime($aETTF) ; called with just the data holder to init. it For $n = 1234 To 1 Step -1 Sleep(1) If $sec <> @SEC Then $sec = @SEC _EstimatedTime($aETTF, 1234 - $n, 1234) ConsoleWrite($aETTF[0] & @TAB & $aETTF[1] & @TAB & $aETTF[2] & @TAB & $aETTF[3] & @TAB & $aETTF[4] & @CRLF) EndIf Next _EstimatedTime($aETTF, 1234 - $n, 1234) ConsoleWrite($aETTF[0] & @TAB & $aETTF[1] & @TAB & $aETTF[2] & @TAB & $aETTF[3] & @TAB & $aETTF[4] & @CRLF) ConsoleWrite(" _NowCalc() = " & _NowCalc() & @CRLF) EndFunc ;==>_EstimatedTime_Example_1 _EstimatedTime_Example_2() Func _EstimatedTime_Example_2() ConsoleWrite(@CRLF & "+ Example 2" & @CRLF) ConsoleWrite("elapsed " & @TAB & "estimated" & @TAB & "starting date time " & @TAB & "estimated date time" & @CRLF) ConsoleWrite(" _NowCalc() = " & _NowCalc() & @CRLF) Local $aETTF ; "Estimated Time To Finish" data holder _EstimatedTime($aETTF) ; called with just the data holder to init. it ; Save the downloaded file to the temporary folder. Local $sFilePath = @MyDocumentsDir & "\tempFile.bin" Local $sLink = "http://downloads.sourceforge.net/project/portableapps/PortableApps.com%20Platform/PortableApps.com_Platform_Setup_12.0.5.paf.exe?r=http%3A%2F%2Fsourceforge.net%2Fdirectory%2Fbusiness-enterprise%2Fos%3Awindows%2Ffreshness%3Arecently-updated%2F&ts=1442615522&use_mirror=iweb" ; Download the file in the background with the selected option of 'force a reload from the remote site.' Local $hDownload = InetGet($sLink, $sFilePath, $INET_FORCERELOAD, $INET_DOWNLOADBACKGROUND) ; Wait for the download to complete by monitoring when the 2nd index value of InetGetInfo returns True. Do Sleep(1000) If _EstimatedTime($aETTF, InetGetInfo($hDownload, $INET_DOWNLOADREAD), InetGetInfo($hDownload, $INET_DOWNLOADSIZE)) Then ConsoleWrite($aETTF[0] & @TAB & $aETTF[1] & @TAB & $aETTF[2] & @TAB & $aETTF[3] & @TAB & $aETTF[4] & @CRLF) Else ConsoleWrite(". ") EndIf Until InetGetInfo($hDownload, $INET_DOWNLOADCOMPLETE) ; Retrieve the number of total bytes received and the filesize. Local $iBytesSize = InetGetInfo($hDownload, $INET_DOWNLOADREAD) Local $iFileSize = FileGetSize($sFilePath) ; Close the handle returned by InetGet. InetClose($hDownload) _EstimatedTime($aETTF, $iBytesSize, $iFileSize) ConsoleWrite($aETTF[0] & @TAB & $aETTF[1] & @TAB & $aETTF[2] & @TAB & $aETTF[3] & @TAB & $aETTF[4] & @CRLF) ConsoleWrite(" _NowCalc() = " & _NowCalc() & @CRLF) ; Display details about the total number of bytes read and the filesize. ConsoleWrite("The total download size: " & $iBytesSize & " - The total filesize: " & $iFileSize & @CRLF) ; Delete the file. FileDelete($sFilePath) EndFunc ;==>_EstimatedTime_Example_2  
    • Realm
      By Realm
      After Several Minutes searching the forums for a simple solution to calculating for the Unix Time, as required for another project I am working on, I was astonished to not find one clean and universal solution. After looking over the helpfile, I noticed one simple solution given in the example with _DateDiff(). However it did not account for my current timezone or daylight savings time.
      Without further ado... This is my example of Calculating for a current Unix Time stamp or  seconds since Jan, 1st, 1970 00:00:00 GMT.
      This function will account for your timezone as well as for daylight savings time.
      Update: v0.3
      -Added Function to revert a Unix Time stamp into Date form.
      -Added parameter to original _GetUnixTime() allowing for a custom date since Jan 1st 1970 to be passed and converted to Unix Time.
      #include <Date.au3> #include <Array.au3> #include <Constants.au3> Local $iUnixTime1 = _GetUnixTime() MsgBox($MB_SYSTEMMODAL, "Unix Timestamp", "Seconds Since Jan, 1st, 1970 00:00:00 GMT" & @CRLF & $iUnixTime1) Local $sUnixDate1 = _GetDate_fromUnixTime($iUnixTime1) MsgBox($MB_SYSTEMMODAL, "Unix Timestamp", "Get Date from Unix Timestamp in Local Time" & @CRLF & $sUnixDate1) $sUnixDate1 = _GetDate_fromUnixTime($iUnixTime1, False) MsgBox($MB_SYSTEMMODAL, "Unix Timestamp", "Get Date from Unix Timestamp with $iReturnLocal = False which returns UTC Time" & @CRLF & $sUnixDate1) Local $iUnixTime2 = _GetUnixTime('2013/01/01 00:00:00') MsgBox($MB_SYSTEMMODAL, "Unix Timestamp", "Seconds since 2013/01/01 00:00:00" & @CRLF & $iUnixTime2) ; Get timestamp for input datetime (or current datetime). Func _GetUnixTime($sDate = 0);Date Format: 2013/01/01 00:00:00 ~ Year/Mo/Da Hr:Mi:Se Local $aSysTimeInfo = _Date_Time_GetTimeZoneInformation() Local $utcTime = "" If Not $sDate Then $sDate = _NowCalc() If Int(StringLeft($sDate, 4)) < 1970 Then Return "" If $aSysTimeInfo[0] = 2 Then ; if daylight saving time is active $utcTime = _DateAdd('n', $aSysTimeInfo[1] + $aSysTimeInfo[7], $sDate) ; account for time zone and daylight saving time Else $utcTime = _DateAdd('n', $aSysTimeInfo[1], $sDate) ; account for time zone EndIf Return _DateDiff('s', "1970/01/01 00:00:00", $utcTime) EndFunc ;==>_GetUnixTime ;$blTrim: Year in short format and no seconds. Func _GetDate_fromUnixTime($iUnixTime, $iReturnLocal = True) Local $aRet = 0, $aDate = 0 Local $aMonthNumberAbbrev[13] = ["", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] Local $timeAdj = 0 If Not $iReturnLocal Then Local $aSysTimeInfo = _Date_Time_GetTimeZoneInformation() Local $timeAdj = $aSysTimeInfo[1] * 60 If $aSysTimeInfo[0] = 2 Then $timeAdj += $aSysTimeInfo[7] * 60 EndIf $aRet = DllCall("msvcrt.dll", "str:cdecl", "ctime", "int*", $iUnixTime + $timeAdj ) If @error Or Not $aRet[0] Then Return "" $aDate = StringSplit(StringTrimRight($aRet[0], 1), " ", 2) Return $aDate[4] & "/" & StringFormat("%.2d", _ArraySearch($aMonthNumberAbbrev, $aDate[1])) & "/" & $aDate[2] & " " & $aDate[3] EndFunc ;==>_GetUnixDate Enjoy!
      Realm
      Update: >Added some functionality and a suggestion from FireFox