blib/lib/Bot/IRC/X/Time.pm

filter: Statementstmt 54.7% 23 / 42 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 87.5% 7 / 8 Podpod 0.0% 0 / 1 totaltotal 34.8% 30 / 86 CC 16 SCAR 52.4
CC
16
Cov
32%
__ANON__
272.0
init
1.8
BEGIN
1.0
CRAP 189.6
↑ Summary
1 package Bot::IRC::X::Time;
2 # ABSTRACT: Bot::IRC plugin for some time functions
3  
4 2 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use exact;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 ▶use Date::Parse 'str2time';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use DateTime::Format::Human::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use DateTime::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 our $VERSION = '1.07'; # VERSION
13  
14 1 ▶sub init {
Subroutine init: called CC=1 SCAR=5.8
Pod undocumented
15 1 my ($bot) = @_;
16  
17 1 my $duration = DateTime::Format::Human::Duration->new;
18 $bot->hook(
19 {
20 to_me => 1,
21 text => qr/
22 ^(?<command>date|time|zulu|str2time|str2date|when\s+(?:wa|i)s|ago)
23 \b\s*(?<input>.+)?[.,;:?!]*$
24 /ix,
25 },
26 0 ▶ sub {
Subroutine __ANON__: not called CC=16 SCAR=56.1
27 0 my ( $bot, $in, $m ) = @_;
28 0 my $command = lc( $m->{command} );
29  
30 0 my $reply = '';
31 0 ▶ if ( $command eq 'date' ) {
Branch: 7 branches
BranchTrueFalseTotal
if ($command eq 'date') { } 0 0 0
elsif ($command eq 'time') { } 0 0 0
elsif ($command eq 'zulu') { } 0 0 0
elsif ($command eq 'str2time') { } 0 0 0
elsif ($command eq 'str2date') { } 0 0 0
elsif ($command eq 'ago') { } 0 0 0
elsif ($command =~ /when\s+((?:wa|i)s)/iu) { } 0 0 0
32 0 ▶ $reply = scalar( localtime( $m->{input} || time ) );
Condition0%0/2
$m->{'input'} || time
l!l
00
MC/DC0%0/1
$m->{'input'} || time
$m->{'input'}
Truth table0%0/2
A
A= $m->{'input'}
Aresult
11
00
33 }
34 elsif ( $command eq 'time' ) {
35 0 $reply = time();
36 }
37 elsif ( $command eq 'zulu' ) {
38 0 ▶ $reply = scalar( gmtime( $m->{input} || time ) );
Condition0%0/2
$m->{'input'} || time
l!l
00
MC/DC0%0/1
$m->{'input'} || time
$m->{'input'}
Truth table0%0/2
A
A= $m->{'input'}
Aresult
11
00
39 }
40 elsif ( $command eq 'str2time' ) {
41 0 $reply = str2time( $m->{input} );
42 }
43 elsif ( $command eq 'str2date' ) {
44 0 $reply = scalar( localtime( str2time( $m->{input} ) ) );
45 }
46 elsif ( $command eq 'ago' ) {
47 $reply = scalar( localtime( DateTime->now->subtract_duration(
48 DateTime::Duration->new(
49 map {
50 0 ▶ $_ . 's' => ( ( $m->{input} =~ /\b(\d+)\s+$_/ ) ? $1 : 0 )
Branch: 1 branch
BranchTrueFalseTotal
$m->{'input'} =~ /\b(\d+)\s+$_/u ? : 0 0 0
51 } qw( year month week day hour minute second )
52 )
53 )->epoch ) );
54 }
55 elsif ( $command =~ /when\s+((?:wa|i)s)/i ) {
56 0 ▶ my $dir = ( lc($1) eq 'was' ) ? 'was' : 'is';
Branch: 1 branch
BranchTrueFalseTotal
lc $1 eq 'was' ? : 0 0 0
57 0 ▶ my $start = str2time( $m->{input} ) || $m->{input};
Condition0%0/3
str2time($m->{'input'}) || $m->{'input'}
l!l&&r!l&&!r
000
MC/DC0%0/2
str2time($m->{'input'}) || $m->{'input'}
str2time($m->{'input'}) $m->{'input'}
Truth table0%0/3
A || B
A= str2time($m->{'input'})B= $m->{'input'}
ABresult
1X1
011
000
58 0 my $dur;
59  
60 0 eval {
61 $dur = $duration->format_duration_between(
62 0 map { DateTime->from_epoch( epoch => $_ ) } $start, time
63 )
64 };
65  
66 0 ▶ $reply = scalar( localtime($start) ) . " $dir $dur " .
Branch: 2 branches
BranchTrueFalseTotal
if $dur 0 0 0
$dir eq 'was' ? : 0 0 0
67 ( ( $dir eq 'was' ) ? 'ago' : 'from now' ) . '.' if ($dur);
68 }
69  
70 0 ▶ $bot->reply_to($reply) if ($reply);
Branch: 1 branch
BranchTrueFalseTotal
if $reply 0 0 0
71 },
72 1 );
73  
74 1 $bot->helps( time =>
75 'Various time functions. Usage: ' . join( ', ',
76 'date',
77 'time',
78 'zulu',
79 'str2time <string>',
80 'str2date <string>',
81 'when was <time>|<"ago" string>',
82 'ago <time>|<string>',
83 ) . '.'
84 );
85 }
86  
87 2 1;
88  
89 __END__
↑ Summary