aboutsummaryrefslogtreecommitdiff
path: root/Plugin
diff options
context:
space:
mode:
authorDavid Phillips <david@yeah.nah.nz>2020-07-08 21:39:20 +1200
committerDavid Phillips <david@yeah.nah.nz>2020-07-08 21:39:20 +1200
commit0a9d0532dd13a955e5adcd07be2d42ceeef2fa0c (patch)
tree40383e057fc1beffa5454b71bdcaad9b11a70466 /Plugin
parentcd62457f66c80ff5f0e8643910f094e54cea06e2 (diff)
downloadidalius-0a9d0532dd13a955e5adcd07be2d42ceeef2fa0c.tar.xz
Timezone: Add tz conversion command
Diffstat (limited to 'Plugin')
-rw-r--r--Plugin/Timezone.pm73
1 files changed, 63 insertions, 10 deletions
diff --git a/Plugin/Timezone.pm b/Plugin/Timezone.pm
index 6380465..947bc7a 100644
--- a/Plugin/Timezone.pm
+++ b/Plugin/Timezone.pm
@@ -17,36 +17,89 @@ sub configure {
IdaliusConfig::assert_dict($config, $self, "timezone");
$cmdref->($self, "time", sub { $self->time(@_); } );
+ $cmdref->($self, "tz", sub { $self->zone_convert(@_); } );
return $self;
}
+# look a nick up and return their configured timezone, else return the input
+# assuming it's a timezone rather than a nick
+sub nick_to_zone {
+ my ($nick) = @_;
+ my @known_zones = (keys %{$config->{timezone}});
+ my ($case_nick) = grep {/^\Q$nick\E$/i} @known_zones;
+ my $tz;
+ if ($case_nick) {
+ return $config->{timezone}->{$case_nick};
+ } else {
+ return $nick;
+ }
+}
+
+sub clock_message {
+ my ($nick, $tz, $time_string) = @_;
+ if ($tz eq $nick) {
+ return "clocks in $tz read $time_string";
+ } else {
+ return "$nick\'s clock reads $time_string";
+ }
+}
+
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;
- }
+ my $tz = nick_to_zone($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";
+ return clock_message($nick, $tz, $timestr);
} or do {
return "$requester: I'm unsure what the time is for $nick";
}
}
+
+sub zone_convert {
+ my ($self, $irc, $logger, $who, $where, $ided, $rest, $no_reenter, @arguments) = @_;
+
+ # tz 1200 from to
+ # tz 1200 from
+
+ my $requester = (split /!/, $who)[0];
+ return "Syntax: tz <time> <from> [to]" unless @arguments == 2 || @arguments == 3;
+
+ my ($time_string, $from_arg, $to_arg) = @arguments;
+
+ my $from_tz = nick_to_zone($from_arg);
+ $to_arg = $requester unless $to_arg;
+ my $to_tz = nick_to_zone($to_arg);
+
+ my ($hour, $minute) = $time_string =~ /^([0-9]{1,2}):([0-9]{1,2})/;
+
+ eval {
+ my $d = DateTime->now();
+ my $format = "%H:%M on %a %d %b, %Y (%Z)";
+ $d->set_time_zone($from_tz);
+ $d->set(
+ hour => $hour,
+ minute => $minute,
+ );
+ my $unconverted = $d->strftime($format);
+ $d->set_time_zone($to_tz);
+ my $converted = $d->strftime($format);
+ my $from_message = clock_message($from_arg, $from_tz, $unconverted);
+ my $to_message = clock_message($to_arg, $to_tz, $converted);
+ return "$to_message when $from_message";
+ } or do {
+ return "$requester: I can't figure this time conversion out";
+ }
+
+}
1;