Jump to content

Having trouble with TimeDiff


Recommended Posts

Ok a little background, I wanted to make a Custom Time Clock to keep track of certain things. I have 3 empoyees right now. I want to make this for easy viewing and recording information. I am having a hard time getting the Time/Date to work right. I have 1 employee that works past Midnight so I need it to work with all shifts. I was going to use TimeDiff to figure out how long the employee worked and then divide that by how many parts they ran.

.ini was the only way I knew how to store the data, .xml wasn't working for me....

#include <ButtonConstants.au3>
#include <ComboConstants.au3>
#include <DateTimeConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <Timers.au3>
#include <Array.au3>
#include <Date.au3>

Opt("GUIOnEventMode", 1)
Global $SetupTime[5], $Label[5], $xHour, $xMins, $xSecs, $gspeech, $CurrentSec, $Lastsec
Global $AppData = @ScriptDir & "\AppData\Database\"
Global $DataPath, $CurrentDate
AdlibRegister("UpdateTime", 100)

$GUI = GUICreate("Time Clock", 564, 425, -1, -1)
GUISetOnEvent($GUI_EVENT_CLOSE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_MINIMIZE, "SpecialEvents")
GUISetOnEvent($GUI_EVENT_RESTORE, "SpecialEvents")
$FileMenu = GUICtrlCreateMenu("&File")
$ResetGUI = GUICtrlCreateMenuItem("Reset / Clear", $FileMenu)
$UnusedMenu = GUICtrlCreateMenuItem(" ------- ", $FileMenu)
$ProductionMenu = GUICtrlCreateMenu("P&roduction")
$RunProEXE = GUICtrlCreateMenuItem("Production Manager", $ProductionMenu)
$CalcPerHour = GUICtrlCreateMenuItem("Calculate Per Hour Rate", $ProductionMenu)
$ExitMenu = GUICtrlCreateMenu("Ex&it")
GUISetOnEvent(-1, "SpecialEvents")
$MenuExit = GUICtrlCreateMenuItem("Close", $ExitMenu)
GUICtrlCreateGroup("Clock Information", 8, 16, 265, 169)
GUICtrlCreateLabel("Name:", 24, 48, 35, 17)
$Name = GUICtrlCreateCombo("", 80, 40, 185, 21)
$Data = IniReadSectionNames(@ScriptDir & "\AppData\ClockData.ini")
GUICtrlSetData(-1, StringTrimLeft(_ArrayToString($Data), 2))
GUICtrlSetOnEvent(-1, "InorOut")
$DisplayTime = GUICtrlCreateLabel("", 55, 105, 188, 41)
GUICtrlSetFont(-1, 46, 400, 0, "MS Sans Serif")
$ClockIn = GUICtrlCreateButton("Clock In", 30, 150, 95, 25)
GUICtrlSetFont(-1, 8.5, 600, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "Clock")
$ClockOut = GUICtrlCreateButton("Clock Out", 150, 150, 95, 25)
GUICtrlSetFont(-1, 8.5, 600, 0, "MS Sans Serif")
GUICtrlSetOnEvent(-1, "Clock")
$Date = GUICtrlCreateDate("", 80, 72, 186, 21, $DTS_SHORTDATEFORMAT)
GUICtrlCreateLabel("Date:", 24, 80, 30, 17)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUICtrlCreateGroup("Production Information", 8, 200, 265, 137)
$Label[0] = GUICtrlCreateLabel("Good:", 24, 272, 33, 17)
$Good = GUICtrlCreateInput("", 72, 264, 49, 21)
$Label[1] = GUICtrlCreateLabel("Part #:", 24, 232, 36, 17)
$PartNumber = GUICtrlCreateCombo("", 72, 224, 193, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
$Data = IniReadSectionNames(@ScriptDir & "\AppData\PartData.ini")
GUICtrlSetData(-1, StringTrimLeft(_ArrayToString($Data), 3))
$Label[2] = GUICtrlCreateLabel("Scrap:", 160, 272, 35, 17)
$Scrap = GUICtrlCreateInput("", 216, 264, 49, 21)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Send = GUICtrlCreateButton("Send", 184, 368, 75, 25)
$Cancel = GUICtrlCreateButton("Cancel", 312, 368, 75, 25)
GUISetOnEvent(-1, "SpecialEvents")
GUICtrlCreateGroup("Set-up", 288, 16, 265, 169)
$Label[3] = GUICtrlCreateLabel("Did you have to do a Set-up today?", 328, 40, 173, 17)
$RadioYes = GUICtrlCreateRadio("Yes", 344, 64, 49, 17)
GUICtrlSetOnEvent(-1, "Radio")
$RadioNo = GUICtrlCreateRadio("No", 424, 64, 57, 17)
GUICtrlSetState(-1, $GUI_CHECKED)
GUICtrlSetOnEvent(-1, "Radio")
$SetupTime[0] = GUICtrlCreateLabel("How long did it take?", 354, 120, 109, 17)
GUICtrlSetState (-1, $GUI_Disable)
$SetupTime[3] = GUICtrlCreateCombo("", 312, 144, 57, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "1|2|3|4|5|6|7|8|9|10")
GUICtrlSetState (-1, $GUI_Disable)
$SetupTime[1] = GUICtrlCreateLabel("Hours", 376, 152, 32, 17)
GUICtrlSetState (-1, $GUI_Disable)
$SetupTime[4] = GUICtrlCreateCombo("", 432, 144, 57, 25, BitOR($CBS_DROPDOWN,$CBS_AUTOHSCROLL))
GUICtrlSetData(-1, "5|10|15|20|25|30|35|40|45|50|55")
GUICtrlSetState (-1, $GUI_Disable)
$SetupTime[2] = GUICtrlCreateLabel("Minutes", 496, 152, 41, 17)
GUICtrlSetState (-1, $GUI_Disable)
GUICtrlCreateGroup("", -99, -99, 1, 1)
$Label[4] = GUICtrlCreateLabel("Comments:", 296, 200, 56, 17)
$Comments = GUICtrlCreateEdit("", 296, 224, 257, 113)
GUICtrlSetData(-1, "")
GUISetState(@SW_SHOW)

While 1
    Sleep(10)
WEnd

Func UpdateTime()
    $CurrentSec = @SEC
    If $CurrentSec <> $LastSec Then 
        $LastSec = $CurrentSec
        GUICtrlSetData($DisplayTime, _NowTime())
    EndIf
EndFunc

Func Clock()
    Local $Employee = GUICtrlRead($Name)
    Global $iHour = @HOUR, $AMPM = "AM"
    If $iHour >= 12 Then $AMPM = "PM"
    If $iHour = 00 Then $iHour = 12
    If $iHour > 12 Then $iHour -= 12
    
    $DataPath = @ScriptDir & "\AppData\Database\" & $Employee & "\EmployeeData.ini"
    $CurrentDate = GUICtrlRead($Date)
    $INorOUT = IniRead(@ScriptDir & "\AppData\ClockData.ini", $Employee, "InorOut", "")
    
    If Not FileExists($DataPath) Then
        DirCreate(StringTrimRight($DataPath, 15))
    EndIf  ;First Time Clocking In
    
    If $Employee = "" Then
        SplashTextOn("Clock", "Failed to" & @CRLF & "clock In or Out!!", 320, 190, -1, -1, 35, -1, 18, 500)
        $gspeech = "Failed to clock In or Out!!"
    ElseIf $INorOUT = 1 Then  ;Already Clocked in -- Clocking Out
        $Timer = TimerDiff(IniRead($DataPath, "Timer", "Start", ""))
        _TicksToTime(Int($Timer), $xHour, $xMins, $xSecs)
        WriteINI("TimeOut", $iHour & ":" & @MIN & $AMPM)
        WriteINI("PartRan", GUICtrlRead($PartNumber))
        WriteINI("GoodParts", GUICtrlRead($Good))
        WriteINI("ScrapParts", GUICtrlRead($Scrap))
        WriteINI("PartsPerHour", Round(GUICtrlRead($Good) / ($xHour + ($xMins / 60)), 2))
        WriteINI("ScrapPerHour", Round((GUICtrlRead($Good) + GUICtrlRead($Scrap)) / ($xHour + ($xMins / 60)), 2))
        WriteINI("SetupTime", GUICtrlRead($SetupTime[3]) & "." & (GUICtrlRead($SetupTime[4])/60))
        WriteINI("TotalHours",StringFormat("%02i.%02i", $xHour, ($xMins/60)))
        WriteINI("Comments", GUICtrlRead($Comments))
        IniWrite(@ScriptDir & "\AppData\ClockData.ini", $Employee, "InorOut", 0)
        SplashTextOn("Clock", $Employee & ", Successfully" & @CRLF & "clocked out at " & $iHour & ":" & @MIN & $AMPM & "!", 320, 190, -1, -1, 35, -1, 18, 500)
        $gspeech = $Employee & ", Successfully clocked out. at " & $iHour & ". " & @MIN & $AMPM
        GUICtrlSetData($Name, "")
        $Data = IniReadSectionNames(@ScriptDir & "\AppData\ClockData.ini")
        GUICtrlSetData($Name, StringTrimLeft(_ArrayToString($Data), 2))
    Else ;Clocking In
        IniWrite(@ScriptDir & "\AppData\ClockData.ini", $Employee, "InorOut", 1)
        WriteINI("TimeIn", $iHour & ":" & @MIN & $AMPM)
        IniWrite($DataPath, "Timer", "Start", TimerInit())
        SplashTextOn("Clock", $Employee & ", Successfully" & @CRLF & "clocked in at " & $iHour & ":" & @MIN & $AMPM & "!", 320, 190, -1, -1, 35, -1, 18, 500)
        $gspeech = $Employee & ", Successfully clocked in. at " & $iHour & ". " & @MIN & $AMPM
        GUICtrlSetData($Name, "")
        $Data = IniReadSectionNames(@ScriptDir & "\AppData\ClockData.ini")
        GUICtrlSetData($Name, StringTrimLeft(_ArrayToString($Data), 2))
    EndIf
    WinSetTrans("Clock", "", 185)
    Speech()
    Sleep(1000)
    SplashOff()
EndFunc
    
Func Radio()
    Select
        Case @GUI_CtrlId = $RadioYes
            For $i = 0 To 4
                GUICtrlSetState($SetupTime[$i], $GUI_Enable)
            Next
        Case @GUI_CtrlId = $RadioNo
            For $i = 3 To 4
                GUICtrlSetData($SetupTime[$i], "")
                If $i = 3 Then GUICtrlSetData($SetupTime[$i], "1|2|3|4|5|6|7|8")
                if $i = 4 Then GUICtrlSetData($SetupTime[$i], "5|10|15|20|25|30|35|40|45|50|55")
            Next
            For $i = 0 To 4
                GUICtrlSetState ($SetupTime[$i], $GUI_Disable)
            Next
    EndSelect
EndFunc

Func Password()
    $Password = InputBox("Verify Employee", "                  Enter Password!", "", "*M10", 250, 144)
    If IniRead(@ScriptDir & "\AppData\ClockData.ini", GUICtrlRead($Name), "Password", "") = $Password Then
        Clock()
    Else
        MsgBox(16, "Error", "Password Doesn't Match")
    EndIf
EndFunc

Func WriteINI($xKey, $xValue)
    IniWrite($DataPath, $CurrentDate, $xKey, $xValue)
EndFunc

Func InorOut()
    $Employee = GUICtrlRead($Name)
    $INorOUT = IniRead(@ScriptDir & "\AppData\ClockData.ini", $Employee, "InorOut", "")
    If $INorOUT = 1 Then
        GUICtrlSetState($ClockIn, $GUI_Disable)
        GUICtrlSetState($ClockOut, $GUI_Enable)
    Else
        GUICtrlSetState($ClockIn, $GUI_Enable)
        GUICtrlSetState($ClockOut, $GUI_Disable)
    EndIf
EndFunc

Func Speech()
    Local $temp, $File
    $temp = StringReplace($gspeech, " ", "+")
    $File = StringRegExpReplace($gspeech, "([ .!])", "") 
    If Not FileExists(@TempDir & "\" & $file & ".mp3") Then _
    InetGet("http://translate.google.com/translate_tts?q=" & $temp, _
    @TempDir & "\" & $file & ".mp3")
    SoundPlay(@TempDir & "\" & $file & ".mp3", 1)
    FileDelete(@TempDir & "\" & $file & ".mp3")
    Return
EndFunc


Func SpecialEvents()
    Select
        Case @GUI_CtrlId = $GUI_EVENT_CLOSE
            Exit
        Case @GUI_CtrlId = $ExitMenu
            Exit
        Case @GUI_CtrlId = $Cancel
            Exit
        Case @GUI_CtrlId = $GUI_EVENT_MINIMIZE
        Case @GUI_CtrlId = $GUI_EVENT_RESTORE

    EndSelect
EndFunc

;=================================================================
;================ Can try this if TimerDiff fails ================
;=================================================================
;~ Local $iStartHour = 22 ;; 10PM
;~ Local $iEndHour = 06 ;; 6 AM
;~ Local $iHoursWorked = _HoursTotal($iStartHour, $iEndHour)
;~ If NOT @Error Then MsgBox(0, "Time", $iHoursWorked)

;~ Func _HoursTotal($i_Start, $i_End)
;~     If $i_End < $i_Start Then
;~         Return ((24 - $i_Start) + $i_End)
;~     Else
;~         Return ($i_End - $i_Start)
;~     EndIf
;~     Return SetError(1, 1, 0)
;~ EndFunc

Attached is my AppData Folder...

AppData.zip

Link to comment
Share on other sites

Ps: If your not measuring something that really needs to be in Milliseconds or smaller. But more something that only needs to be done in seconds. You might consider ditching the timerInit() and TimerDiff() as there not really needed and would make things just unnecessary complicated.

You probably only need to used the by AutoIt provided time macros. Witch, btm, also allow you to time things in Milliseconds.

... I was going to use TimeDiff to figure out how long the employee worked ...

O, I see you did not get that far. good. :graduated: Edited by MvGulik

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

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...