Jump to content

Stumped on Time....


Recommended Posts

Ok so when an employee clicks a button on the GUI it creates a line in a log file:

[03/09/2011 - 21:15 - 704394] Name N. - Clocked In

The following function is suppose to read that last line once they have clocked out and Return a total hours and mins worked rounding to the nearest 15 min. Problem also is that 1 shift runs over midnight 24:00-00:00.

Func Time()
    $TimeString = "[03/09/2011 - 21:15 - 704394] Name N. - Clocked In" ;FileReadLine(@ScriptDir & "\Logs\Time Log.log" , -1)
    $StartHour = StringTrimRight(StringTrimLeft($TimeString, StringInStr($TimeString, ":") - 3), StringLen(StringTrimLeft($TimeString, StringInStr($TimeString, ":") - 3)) - StringInStr(StringTrimLeft($TimeString, StringInStr($TimeString, ":") - 3), ":") + 1);, StringLen($String))
    $Min = StringTrimRight(StringTrimLeft($TimeString, StringInStr($TimeString, ":")), StringLen(StringTrimLeft($TimeString, StringInStr($TimeString, ":"))) - 2);, StringLen($String))
    Switch $Min
        Case 0 To 7
            $Min = 00
        Case 7 To 22
            $Min = 15
        Case 22 To 37
            $Min = 30
        Case 38 To 52
            $Min = 45
        Case 53 To 59
            $Min = 00
            $StartHour = $StartHour + 1
    EndSwitch
    $StartTime = _TimeToTicks($StartHour, $Min)
    $FinishHour = @HOUR
    Switch @MIN
        Case 0 To 7
            $CurMin = 00
        Case 7 To 22
            $CurMin = 15
        Case 22 To 37
            $CurMin = 30
        Case 38 To 52
            $CurMin = 45
        Case 53 To 59
            $CurMin = 00
            $FinishHour = $FinishHour + 1
    EndSwitch

    $FinishTime = _TimeToTicks($FinishHour, $CurMin)
    MsgBox(0, "Test", $FinishTime &@CRLF& $StartTime)
    If $FinishTime < $StartTime Then
        $StartTime = 86400000 - $StartTime
        $TotalTime = $StartTime + $FinishTime
    Else
        $TotalTime = $FinishTime - $StartTime
    EndIf


     _TicksToTime($TotalTime, $Hour, $Mins);, $Secs)
     $TotalTime = $Hour & " Hour(s) & " & $Min & " Min(s)"

    Return $TotalTime
EndFunc
Link to comment
Share on other sites

Thank you took a little editing but I got it to fit my needs:

Func Time()
    Func Time()
    $TimeString = FileReadLine(@ScriptDir & "\Logs\Time Log.log", -1)
    $TimeString = StringMid($TimeString, 2, 16) & ":00"
    $Min = _DateDiff( 'n',$TimeString, _NowCalc() & ":00")
    $Hour = 0

    While $Min >= 60
        $Min = $Min - 60
        $Hour = $Hour + 1
        Sleep(10)
    WEnd
    Sleep(300)

    Return($Hour & " Hour(s) " & $Min & " Min(s)")
EndFunc

Func _NowCalc()
    $iHour = @HOUR
    Switch @MIN
        Case 0 To 7
            $iMin = "00"
        Case 7 To 22
            $iMin = 15
        Case 22 To 37
            $iMin = 30
        Case 38 To 52
            $iMin = 45
        Case 53 To 59
            $iMin = "00"
            $iHour = $iHour + 1
            If $iHour < 10 Then $iHour = "0" & $iHour
    EndSwitch
    Return (@YEAR & "/" & @MON & "/" & @MDAY & " " & $iHour & ":" & $iMin)
EndFunc   ;==>_NowCalc Modified
Edited by rogue5099
Link to comment
Share on other sites

rogue5099,

Made the following changes:

changed hour/minute calculation

formatted hour and minute string variables

took out modified _nowcalc()

Try the code and see if it fits. You can round off the minutes by manipulating the $min variable AFTER the duration calc.

#include <date.au3>

time()

Func Time()
    $TimeString = "[2011/03/10 00:10 - 704394] Name N. - Clocked In"
    $TimeString = StringLeft($TimeString, 17)
    $TimeString = StringTrimLeft($TimeString, 1)
    $TimeString = $TimeString & ":00"
    $Min = _DateDiff( 'n', $timestring, _NowCalc())

    $hour = stringformat('%02s',Floor($min / 60))
    $min  = stringformat('%02s',Mod($min, 60))
    consolewrite($hour & ':' & $min & @lf)

    Return($Hour & " Hour(s) " & $Min & " Min(s)")
EndFunc

Good Luck,

kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Link to comment
Share on other sites

Thanks for your help I got it narrowed down to saving starttime to INI

IniWrite(@ScriptDir & "\Settings.ini", "Info", "StartTime", _NowCalc())

Func Time()
    $TimeString = IniRead(@ScriptDir & "\Settings.ini", "Info", "StartTime", _NowCalc()) & ":00"
    $Min = _DateDiff( 'n',$TimeString, _NowCalc() & ":00")
    $Hour = Floor($Min / 60)
    $Min  = Mod($Min, 60)
    $PerHour = Round($Good / ($Hour + ($Min / 60)), 2)
    Return($Hour & " Hour(s) " & $Min & " Min(s)")
EndFunc

Func _NowCalc()
    Return (@YEAR & "/" & @MON & "/" & @MDAY & " " & @HOUR & ":" & @MIN)
EndFunc   ;==>_NowCalc Modified
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...