File Coverage

blib/lib/Bot/BasicBot/Pluggable/Module/DateTimeCalc.pm
Criterion Covered Total %
statement 18 54 33.3
branch 0 18 0.0
condition n/a
subroutine 6 10 60.0
pod 2 2 100.0
total 26 84 30.9


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