From 6afe5f40c4f05d3db955b3842e5ec1a34ae92296 Mon Sep 17 00:00:00 2001 From: Andrew Ruthven Date: Wed, 16 Apr 2008 21:15:19 +1200 Subject: [PATCH] Convert to iCal::Parser and improve output a bit. --- bin/summary-ics.pl | 67 +++++++++++++++++++++++++++++++--------------- 1 file changed, 46 insertions(+), 21 deletions(-) diff --git a/bin/summary-ics.pl b/bin/summary-ics.pl index f1efb99..997b101 100755 --- a/bin/summary-ics.pl +++ b/bin/summary-ics.pl @@ -1,9 +1,10 @@ #!/usr/bin/perl -w use strict; -use Data::ICal; use Getopt::Long; use Date::Manip; +use iCal::Parser; +use Text::Wrap; my $start = undef; my $end = undef; @@ -28,33 +29,57 @@ die "I can't find the file: $filename\n" print "Processing date range: " . UnixDate($start, '%Y-%m-%d') . " - " . UnixDate($end, '%Y-%m-%d') . "\n"; -my $calendar = Data::ICal->new(filename => $filename); +my $parser = iCal::Parser->new( + 'start' => UnixDate($start, '%Y%m%d'), + 'end' => UnixDate($end, '%Y%m%d'), + 'no_todos' => 1, +); + +my $cal = $parser->parse($filename); +my $events = $cal->{'events'}; + +my %clients; use Data::Dumper; -for my $entry (@{ $calendar->entries() }) { - if ($entry->ical_entry_type eq 'VEVENT') { - my $summary = return_property($entry, 'Summary'); - my $description = return_property($entry, 'Description'); - my $dtstart = return_property($entry, 'dtstart'); - print "event: $summary - $dtstart - $description\n"; +for my $year (keys %{ $events }) { + for my $month (sort keys %{ $events->{$year} }) { + for my $day (sort keys %{ $events->{$year}{$month} }) { + for my $uid (keys %{ $events->{$year}{$month}{$day} }) { + my $summary = $events->{$year}{$month}{$day}{$uid}{'SUMMARY'} || ''; + my $desc = $events->{$year}{$month}{$day}{$uid}{'DESCRIPTION'} || 'misc'; + my $hours = $events->{$year}{$month}{$day}{$uid}{'hours'} || 0; + my $dtstart = $events->{$year}{$month}{$day}{$uid}{'DTSTART'} || ''; + + $hours = 8 + if defined $events->{$year}{$month}{$day}{$uid}{'allday'}; + + $clients{lc($summary)}{'work'}{$desc}{'hours'} += $hours; + $clients{lc($summary)}{'client'} ||= $summary; + push @{ $clients{lc($summary)}{'work'}{$desc}{'times'} }, [ $hours, $dtstart ]; + #print "event: $summary - $dtstart - $desc\n"; #print Dumper($entry); - } else { - print "not event\n"; + } } + } } -sub return_property { - my $entry = shift; - my $property = shift; +for my $client (sort keys %clients) { + print "$clients{$client}{'client'}:\n"; + for my $desc (sort keys %{ $clients{$client}{'work'} }) { + print wrap(' ', ' ', $desc) . "\n"; - my $properties = $entry->property($property); - - return "" - unless defined $properties; + for my $time (sort @{ $clients{$client}{'work'}{$desc}{'times'} }) { + print detail_line($time->[0], $time->[1]); + } - if (scalar(@{ $properties }) > 0) { - return @{ $properties }[0]->value(); - } else { - return ""; + if (scalar(@{ $clients{$client}{'work'}{$desc}{'times'} }) > 1) { + print detail_line($clients{$client}{'work'}{$desc}{'hours'}, 'Total'); + } } + print "\n"; +} + +sub detail_line { + return sprintf(" %5.2f - %s\n", @_); } + -- 2.30.2