File Coverage

blib/lib/Bot/BasicBot/Pluggable/Module/DateTimeCalc.pm
Criterion Covered Total %
statement 15 50 30.0
branch 0 18 0.0
condition n/a
subroutine 5 9 55.5
pod 2 2 100.0
total 22 79 27.8


line stmt bran cond sub pod time code
1             package Bot::BasicBot::Pluggable::Module::DateTimeCalc;
2             our $AUTHORITY = 'cpan:GENE';
3              
4             # ABSTRACT: Calculate date-time operations
5              
6             our $VERSION = '0.0400';
7              
8 1     1   805 use strict;
  1         3  
  1         30  
9 1     1   6 use warnings;
  1         3  
  1         29  
10              
11 1     1   6 use base qw(Bot::BasicBot::Pluggable);
  1         2  
  1         585  
12              
13 1     1   377730 use DateTime;
  1         513503  
  1         50  
14 1     1   618 use DateTime::Format::Natural;
  1         42917  
  1         639  
15              
16              
17              
18             sub help {
19 0     0 1   my( $self, $arguments ) = @_;
20              
21             $self->say(
22             channel => $arguments->{channel},
23 0           body => 'source|now|localtime $epoch|dow $stamp|diff $stamp $stamp|{add,sub}_{years,months,days,hours,minutes,seconds} $offset $stamp',
24             );
25             }
26              
27              
28             sub said {
29 0     0 1   my $self = shift;
30 0           my $arguments = shift;
31              
32 0           my $body = '?';
33              
34 0           my $re = qr/(\S+(?:\s+[\d:]+['"])?)/;
35              
36 0 0         if ( $arguments->{address} ) {
37             # Return the source code link
38 0 0         if ( $arguments->{body} =~ /^source$/ ) {
    0          
    0          
    0          
    0          
    0          
    0          
39 0           $body = 'https://github.com/ology/Bot-BasicBot-Pluggable-Module-DateTimeCalc';
40             }
41             # Return the current time
42             elsif ( $arguments->{body} =~ /^now$/ ) {
43 0           $body = DateTime->now( time_zone => 'local' );
44             }
45             # Return the localtime string of a given timestamp
46             elsif ( $arguments->{body} =~ /^localtime (\d+)$/ ) {
47 0           $body = scalar localtime $1;
48             }
49             # Return the day of the week of a given timestamp
50             elsif ( $arguments->{body} =~ /^dow $re$/ ) {
51 0           my $capture = _capture($1);
52              
53 0           my $dt = _to_dt($capture);
54              
55 0           $body = $dt->day_name;
56             }
57             # Return the difference between two given timestamps
58             elsif ( $arguments->{body} =~ /^diff $re $re$/ ) {
59 0           my $capture1 = _capture($1);
60 0           my $capture2 = _capture($2);
61              
62 0           my $dt1 = _to_dt($capture1);
63 0           my $dt2 = _to_dt($capture2);
64              
65 0           $body = sprintf '%.2fd or %dh %dm %ds',
66             $dt1->delta_ms($dt2)->hours / 24 + $dt1->delta_ms($dt2)->minutes / 1440 + $dt1->delta_ms($dt2)->seconds / 86400,
67             $dt1->delta_ms($dt2)->hours,
68             $dt1->delta_ms($dt2)->minutes,
69             $dt1->delta_ms($dt2)->seconds;
70             }
71             # Return the addition or subtraction of the given span and offset from the given timestamp
72             elsif ( $arguments->{body} =~ /^([a-zA-Z]+)_([a-zA-Z]+) (\d+) $re$/ ) {
73 0           my $method = $1;
74 0           my $span = $2;
75              
76 0 0         $method = 'subtract' if $method eq 'sub';
77              
78 0           my $capture = _capture($4);
79              
80 0           my $dt = _to_dt($capture);
81              
82 0           $body = $dt->$method( $span => $3 );
83             }
84             # Exit IRC
85             elsif ( $arguments->{body} =~ /^leave$/ ) {
86 0           $self->shutdown( $self->quit_message );
87 0           exit;
88             }
89              
90             $self->say(
91             channel => $arguments->{channel},
92 0           body => $body,
93             );
94             }
95             }
96              
97              
98             sub _capture {
99 0     0     my ($string) = @_;
100 0           $string =~ s/['"]//g;
101 0           return $string;
102             }
103              
104             sub _to_dt {
105 0     0     my($capture) = @_;
106 0           my $parser = DateTime::Format::Natural->new;
107 0           my $dt = $parser->parse_datetime($capture);
108 0           return $dt;
109             }
110              
111              
112             1;
113              
114             __END__
115              
116             =pod
117              
118             =encoding UTF-8
119              
120             =head1 NAME
121              
122             Bot::BasicBot::Pluggable::Module::DateTimeCalc - Calculate date-time operations
123              
124             =head1 VERSION
125              
126             version 0.0400
127              
128             =head1 SYNOPSIS
129              
130             use Bot::BasicBot::Pluggable::Module::DateTimeCalc;
131             my $bot = Bot::BasicBot::Pluggable::Module::DateTimeCalc->new(
132             server => 'irc.somewhere.org',
133             port => '6667',
134             channels => ['#bots'],
135             nick => 'TimeBot',
136             name => 'Your Name Bot',
137             ignore_list => [qw/other_bot some_fool/],
138             );
139             $bot->run;
140              
141             =head1 DESCRIPTION
142              
143             A C<Bot::BasicBot::Pluggable::Module::DateTimeCalc> calculates date-time
144             operations.
145              
146             This bot is coded to only respond when directly addressed, btw.
147              
148             Since this module uses L<DateTime::Format::Natural>, many different
149             date-time formats are supported.
150              
151             =head1 METHODS
152              
153             =head2 new
154              
155             Create a new C<Bot::BasicBot::Pluggable::Module::DateTimeCalc> object.
156              
157             =head2 help
158              
159             Show the keyword help message.
160              
161             =head2 said
162              
163             Process the date-time calculations.
164              
165             =head2 run
166              
167             Start the process and connect to the IRC.
168              
169             =head1 IRC COMMANDS
170              
171             =head2 help
172              
173             > TimeBot: help
174              
175             Show the keyword help message.
176              
177             =head2 source
178              
179             > TimeBot: source
180              
181             Return the github repository where this is hosted.
182              
183             =head2 now
184              
185             > TimeBot: now
186              
187             Return the current date and time.
188              
189             =head2 localtime
190              
191             > TimeBot: localtime 123456
192              
193             Return the date-time string given an epoch time.
194              
195             =head2 dow
196              
197             > TimeBot: dow 2018-06-24
198              
199             Return the day of the week for the given date-time stamp.
200              
201             =head2 diff
202              
203             > TimeBot: diff '2018-06-24 17:51:17' '1/2/2032'
204              
205             Return a duration string in days, hours, minutes and seconds from two date-time
206             stamps.
207              
208             =head2 {add,sub}_{years,months,days,hours,minutes,seconds}
209              
210             > TimeBot: add_days 3 '1/2/2032'
211              
212             Add or subtract the the given span from the given date-time stamp.
213              
214             =head2 leave
215              
216             > TimeBot: leave
217              
218             Exit the IRC and the running process.
219              
220             =head1 SEE ALSO
221              
222             L<Bot::BasicBot::Pluggable>
223              
224             L<DateTime>
225              
226             L<DateTime::Format::Natural>
227              
228             =head1 AUTHOR
229              
230             Gene Boggs <gene@cpan.org>
231              
232             =head1 COPYRIGHT AND LICENSE
233              
234             This software is copyright (c) 2019 by Gene Boggs.
235              
236             This is free software; you can redistribute it and/or modify it under
237             the same terms as the Perl 5 programming language system itself.
238              
239             =cut