Skip to content

Instantly share code, notes, and snippets.

@karupanerura
Last active May 1, 2023 10:28
Show Gist options
  • Save karupanerura/9b7e43c8779c903f0b776ae9746439c7 to your computer and use it in GitHub Desktop.
Save karupanerura/9b7e43c8779c903f0b776ae9746439c7 to your computer and use it in GitHub Desktop.
use strict;
use warnings;
use feature qw/say/;
use JSON::PP;
use Time::Piece;
use POSIX qw/floor/;
my $events = do {
open my $fh, '<', 'events.json'
or die "$!: events.json";
local $/; # slurp
JSON::PP::decode_json(<$fh>);
};
my $n_month_ago = $ARGV[0] || 1;
sub parse_datetime {
my $datetime = shift;
my $except_offset = substr $datetime->{dateTime}, 0, length("2022-05-11T13:06:00");
local $ENV{TZ} = $datetime->{timeZone};
return Time::Piece->localtime->strptime($except_offset, '%Y-%m-%dT%H:%M:%S');
}
my $this_month = Time::Piece->localtime()->truncate(to => 'month');
my $last_month = $this_month->add_months(-$n_month_ago);
say "RANGE: @{[ $last_month->strftime('%Y-%m-%dT%H:%M:%S%z') ]} ~ @{[ $this_month->strftime('%Y-%m-%dT%H:%M:%S%z') ]}";
my $sum_duration = 0;
for my $item (@{ $events->{items} }) {
my $start_at = parse_datetime($item->{start});
my $end_at = parse_datetime($item->{end});
next unless $last_month <= $start_at < $this_month;
my $duration = $end_at - $start_at;
my $hours = floor($duration / 3600);
my $minutes = floor(($duration % 3600) / 60);
$minutes = floor($minutes / 10) * 10; # 10分未満切り捨て
say sprintf 'WORK: %02d:%02d (%s ~ %s)', $hours, $minutes, $start_at->strftime('%Y-%m-%dT%H:%M:%S%z'), $end_at->strftime('%Y-%m-%dT%H:%M:%S%z');
$sum_duration += $hours * 3600 + $minutes * 60;
}
my $sum_hours = floor($sum_duration / 3600);
my $sum_minutes = floor(($sum_duration % 3600) / 60);
say sprintf "SUMMARY: %02d hours %02d minutes in @{[ $n_month_ago == 1 ? 'a last month' : 'last '.$n_month_ago.' months' ]}", $sum_hours, $sum_minutes;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment