| 1 | package Bot::BasicBot::Pluggable::Module::DateTimeCalc; | ||||||||||||||||||||||||||||||||||
| 2 | 2 | our $AUTHORITY = 'cpan:GENE'; | |||||||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||||||||
| 4 | # ABSTRACT: Calculate date-time operations | ||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||
| 6 | 2 | our $VERSION = '0.0400'; | |||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use base qw(Bot::BasicBot::Pluggable); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use DateTime; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 14 | 2 | ▶ | use DateTime::Format::Natural; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | 0 | ▶ | sub help { | ||||||||||||||||||||||||||||||||
|
Subroutine
help: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 19 | 0 | 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 | 0 | ▶ | sub said { | ||||||||||||||||||||||||||||||||
|
Subroutine
said: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||||
| 29 | 0 | 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 | ▶ | if ( $arguments->{address} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 37 | # Return the source code link | ||||||||||||||||||||||||||||||||||
| 38 | 0 | ▶ | if ( $arguments->{body} =~ /^source$/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 7 branches
| |||||||||||||||||||||||||||||||||||
| 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 | ▶ | $method = 'subtract' if $method eq 'sub'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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 | 0 | ▶ | sub _capture { | ||||||||||||||||||||||||||||||||
|
Subroutine
_capture: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 99 | 0 | my ($string) = @_; | |||||||||||||||||||||||||||||||||
| 100 | 0 | $string =~ s/['"]//g; | |||||||||||||||||||||||||||||||||
| 101 | 0 | return $string; | |||||||||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||
| 104 | 0 | ▶ | sub _to_dt { | ||||||||||||||||||||||||||||||||
|
Subroutine
_to_dt: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||
| 105 | 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 | 2 | 1; | |||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||
| 114 | __END__ | ||||||||||||||||||||||||||||||||||