=================================================================== RCS file: /cvs/RT/Invoicing/report_time,v retrieving revision 1.2 retrieving revision 1.6 diff -u -r1.2 -r1.6 --- RT/Invoicing/report_time 2020/08/04 02:22:44 1.2 +++ RT/Invoicing/report_time 2020/08/20 03:15:23 1.6 @@ -1,9 +1,9 @@ #!/usr/bin/perl -# $AFresh1: report_time,v 1.1 2020/08/04 01:14:42 afresh1 Exp $ +# $AFresh1: report_time,v 1.5 2020/08/04 01:28:24 afresh1 Exp $ use v5.16; use warnings; -my $dir = "$ENV{HOME}/.time"; +my $dir = "$ENV{HOME}/rt_invoicing/time"; my %conversions = ( m => "Misc", @@ -31,10 +31,8 @@ map { "$dir/$_" } sort grep { /^\d{4}-\d{2}-\d{2}$/ } readdir $dh; }; -my %entries; +my @entries; foreach my $file (@files) { - my $key = 'm'; - open my $fh, '<', $file or die "Unable to open $file: $!"; while (readline $fh) { my ($dt, $type, $description) @@ -43,53 +41,66 @@ my %entry = ( dt => $dt, type => $type ); $entry{description} = $description if $description; - $key = descr_to_key($description) - if $type eq 'sta' and $description; - - push @{ $entries{$key} }, \%entry; + push @entries, \%entry; } close $fh; } my %total; -foreach my $key (sort keys %entries) { +{ my $start; - foreach my $entry (@{ $entries{$key} }) { + my $key = 'm'; + foreach my $entry (@entries) { # Sat Jul 11 11:56:06 PDT 2020 $entry->{datetime} = Time::Piece->new->strptime( $entry->{dt}, "%a %b %e %H:%M:%S %Z %Y"); - $total{ $entry->{datetime}->date }{$key} - += $entry->{datetime} - $start->{datetime} - if $start; + if ($start) { + $key = descr_to_key($start->{description}) + if $start->{description}; + $total{ $entry->{datetime}->date }{$key} + += $entry->{datetime} - $start->{datetime}; + } $start = $entry->{type} eq 'sta' ? $entry : ''; } # If we have a timer running, pretend it stops now. if ($start) { + $key = descr_to_key($start->{description}) + if $start->{description}; my $now = localtime; $total{ $now->date }{$key} += $now - $start->{datetime} } } +my $total = 0; foreach my $date ( sort keys %total ) { + my $subtotal = 0; foreach my $key ( sort keys %{ $total{$date} } ) { + my $hours = $total{$date}{$key}->hours; + # round to the quarter hour - my $hours = sprintf "%.2f", - 25 * sprintf "%.2f", $total{$date}{$key}->hours / 25; - say "$date $hours $key";# if $hours != 0; + $hours = sprintf "%.02f", + 25 * sprintf "%.02f", $hours / 25; + + say "$date $hours $key" if $hours != 0; + + $subtotal += $hours; + $total += $hours; } + printf "# %s %.02f\n", $date, $subtotal; } +printf "# Total %.02f\n", $total; sub descr_to_key { my ($descr) = @_; my @key = split /\s+/, $descr; - $key[-1] = $conversions{ $key[-1] } - if @key < 3 and $conversions{ $key[-1] }; + $key[-1] = $conversions{ lc $key[-1] } + if @key < 3 and $conversions{ lc $key[-1] }; return "@key"; }