aboutsummaryrefslogtreecommitdiff
path: root/Plugin/Timezone.pm
blob: 63804659119d3b8d98df303b0aff7974c3f065df (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
43
44
45
46
47
48
49
50
51
52
package Plugin::Timezone;

use strict;
use warnings;

use DateTime;
use IdaliusConfig qw/assert_dict/;

my $config;

sub configure {
	my $self = shift;
	my $cmdref = shift;
	shift; # run_command
	$config = shift;

	IdaliusConfig::assert_dict($config, $self, "timezone");

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

	return $self;
}

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

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

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

	my $nick = $arguments[0] || $requester;

	my ($case_nick) = grep {/^$nick$/i} @known_zones;
	my $tz;
	if ($case_nick) {
		$tz = $config->{timezone}->{$case_nick};
	} else {
		$tz = $nick;
	}

	eval {
		my $d = DateTime->now();
		$d->set_time_zone($tz);
		my $timestr = $d->strftime("%H:%M on %a %d %b, %Y (%Z)");
		return "$nick\'s clock reads $timestr" if $case_nick;
		return "Clocks in $tz read $timestr";
	} or do {
		return "$requester: I'm unsure what the time is for $nick";
	}
}
1;