| 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
| |||||||||||||||||||||||||||||||||||
| 32 | 0 | ▶ | $reply = scalar( localtime( $m->{input} || time ) ); | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$m->{'input'} || time
MC/DC0%0/1
$m->{'input'} || time
$m->{'input'}
Truth table0%0/2
A
A= $m->{'input'}
| |||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$m->{'input'} || time
$m->{'input'}
Truth table0%0/2
A
A= $m->{'input'}
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 57 | 0 | ▶ | my $start = str2time( $m->{input} ) || $m->{input}; | ||||||||||||||||||||||||||||||||
|
Condition0%0/3
str2time($m->{'input'}) || $m->{'input'}
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'}
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 67 | ( ( $dir eq 'was' ) ? 'ago' : 'from now' ) . '.' if ($dur); | ||||||||||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||||||||
| 70 | 0 | ▶ | $bot->reply_to($reply) if ($reply); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||