root/lang/perl/Morris/trunk/lib/Morris/Plugin/Channel/Time.pm @ 26865

Revision 26865, 1.3 kB (checked in by daisuke, 5 years ago)

epoch

Line 
1# $Id$
2
3package Morris::Plugin::Channel::Time;
4use Moose;
5use DateTime;
6
7with 'Morris::Plugin';
8
9__PACKAGE__->meta->make_immutable;
10
11no Moose;
12
13sub register {
14    my ($self, $conn) = @_;
15    $conn->register_hook( 'channel.public', sub { $self->handle_message(@_) } );
16}
17
18sub 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
531;
Note: See TracBrowser for help on using the browser.