Clock In, Clock Out

I’ve worked (and presently work) a number of jobs where I have to keep track of my own hours. As with any repetitive task, the answer is to automate. So tracking your hours isn’t hard or time consuming, but having your computer do it for you while learning a little scripting appeals to me more. Thus, I concocted a nice little bash shell script with a pinch of Ruby–for the heavy lifting.

The script/program is simple. It’s called clock. All you do is type clock into the Terminal (mac) and…WAHLA! You’re clocked in. When you want to clock out, type clock again and you’re clocked out. Each time you clock in or out, the program records a timestamp with the date and time. After each clock out, it records the number of hours worked during that session. When it’s time to submit your timecard so you can receive your paycheck, simply type clock -f to finalize your timecard. This will sum your session hours and rename the timecard with the start and end date so you know the period.

I’ve included a zip archive of the files (clock, timeadd.rb, timediff.rb) so if you find yourself in the same boat you can give the program a shot. I suggest you add them to your own “bin” folder in your home directory like I’ve done. If you aren’t familiar with this concept, let me give a short explanation.

Dedicating a directory in your home folder is convenient if you like to write various little programs/scripts. It allows you to…segregate these from the built in programs/scripts that come with your OS so they are easily accessible and you remember which ones are yours. To make these programs work just like the rest of the programs (e.g. ls, rm, less, pwd, etc.) add the directory to your $PATH as follows:

export PATH=$PATH:/Users/USERNAME/bin

Of course, be sure to replace USERNAME with the obvious. You can type this command into the CLI or you can just add it to your .profile file in your home directory. If you don’t have a .profile file, go ahead and create one. Every time you open Terminal (or another CLI), the .profile file is read and executed, thus you will never have to type in those commands again manually.

Two more things to make this work:

  1. Make sure to open clock in a text editor and change the path where the timecard-current.txt will be saved (in addition to any other paths).
  2. Make sure to install the appropriate gems for the Ruby scripts. Simply type the following into Terminal:
    gem install chronic_duration time_diff

If you have any thoughts, suggestions or questions, please comment. I value feedback.

4 Comments

  1. I’ve been wanting this script from you for a while. I need to clock my hours and I do a horrible job of it.

    Reply

    1. Do you have a means to run shell scripts? If not, you’ve probably heard of Cygwin. It emulates a shell on Windows.

      Reply

  2. […] a follow up to Clock In, Clock Out, I thought I would discuss the methods I used for calculating the amount of hours worked in my […]

    Reply

  3. […] Paste that into a text editor and save it without a suffix/filetype as something like desktop. Then execute the following command while in the folder where you saved the script (preferably in your personal bin directory): chmod 755 desktop. You should be good to go as long as the directory you saved it in is mapped to your $PATH (If you aren’t sure what that means, read the 4th and 5th paragraphs of Clock In, Clock Out). […]

    Reply

Comments, questions and feedback welcome.