|
Revision 26865, 1.3 kB
(checked in by daisuke, 4 years ago)
|
|
epoch
|
| Line | |
|---|
| 1 | # $Id$ |
|---|
| 2 | |
|---|
| 3 | package Morris::Plugin::Channel::Time; |
|---|
| 4 | use Moose; |
|---|
| 5 | use DateTime; |
|---|
| 6 | |
|---|
| 7 | with 'Morris::Plugin'; |
|---|
| 8 | |
|---|
| 9 | __PACKAGE__->meta->make_immutable; |
|---|
| 10 | |
|---|
| 11 | no Moose; |
|---|
| 12 | |
|---|
| 13 | sub register { |
|---|
| 14 | my ($self, $conn) = @_; |
|---|
| 15 | $conn->register_hook( 'channel.public', sub { $self->handle_message(@_) } ); |
|---|
| 16 | } |
|---|
| 17 | |
|---|
| 18 | sub handle_message { |
|---|
| 19 | my ($self, $args) = @_; |
|---|
| 20 | |
|---|
| 21 | my $message = $args->{message}->message; |
|---|
| 22 | my $channel = $args->{message}->channel; |
|---|
| 23 | my $quote; |
|---|
| 24 | |
|---|
| 25 | if ( $message =~ m{^\s*(epoch|time)(?:\s*(\+)\s*(.+))?$} ) { |
|---|
| 26 | my $dt; |
|---|
| 27 | my $type = $1; |
|---|
| 28 | |
|---|
| 29 | $dt = DateTime->now(time_zone => 'Asia/Tokyo'); |
|---|
| 30 | if ($2 eq '+') { |
|---|
| 31 | my $mode = 'add'; |
|---|
| 32 | my $string = $3; |
|---|
| 33 | my ($unit, $amount); |
|---|
| 34 | while ($string =~ /\G\s*(\d+)\s+(year|month|day|hour|min(?:ute)?|sec(?:ond)?)s?/g) { |
|---|
| 35 | ($unit, $amount) = ($2, $1); |
|---|
| 36 | if ($unit eq 'min') { |
|---|
| 37 | $unit = "minute"; |
|---|
| 38 | } elsif ($unit eq 'sec') { |
|---|
| 39 | $unit = "second"; |
|---|
| 40 | } |
|---|
| 41 | $dt->$mode("${unit}s" => $amount); |
|---|
| 42 | } |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | $self->connection->irc_notice({ |
|---|
| 47 | channel => $channel, |
|---|
| 48 | message => $type eq 'epoch' ? $dt->epoch : $dt->strftime('%Y-%m-%d %H:%M:%S') |
|---|
| 49 | }); |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | 1; |
|---|