File Coverage

blib/lib/Bot/IRC/X/Time.pm
Criterion Covered Total %
statement 21 40 52.5
branch 0 24 0.0
condition 0 9 0.0
subroutine 7 8 87.5
pod 0 1 0.0
total 28 82 34.1


line stmt bran cond sub pod time code
1             package Bot::IRC::X::Time;
2             # ABSTRACT: Bot::IRC plugin for some time functions
3              
4 1     1   353511 use 5.014;
  1         10  
5 1     1   4 use exact;
  1         2  
  1         5  
6              
7 1     1   1076 use Date::Parse 'str2time';
  1         2318  
  1         69  
8 1     1   750 use DateTime;
  1         378853  
  1         46  
9 1     1   589 use DateTime::Format::Human::Duration;
  1         1818  
  1         34  
10 1     1   7 use DateTime::Duration;
  1         2  
  1         683  
11              
12             our $VERSION = '1.02'; # VERSION
13              
14             sub init {
15 1     1 0 5960 my ($bot) = @_;
16              
17 1         15 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             sub {
27 0     0   0 my ( $bot, $in, $m ) = @_;
28 0         0 my $command = lc( $m->{command} );
29              
30 0         0 my $reply = '';
31 0 0       0 if ( $command eq 'date' ) {
    0          
    0          
    0          
    0          
    0          
    0          
32 0   0     0 $reply = scalar( localtime( $m->{input} || time ) );
33             }
34             elsif ( $command eq 'time' ) {
35 0         0 $reply = time();
36             }
37             elsif ( $command eq 'zulu' ) {
38 0   0     0 $reply = scalar( gmtime( $m->{input} || time ) );
39             }
40             elsif ( $command eq 'str2time' ) {
41 0         0 $reply = str2time( $m->{input} );
42             }
43             elsif ( $command eq 'str2date' ) {
44 0         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 0       0 $_ . 's' => ( ( $m->{input} =~ /\b(\d+)\s+$_/ ) ? $1 : 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 0       0 my $dir = ( lc($1) eq 'was' ) ? 'was' : 'is';
57 0   0     0 my $start = str2time( $m->{input} ) || $m->{input};
58 0         0 my $dur;
59              
60 0         0 eval {
61             $dur = $duration->format_duration_between(
62 0         0 map { DateTime->from_epoch( epoch => $_ ) } $start, time
  0         0  
63             )
64             };
65              
66 0 0       0 $reply = scalar( localtime($start) ) . " $dir $dur " .
    0          
67             ( ( $dir eq 'was' ) ? 'ago' : 'from now' ) . '.' if ($dur);
68             }
69              
70 0 0       0 $bot->reply_to($reply) if ($reply);
71             },
72 1         17 );
73              
74 1         11 $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             1;
88              
89             __END__
90              
91             =pod
92              
93             =encoding UTF-8
94              
95             =head1 NAME
96              
97             Bot::IRC::X::Time - Bot::IRC plugin for some time functions
98              
99             =head1 VERSION
100              
101             version 1.02
102              
103             =for markdown [![test](https://github.com/gryphonshafer/Bot-IRC-X-Time/workflows/test/badge.svg)](https://github.com/gryphonshafer/Bot-IRC-X-Time/actions?query=workflow%3Atest)
104             [![codecov](https://codecov.io/gh/gryphonshafer/Bot-IRC-X-Time/graph/badge.svg)](https://codecov.io/gh/gryphonshafer/Bot-IRC-X-Time)
105              
106             =head1 SYNOPSIS
107              
108             use Bot::IRC;
109              
110             Bot::IRC->new(
111             connect => { server => 'irc.perl.org' },
112             plugins => ['Time'],
113             )->run;
114              
115             =head1 DESCRIPTION
116              
117             This L<Bot::IRC> plugin provides some time functions. The following are the
118             details:
119              
120             =head2 date [<time>]
121              
122             Returns the current date and time. If an optional timestamp is provided, it'll
123             return the formatted data and time for that timestamp.
124              
125             =head2 time
126              
127             Returns the current timestamp.
128              
129             =head2 zulu
130              
131             Exactly like C<date> except will return Zulu time.
132              
133             =head2 str2time <string>
134              
135             Accepts most reasonable date and time strings and returns timestamps for those
136             values.
137              
138             =head2 str2date <string>
139              
140             Accepts most reasonable date and time strings and returns a formatted date and
141             time string.
142              
143             =head2 when was <time>|<string>
144              
145             Accepts a timestamp or a date and time string and returns a duration string
146             like: "1 year, 2 months, and 4 hours"
147              
148             =head2 ago <duration string>
149              
150             Accepts a duration string like what would be returned from "when was" and
151             returns a formatted date and time string.
152              
153             =head1 SEE ALSO
154              
155             You can look for additional information at:
156              
157             =over 4
158              
159             =item *
160              
161             L<Bot::IRC>
162              
163             =item *
164              
165             L<GitHub|https://github.com/gryphonshafer/Bot-IRC-X-Time>
166              
167             =item *
168              
169             L<MetaCPAN|https://metacpan.org/pod/Bot::IRC::X::Time>
170              
171             =item *
172              
173             L<GitHub Actions|https://github.com/gryphonshafer/Bot-IRC-X-Time/actions>
174              
175             =item *
176              
177             L<Codecov|https://codecov.io/gh/gryphonshafer/Bot-IRC-X-Time>
178              
179             =item *
180              
181             L<CPANTS|http://cpants.cpanauthors.org/dist/Bot-IRC-X-Time>
182              
183             =item *
184              
185             L<CPAN Testers|http://www.cpantesters.org/distro/T/Bot-IRC-X-Time.html>
186              
187             =back
188              
189             =for Pod::Coverage init
190              
191             =head1 AUTHOR
192              
193             Gryphon Shafer <gryphon@cpan.org>
194              
195             =head1 COPYRIGHT AND LICENSE
196              
197             This software is Copyright (c) 2016-2021 by Gryphon Shafer.
198              
199             This is free software, licensed under:
200              
201             The Artistic License 2.0 (GPL Compatible)
202              
203             =cut