argumentum

_EstimatedTime() - calculate estimated time of completion

1 post in this topic

#1 ·  Posted (edited)

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

 

Edited by argumentum
Tweak the code

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

  • Similar Content

    • IamKJ
      By IamKJ
      I am trying to allow the GUI to gather info as to when to execute a function.  I am having trouble doing this.  So far this is what I have.
       
      ;Timer Func timer () If Not IsDeclared("iMsgBoxAnswer") Then Local $iMsgBoxAnswer $iMsgBoxAnswer = MsgBox(36,"Timer","Please format your answer in 00:00:00:000") Select Case $iMsgBoxAnswer = 6 ;Yes Global $infotime = InputBox ('Time', 'What time to execute?') Do $rawtimer = ToolTip(@Hour & ':' & @Min & ':' & @Sec & ':' & _MSec()) until $rawtimer = $infotime if $rawtimer = $infotime Then msgbox (0,'Worked','Worked') Else EndIf Case $iMsgBoxAnswer = 7 ;No Exit EndSelect EndFunc 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 I have also tried _GUIToolTip_GetText in order to read the tooltip until the time specified, but it still doesn't work.  Any help would be great.
    • SaeidN
      By SaeidN
      Hi,
      I searched a lot here but didn't find what I want. Most of them were countdown and other stuff and back to 2004.
      I just want a normal timer that when a button on gui is pressed, it starts from 00:00:00 and goes up.
    • Altor
      By Altor
      Hi all.
      I need execute a function at a certain time for example at 00:00 and 12:00.
      My initial idea is use a infinite loop that take the time system and compare with the specified time.
      I do not know if this is the right way to do it. I think this must have occupied the processor for a long time.
      I would appreciate if  comment on whether this is the system that you would use to do this kind of task
      or if you know  any UDF to perform this function.
      Thanks to all
    • xCobra15x
      By xCobra15x
      Hi all, 
      I am looking for a quick tip on why my autoit script crashes when I run a timer. Currently I have this code:
            ....(more code above this)....
                Case $button2
                            function1()
                 Case $butTimer
                            _Timer_setTimer($hGUI, 3200, "_timedout")
              ......
               Func _timedout()
                             Msgbox(0, "Timed out", "Time is up!")
                Endfunc
      When the button is pressed, my timer starts, correct? It doesnt seem to matter if I have it on button activation or not, after the time goes by for a bit, it crashes.
      Seemingly when the time is up, my script crashes. 
      Thanks for your help!
       
       
    • Rfsvieira
      By Rfsvieira
      Hello, i havê been reading, and thinking.. Im on my tablet só i WLL try explain what im trying to do....
      Will havê an GUI with 8 timer input that havê to set timer, but wen our if 2 our more ends at some time it havê to run 1, then outher , then outher
      Timer 1, - input 5 minutes, timer 2 - input 10, timer 3 input 15, timer 4 - input 8,......
      At some time,  timer 1 will end ate some time of timer 2 / then it at some time will end at some time timer 2 and 3 
      How i said that wen it happpens first is 2 then 1, then outhers and if 1 is done then 1st is 3, will have more than 9 input boxes that can happen it end at some time, imagine if wen timer 1= 0 function1(), then after end funciona start countdown again, if timer 2 = 0 run function2(), then after end function2 start contdoen again, if both =0 at some time first run 1 then 2, and if wen ends 1, 2,3,4 are =0 :-( :-( :-(  im lost
      This work…???
       
      thank you!
       
      Thanks for time, any ideas???