Simple Calculation of Billable Time using epoch-seconds

Every freelancer uses some kind of accounting-package to keep track of the billable time, but, given multiple machines, on-site gigs and the like, keeping track is not always that easy, isn’t it? For that reason, I came up with a really simple combination of two Perl-scripts.

The first one,, writes the epoch-seconds to a temporary file and prints some information to the terminal-window.

Update: Thanks to Hebikai for pointing out a problem with the relative path, the scripts are using the environment-variable $HOME to determine the path now.


$HOME_dir = $ENV{HOME};
$DATA_dir = "$HOME_dir/work/time_control_stuff";

$start_time = time;

#print "Seconds: $start_time\n";

print "customer|topic\n";
print "Remarks: \n";
print localtime($start_time)."\n";

open(MY_HANDLE, ">$DATA_dir/time.log") or die "$0: something went wrong: $!\n";
print MY_HANDLE $start_time."\n";

The script assumes, that there is a directory called time_control_stuff in ~/work/. Its output should be copied or piped into a file, it records the time you started. If you want, feel free to modify the customer|topic and Remarks lines to taste.

If you are done with the task, the script comes into play.

$HOME_dir = $ENV{HOME};
$DATA_dir = "$HOME_dir/work/time_control_stuff";
$end_time = time;

open(MY_HANDLE, "$DATA_dir/time.log") or die "$0: something went wrong: $!\n";

$start_time = $_;


$total_time = $end_time - $start_time;
$total_mins = $total_time/60;
$total_hours = $total_mins/60;

print "Start: ".localtime($start_time)."\n";

print "End: ".localtime($end_time)."\n";

print "Total: ".$total_time."\n";
print "Mins: $total_mins\n";
print "Hours: $total_hours\n";
print "\nBill: \n\n";

Its output is pretty much self-explanatory, copy or append to the same file and you are done.

I use an USB-stick to carry the scripts with me all the time. If I need to record billable time and don’t have access to one of my machines, these scripts are my way to go. I input the recorded data into my accounting-package as soon as I’m able to do so.

Granted, they could be a lot more sophisticated, but remember KISS (Keep It Simple Sā€¦) rules. Use the scripts as a starting-point for you “accounting on the road.”


  1. Hebikai
    Posted October 8, 2007 at 9:09 pm | Permalink

    Doesn’t work; this line “open(MY_HANDLE, “>$DATA_dir/time.log”) or die “$0: something went wrong: $!\n”;” results in an error “No such file or directory”

    I made sure to create a directory by doing a mkdir ~/work/time_control_stuff”. The directory exists and I did a touch time.log in that directory. I also changed the permissions to 777 on time.log. No dice.

  2. Hebikai
    Posted October 8, 2007 at 9:28 pm | Permalink

    I put in the absolute path (not using ~/work/) but by hard-coding “/root/work..” the start script works. The end script doesn’t work, however.

    It basically starts but I get no output. I did a rm time.log and it errored out, so that’s a good sign. But if the file exists the script just stays in a loop.

  3. Erik
    Posted October 8, 2007 at 11:00 pm | Permalink

    I’m sorry, you are right, I need to use an absolute Path ā€“ on my Mac at least ā€“ as well. But both scripts are doing what they should, as soon as the absolute Path is used. (I tried it a couple of minutes ago.) I added code to determine the location of $HOME.

    Which OS are you on? I never spent too much time thinking about the localtime call, but it should be available by default ā€“ at least on *NIX.

Post a Comment

Your email is never published nor shared. Required fields are marked *, comments are moderated.