aboutsummaryrefslogtreecommitdiff
path: root/Plugin/Timezone.pm
blob: eb953a174cc0f3d6e84e1f70721f9770529c3226 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env perl

package Plugin::Timezone;

use strict;
use warnings;

use DateTime;

my %config;

sub configure {
	my $self = $_[0];
	my $cmdref = $_[1];
	my $cref = $_[2];
	%config = %$cref;

	$cmdref->("time", sub { $self->time(@_); } );

	return $self;
}

sub time {
	my ($self, $irc, $logger, $who, $where, $rest, @arguments) = @_;

	my $requester = (split /!/, $who)[0];
	my @known_zones = (keys %{$config{timezone}});

	return "Syntax: time [nick]" unless @arguments == 1;

	my $nick = $arguments[0];
	my ($case_nick) = grep {/^$nick$/i} @known_zones;
	if ($case_nick) {
		my $d = DateTime->now();
		$d->set_time_zone($config{timezone}->{$case_nick});
		my $timestr = $d->strftime("%Y-%m-%d %H:%M %Z");
		return "$requester: $nick\'s clock reads $timestr";
	} else {
		return "$requester: I don't know what timezone $nick is in";
	}
}
1;