Jump to content
Sign in to follow this  
hiimjoey11

Clock in/out help

Recommended Posts

hiimjoey11

I am trying to create a program that allows me to clock in and out, and this is what i have so far.. everything works great except i want to update (in real time) how long the user has been clocked in.

I can't seem to DateDiff between my Input1 () and NowTime

Any ideas?

Thanks!

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Date.au3>
#include <File.au3>
#include <Misc.au3>
#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("TimeKeeper", 258, 199, 192, 124)
$Input1 = GUICtrlCreateInput(_NowTime(), 24, 24, 113, 28)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Clock in", 152, 24, 83, 33)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$Input2 = GUICtrlCreateInput(_DateTimeFormat(_DateAdd('h', 8, _NowCalc()), 3), 24, 96, 113, 28)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Button2 = GUICtrlCreateButton("Clock out", 152, 96, 83, 33)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$Button3 = GUICtrlCreateButton("SUBMIT", 24, 144, 209, 41)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
$Label1 = GUICtrlCreateLabel("Clocked in for ", 24, 64, 200, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case _IsPressed("0D")
            $clockedIn = GUICtrlRead($Input1)
            $hours = _DateDiff('h', $clockedIn, _NowCalc())
            $hours = _DateTimeFormat($hours, 3)
            GUICtrlSetData($Label1, "Clocked in for " & $hours & " hours.")
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            GUICtrlSetData($Input1, _NowTime())
            GUICtrlSetData($Input2, _DateTimeFormat(_DateAdd('hh', 8, _NowCalc()), 3))
        Case $Button2
            GUICtrlSetData($Input2, _NowTime())
                    
    EndSwitch
WEnd

Share this post


Link to post
Share on other sites
MikahS

Is the your script in it's entirety? :ermm:


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites
hiimjoey11

Is the your script in it's entirety? :ermm:

 

Yes, why?

Share this post


Link to post
Share on other sites
JohnOne

Switch is not what you need for...

Case _IsPressed("0D")

It will never be true

Try Select

EDIT: Maybe almost never, it's not the correct way though.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites
hiimjoey11

Switch is not what you need for...

Case _IsPressed("0D")

It will never be true

Try Select

EDIT: Maybe almost never, it's not the correct way though.

 

Thanks i will try that, but that's not my most pressing concern.. I can't seem to Math correctly today.. I want the DateDiff to work :/

Share this post


Link to post
Share on other sites
MikahS

I just see that you haven't declared any variables, I'm currently looking at it though :)

Instead of doing Case _IsPressed("0D") put the function into a variable and check if that variable equals true (that is the return from _IsPressed if it is successful)

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites
hiimjoey11

Whay are you testing _NowTime against _NowCalc?  Why not use same?

 

_NowTime() gives it to me in just the hours, where as NowCalc gives the date which apparently i need to do a DateAdd.. If there is an easier way to do this, please let me know!

Share this post


Link to post
Share on other sites
MikahS

I have modified your script to update the boxes always, which I still don't see the need for a clockout window with the time in it, since they both use the same time. Now by clicking clock in, you will set the $time to the current time, then when hitting clockout you will then set the $timeDiff var to hold the difference in hours.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Date.au3>
#include <File.au3>
#include <Misc.au3>

Local $clockedIn, $Label1, $time, $Input2, $nMsg, $Input1
Local $Button2, $Button1, $Button3, $Form1, $timeDiff = ""

AdlibRegister("refresh", 250)

GUI()
refresh()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button3
            If $timeDiff = "" Then
                MsgBox(0, "clockin/clockout", "Did not clock out.")
                ContinueLoop
            EndIf
            GUICtrlSetData($Label1, "Clocked in for " & $timeDiff & " hours.")
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $time = _DateTimeFormat(_NowCalc(), 3)
        Case $Button2
            $timeDiff = _DateDiff("h", $time, _NowCalc())
            GUICtrlSetData($Label1, "Clocked in for " & $timeDiff & " hours.")
    EndSwitch
WEnd



Func GUI()
    $Form1 = GUICreate("TimeKeeper", 258, 199, 192, 124)
    $Input1 = GUICtrlCreateInput("", 24, 24, 113, 28)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $Button1 = GUICtrlCreateButton("Clock in", 152, 24, 83, 33)
    GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
    $Input2 = GUICtrlCreateInput("", 24, 96, 113, 28)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $Button2 = GUICtrlCreateButton("Clock out", 152, 96, 83, 33)
    GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
    $Button3 = GUICtrlCreateButton("SUBMIT", 24, 144, 209, 41)
    GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
    $Label1 = GUICtrlCreateLabel("Clocked in for ", 24, 64, 200, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    GUISetState(@SW_SHOW)
EndFunc   ;==>GUI

Func refresh()
    GUICtrlSetData($Input2, _NowTime())
    GUICtrlSetData($Input1, _NowTime())
EndFunc   ;==>refresh

EDIT: Maybe instead of having a clockout window with a submit button, try taking those away and just put the hours mins and secs into the second window to show the time difference from clock in to clock out.

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites
hiimjoey11

I have modified your script to update the boxes always, which I still don't see the need for a clockout window with the time in it, since they both use the same time. Now by clicking clock in, you will set the $time to the current time, then when hitting clockout you will then set the $timeDiff var to hold the difference in hours.

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Date.au3>
#include <File.au3>
#include <Misc.au3>

Local $clockedIn, $Label1, $time, $Input2, $nMsg, $Input1
Local $Button2, $Button1, $Button3, $Form1, $timeDiff = ""

AdlibRegister("refresh", 250)

GUI()
refresh()

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $Button3
            If $timeDiff = "" Then
                MsgBox(0, "clockin/clockout", "Did not clock out.")
                ContinueLoop
            EndIf
            GUICtrlSetData($Label1, "Clocked in for " & $timeDiff & " hours.")
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            $time = _DateTimeFormat(_NowCalc(), 3)
        Case $Button2
            $timeDiff = _DateDiff("h", $time, _NowCalc())
            GUICtrlSetData($Label1, "Clocked in for " & $timeDiff & " hours.")
    EndSwitch
WEnd



Func GUI()
    $Form1 = GUICreate("TimeKeeper", 258, 199, 192, 124)
    $Input1 = GUICtrlCreateInput("", 24, 24, 113, 28)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $Button1 = GUICtrlCreateButton("Clock in", 152, 24, 83, 33)
    GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
    $Input2 = GUICtrlCreateInput("", 24, 96, 113, 28)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
    $Button2 = GUICtrlCreateButton("Clock out", 152, 96, 83, 33)
    GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
    $Button3 = GUICtrlCreateButton("SUBMIT", 24, 144, 209, 41)
    GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
    $Label1 = GUICtrlCreateLabel("Clocked in for ", 24, 64, 200, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    GUISetState(@SW_SHOW)
EndFunc   ;==>GUI

Func refresh()
    GUICtrlSetData($Input2, _NowTime())
    GUICtrlSetData($Input1, _NowTime())
EndFunc   ;==>refresh

EDIT: Maybe instead of having a clockout window with a submit button, try taking those away and just put the hours mins and secs into the second window to show the time difference from clock in to clock out.

 

Great idea! I wanted the clocked in for label to update with how long ive been at work so far..
 
Here is what I wanted!  Change the clock in to 7:...:..: and click the clock out button, and it should say "youve been clocked in for 6 hours" or something
 
 
 
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Date.au3>
#include <File.au3>
#include <Misc.au3>
#region ### START Koda GUI section ### Form=
$Form1 = GUICreate("TimeKeeper", 258, 199, 192, 124)
$Input1 = GUICtrlCreateInput(@HOUR & ":" & @MIN & ":" & @SEC, 24, 24, 113, 28)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Button1 = GUICtrlCreateButton("Clock in", 152, 24, 83, 33)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$Input2 = GUICtrlCreateInput(@HOUR + 8 & ":" & @MIN & ":" & @SEC, 24, 96, 113, 28)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
$Button2 = GUICtrlCreateButton("Clock out", 152, 96, 83, 33)
GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
$Button3 = GUICtrlCreateButton("SUBMIT", 24, 144, 209, 41)
GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
$Label1 = GUICtrlCreateLabel("Clocked in for ", 24, 64, 200, 20)
GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
GUISetState(@SW_SHOW)
#endregion ### END Koda GUI section ###
Dim $clockedIn[3]

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case _IsPressed("0D")
            $clockedIn = StringSplit(GUICtrlRead($Input1), ":")
            $hours = @HOUR - $clockedIn[1] & ":" & $clockedIn[2] & ":" & $clockedIn[3]
            GUICtrlSetData($Label1, "Time at work: " & $hours)
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            GUICtrlSetData($Input1, @HOUR & ":" & @MIN & ":" & @SEC)
            GUICtrlSetData($Input2, @HOUR + 8 & ":" & @MIN & ":" & @SEC)
        Case $Button2
            GUICtrlSetData($Input2, @HOUR & ":" & @MIN & ":" & @SEC)
        Case $Button3
            
    EndSwitch
WEnd
Edited by hiimjoey11

Share this post


Link to post
Share on other sites
MikahS

I'm not sure I understand the question asked (as if you hit clock in, wait 6 hours and hit clock out it will say exactly "You have been clocked in for 6 hours" with my code; have you not tried it?), also why post your code again?

If you want to be updated on how many hours you've been there (all the time), just add this line to the refresh function in my code provided:

GUICtrlSetData($Label1, "Clocked in for " & $timeDiff & " hours.")
Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites
hiimjoey11

 

I'm not sure I understand the question asked (as if you hit clock in, wait 6 hours and hit clock out it will say exactly "You have been clocked in for 6 hours" with my code; have you not tried it?), also why post your code again?

If you want to be updated on how many hours you've been there (all the time), just add this line to the refresh function in my code provided:

GUICtrlSetData($Label1, "Clocked in for " & $timeDiff & " hours.")

Thanks i will use that

Share this post


Link to post
Share on other sites
MikahS

No problem, let us know if you have anymore questions ;)


Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Share this post


Link to post
Share on other sites
kylomas

hiimjoey11,

If you want to do time arithmetic using the macros you need to account for hour and minute boundries.  Much better to use the existing UDF to get number of seconds and calculate hours and minutes.

Also, instead of _IsPressed you can use an accelerator key, ENTER key in this case.

The following is an example of doing duration arithmetic and using an accelerator key.  I also use an adlib to update the running time.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <Date.au3>
#include <StaticConstants.au3>

local $Gui      =   GUICreate("TimeKeeper", 250, 200,-1,-1,-1,$WS_EX_COMPOSITED)
local $ClockIn  =   GUICtrlCreateButton('Clock In',10,20,230,30)
                    GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
local $ClockOut =   GUICtrlCreateButton('Clock Out',10,60,230,30)
                    GUICtrlSetFont(-1, 12, 800, 0, "MS Sans Serif")
local $Submit   =   GUICtrlCreateButton("SUBMIT", 20,175,210,20)
                    GUICtrlSetFont(-1, 14, 800, 0, "MS Sans Serif")
local $Hours    =   guictrlcreatelabel('',20,120,210,20, $ss_center)
                    guictrlsetstate($Hours,$GUI_HIDE)
local $COTime   =   guictrlcreatelabel('',20,140,210,20, $ss_center)
                    guictrlsetstate($COTime,$GUI_HIDE)
local $Enter    =   GUICtrlCreateDummy()

                    GUISetState(@SW_SHOW)

Dim $aAccelKeys[1][2] = [["{ENTER}", $Enter]]
GUISetAccelerators($aAccelKeys)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        case $ClockIn, $Enter
            guictrlsetdata($ClockIn,_NowCalc())
            Guictrlsetstate($Hours,$GUI_Show)
            _ElapsedTime()
            Guictrlsetstate($COTime,$GUI_Show)
            AdlibRegister('_ElapsedTime',1000)
            guictrlsetdata($COTime, 'Clock Out Time = ' & _dateadd('h',8,guictrlread($ClockIn)))
        case $ClockOut
            AdlibUnRegister('_ElapsedTime')
        case $Submit
            ;
            ; go do something else
            ;
    EndSwitch

WEnd

func _ElapsedTime()

    local $s

    $s = _datediff('s',guictrlread($ClockIn),_Nowcalc())
    guictrlsetdata($Hours, 'Time worked = ' & stringformat( '%02i:%02i:%02i', _
                                                    floor( $s/60^2), _
                                                    mod  ( $s/60,60 ), _
                                                    mod  ( $s, 60 )))

endfunc

This is just a quick and dirty to show technique.

kylomas

edit: corrected duration arithmetic 

Edited by 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

Share this post


Link to post
Share on other sites
MikahS

Kylomas, nice solution :thumbsup:

EDIT: I really like kylomas' solution, it is very clean and much better than mine :)

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

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  

×