Skip to content

Instantly share code, notes, and snippets.

@jajm
Last active January 29, 2016 13:49
Show Gist options
  • Save jajm/8236268 to your computer and use it in GitHub Desktop.
Save jajm/8236268 to your computer and use it in GitHub Desktop.
caltool.pl
#!/usr/bin/perl
use Modern::Perl;
use DateTime;
use Date::Calc qw/Monday_of_Week This_Year Add_Delta_Days/;
use FindBin qw/$Bin/;
use Data::Dumper;
my $weekno = $ARGV[0] or die "give a week number!";
my $year = (@ARGV > 1) ? $ARGV[1] : This_Year();
my ($year1, $month1, $day1) = Monday_of_Week($weekno, $year);
my ($year2, $month2, $day2) = Add_Delta_Days($year1, $month1, $day1, 5);
my $monday = "$year1-$month1-$day1";
my $saturday = "$year2-$month2-$day2";
my @events;
my @lines = qx/gcalcli --tsv agenda $monday $saturday/;
foreach my $line (@lines) {
chomp $line;
my ($date_start, $time_start, $date_end, $time_end, $desc) = split /\t/, $line;
push @events, {
date_start => $date_start,
time_start => $time_start,
date_end => $date_end,
time_end => $time_end,
desc => $desc,
};
}
my %h;
foreach my $event (@events) {
my $date_start = $event->{date_start};
my $time_start = $event->{time_start};
my $date_end = $event->{date_end};
my $time_end = $event->{time_end};
my $desc = $event->{desc};
my ($year, $month, $day) = split /-/, $date_start;
my ($hour, $minute) = split /:/, $time_start;
my $dt_start = DateTime->new(
year => $year,
month => $month,
day => $day,
hour => $hour,
minute => $minute,
);
($year, $month, $day) = split /-/, $date_end;
($hour, $minute) = split /:/, $time_end;
my $dt_end = DateTime->new(
year => $year,
month => $month,
day => $day,
hour => $hour,
minute => $minute,
);
my $duration = $dt_end - $dt_start;
if (not exists $h{$desc}) {
$h{$desc} = $duration;
} else {
$h{$desc} = $h{$desc} + $duration;
}
}
foreach my $desc (sort { lc($a) cmp lc($b) } keys %h) {
my ($days, $hours, $minutes) = $h{$desc}->in_units('days', 'hours', 'minutes');
say "$desc";
print "\t";
my $time_str = '';
if ($days) {
$time_str .= ", $days jours";
}
if ($hours) {
$time_str .= ", $hours heures";
}
if ($minutes) {
$time_str .= ", $minutes minutes";
}
$time_str = substr($time_str, 1);
say $time_str;
say "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment