#!/usr/bin/perl # Program to parse my time tracking info file # C. David Sherrill # April 2006 $NUM_PRIORITIES = 6; for ($i=1; $i<$NUM_PRIORITIES; $i++) { $time_per_priority[$i] = 0; } if ((@ARGV + 0) < 1) { print "You must provide a filename.\n"; exit; } $infile = $ARGV[0]; open (IFP, $infile) or die "Can't open input file: $!\n"; while ( defined ($line = ) ) { # printf "$line\n"; # Does the line give a time range? # Note: * means zero or more of the previous matching character # and + means one or more of the previous matching character # \d is a digit, \s is white space if ( $line =~ m/\d*:\d*-\s*\d*:\d*\s+/ ) { $times = $&; # get the matched text $task = $'; # get what comes after matched text ($time_start,$time_end) = split(/-\s*/,$times); # compute how much time ($time_start_hour,$time_start_minute) = split(/:/,$time_start); ($time_end_hour,$time_end_minute) = split(/:/,$time_end); if ($time_end_hour < $time_start_hour) { $time_end_hour += 12; } $time_start_abs = $time_start_hour * 60 + $time_start_minute; $time_end_min = $time_end_hour * 60 + $time_end_minute; $time_mins = $time_end_min - $time_start_abs; @taskarr = split(/\s+/, $task); $priority = $taskarr[0]; $category = $taskarr[@taskarr-1]; # add this task to the priority list $time_per_priority[$priority] += $time_mins; &file_category($category, $time_mins); } # or does the line define a supercategory? elsif ( $line =~ m/[A-Z]+\s*=\s*\(\s*/ ) { ($supercategory,$line2) = split(/\s*=\s*\(\s*/, $line); # store the supercategory name if (defined (@supercategory_names) ) { $supercategory_names[@supercategory_names] = $supercategory; } else { $supercategory_names[0] = $supercategory; } # store the category names under the supercategory @list = split(/\s+/, $line2); $j = 0; foreach $category(@list) { if ($category eq ")") { last } $supercategory_entries[@supercategory_names-1][$j++] = $category; } $supercategory_nums[@supercategory_names-1] = $j; } # end the supercategory parsing } # end the file parsing close (IFP); $time_total = 0; for ($i=1; $i<$NUM_PRIORITIES; $i++) { $time_total += $time_per_priority[$i]; } print "\n"; print "Total time: $time_total minutes\n"; print "\n"; for ($i=1; $i<$NUM_PRIORITIES; $i++) { $j = $time_per_priority[$i]; $k = ($j / $time_total) * 100; $days = int($j / (24*60)); $mins = int($j % (24*60)); $hours = int($mins / 60); $mins = int($mins % 60); printf "Priority %d: %03d:%02d:%02d (%2d\%) \n", $i, $days, $hours, $mins, $k; } print "\n"; $a = 0; foreach $supercategory(@supercategory_names) { $supercategory_mins = 0; print "$supercategory:\n"; for ($b=0; $b<$supercategory_nums[$a]; $b++) { $category = $supercategory_entries[$a][$b]; $j = $time_per_category{$category}; $supercategory_mins += $j; $k = ($j / $time_total) * 100; $days = int($j / (24*60)); $mins = int($j % (24*60)); $hours = int($mins / 60); $mins = int($mins % 60); printf "%13s %03d:%02d:%02d (%2d\%) \n", $category, $days, $hours, $mins, $k; } print " " x 14, "-" x 15; print "\n"; $j = $supercategory_mins; $k = ($j / $time_total) * 100; $days = int($j / (24*60)); $mins = int($j % (24*60)); $hours = int($mins / 60); $mins = int($mins % 60); printf "%13s %03d:%02d:%02d (%2d\%) \n", " ", $days, $hours, $mins, $k; print "\n"; $a++; } # print out all the miscellaneous categories not found in a supercategory print "\n"; $supercategory_mins = 0; print "MISC:\n"; foreach $category(keys(%time_per_category)) { $found = 0; $a = 0; foreach $supercategory(@supercategory_names) { for ($b=0; $b<$supercategory_nums[$a]; $b++) { if ($category eq $supercategory_entries[$a][$b]) { $found = 1; last; } } $a++; } if ($found) { next } ; $j = $time_per_category{$category}; $supercategory_mins += $j; $k = ($j / $time_total) * 100; $days = int($j / (24*60)); $mins = int($j % (24*60)); $hours = int($mins / 60); $mins = int($mins % 60); printf "%13s %03d:%02d:%02d (%2d\%) \n", $category, $days, $hours, $mins, $k; } print " " x 14, "-" x 15; print "\n"; $j = $supercategory_mins; $k = ($j / $time_total) * 100; $days = int($j / (24*60)); $mins = int($j % (24*60)); $hours = int($mins / 60); $mins = int($mins % 60); printf "%13s %03d:%02d:%02d (%2d\%) \n", " ", $days, $hours, $mins, $k; print "\n"; # Subroutines start here sub file_category { if (exists $time_per_category{$_[0]}) { # print "Category $_[0] exists.. adding $_[1] minutes\n"; $time_per_category{$_[0]} += $_[1]; } else { $time_per_category{$_[0]} = $_[1]; } }