Dotfiles, utilities, and other apparatus.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
2.0 KiB

  1. #!/usr/bin/env perl
  2. use warnings;
  3. use strict;
  4. use 5.10.0;
  5. use File::HomeDir;
  6. my $rcfile = File::HomeDir->my_home . "/.timelogrc";
  7. # set an unreasonable default, so it's obvious:
  8. our $hourly_rate = 10000;
  9. do $rcfile if -e $rcfile;
  10. my $total_hours = 0;
  11. my $total_expenses = 0;
  12. # while we've got input from a file/stdin, split it into two datestamps
  13. # and feed that to date(1)
  14. while (my $line = <>) {
  15. chomp($line);
  16. if ($line =~ m/
  17. ^
  18. \s{2,} # leading whitespace, two or more
  19. expense: # the string "expense"
  20. [ ] # a space
  21. \$ ([\d.]+) # an amount
  22. [ ]
  23. - # a dash with spaces
  24. [ ]
  25. (.*?) # any notes
  26. $ # eol
  27. /ix)
  28. {
  29. my $expense_amount = $1;
  30. $total_expenses += $expense_amount;
  31. my $expense_notes = $2;
  32. say "* $line";
  33. }
  34. # skip things that don't look like a date range - everything else can
  35. # just be commentary.
  36. next unless $line =~ m/
  37. ^
  38. \s{2,} # leading whitespace of two or more chars
  39. (
  40. \d{4} - \d{2} - \d{2} # date
  41. )
  42. [ ] # space
  43. (
  44. \d{1,2} : \d{2} # start timestamp
  45. )
  46. [ ] - [ ] # delimiter
  47. (
  48. \d{1,2} : \d{2} # end timestamp
  49. )
  50. $
  51. /ix;
  52. my $start_timestamp = "$1 $2";
  53. my $end_timestamp = "$1 $3";
  54. my ($start, $end) = map { get_seconds($_) } ($start_timestamp, $end_timestamp);
  55. my $interval = $end - $start;
  56. my $hours = $interval / 3600;
  57. $total_hours += $hours;
  58. say sprintf("* $line\t%.3f hours", $hours);
  59. }
  60. my $hourly_total = $total_hours * $hourly_rate;
  61. my $overall_total = $hourly_total + $total_expenses;
  62. print "\n";
  63. say sprintf(
  64. # extra spaces at end for newline in markdown:
  65. '%.3f total hours at $%.2f/hr = $%.2f ',
  66. $total_hours,
  67. $hourly_rate,
  68. $hourly_total
  69. );
  70. say sprintf(
  71. '$%.2f in expenses',
  72. $total_expenses
  73. );
  74. print "\n";
  75. say sprintf('**Total due = $%.2f**', $overall_total);
  76. sub get_seconds {
  77. my ($stamp) = @_;
  78. my $seconds = `date --date="$stamp" +%s`;
  79. chomp($seconds);
  80. return $seconds;
  81. }