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.0303'; |
7
|
|
|
|
|
|
|
|
8
|
1
|
|
|
1
|
|
616
|
use strict; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
24
|
|
9
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
24
|
|
10
|
|
|
|
|
|
|
|
11
|
1
|
|
|
1
|
|
4
|
use base qw(Bot::BasicBot::Pluggable); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
425
|
|
12
|
|
|
|
|
|
|
|
13
|
1
|
|
|
1
|
|
284908
|
use Date::Manip; |
|
1
|
|
|
|
|
108350
|
|
|
1
|
|
|
|
|
134
|
|
14
|
1
|
|
|
1
|
|
715
|
use DateTime; |
|
1
|
|
|
|
|
385669
|
|
|
1
|
|
|
|
|
43
|
|
15
|
1
|
|
|
1
|
|
481
|
use DateTime::Format::DateParse; |
|
1
|
|
|
|
|
6398
|
|
|
1
|
|
|
|
|
513
|
|
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
|
|
|
|
|
|
|
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 $format = '%Y-%m-%dT%H:%M:%S'; |
107
|
0
|
|
|
|
|
|
my $stamp = UnixDate( $capture, $format ); |
108
|
0
|
|
|
|
|
|
my $dt = DateTime::Format::DateParse->parse_datetime($stamp); |
109
|
0
|
|
|
|
|
|
return $dt; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
1; |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
__END__ |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=pod |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=encoding UTF-8 |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=head1 NAME |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
Bot::BasicBot::Pluggable::Module::DateTimeCalc - Calculate date-time operations |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head1 VERSION |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
version 0.0303 |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head1 SYNOPSIS |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
use Bot::BasicBot::Pluggable::Module::DateTimeCalc; |
132
|
|
|
|
|
|
|
my $bot = Bot::BasicBot::Pluggable::Module::DateTimeCalc->new( |
133
|
|
|
|
|
|
|
server => 'irc.somewhere.org', |
134
|
|
|
|
|
|
|
port => '6667', |
135
|
|
|
|
|
|
|
channels => ['#bots'], |
136
|
|
|
|
|
|
|
nick => 'TimeBot', |
137
|
|
|
|
|
|
|
name => 'Your Name Bot', |
138
|
|
|
|
|
|
|
ignore_list => [qw/other_bot some_fool/], |
139
|
|
|
|
|
|
|
); |
140
|
|
|
|
|
|
|
$bot->run(); |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=head1 DESCRIPTION |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
A C<Bot::BasicBot::Pluggable::Module::DateTimeCalc> calculates date-time |
145
|
|
|
|
|
|
|
operations. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
This bot is coded to only respond when directly addressed, btw. |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
Since this module uses L<Date::Manip>, many different date-time formats are |
150
|
|
|
|
|
|
|
supported. |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
=head1 METHODS |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
=head2 new() |
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
Create a new C<Bot::BasicBot::Pluggable::Module::DateTimeCalc> object. |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head2 help() |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
Show the keyword help message. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=head2 said() |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
Process the date-time calculations. |
165
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
=head1 IRC COMMANDS |
167
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
=head2 help |
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
> TimeBot: help |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
Show the keyword help message. |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
=head2 source |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
> TimeBot: source |
177
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Return the github repository where this is hosted. |
179
|
|
|
|
|
|
|
|
180
|
|
|
|
|
|
|
=head2 now |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
> TimeBot: now |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
Return the current date and time. |
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
=head2 localtime |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
> TimeBot: localtime 123456 |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
Return the date-time string given an epoch time. |
191
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
=head2 dow |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
> TimeBot: dow 2018-06-24 |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
Return the day of the week for the given date-time stamp. |
197
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
=head2 diff |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
> TimeBot: diff '2018-06-24 17:51:17' '1/2/2032' |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
Return a duration string in days, hours, minutes and seconds from two date-time |
203
|
|
|
|
|
|
|
stamps. |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head2 {add,sub}_{years,months,days,hours,minutes,seconds} |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
> TimeBot: add_days 3 '1/2/2032' |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
Add or subtract the the given span from the given date-time stamp. |
210
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
=head2 leave |
212
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
> TimeBot: leave |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
Exit the IRC and the running process. |
216
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
=head1 SEE ALSO |
218
|
|
|
|
|
|
|
|
219
|
|
|
|
|
|
|
L<Bot::BasicBot::Pluggable> |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
L<Date::Manip> |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
L<DateTime> |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
L<DateTime::Format::DateParse> |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head1 AUTHOR |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
Gene Boggs <gene@cpan.org> |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
232
|
|
|
|
|
|
|
|
233
|
|
|
|
|
|
|
This software is copyright (c) 2018 by Gene Boggs. |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
236
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=cut |