From 0a9d0532dd13a955e5adcd07be2d42ceeef2fa0c Mon Sep 17 00:00:00 2001 From: David Phillips Date: Wed, 8 Jul 2020 21:39:20 +1200 Subject: Timezone: Add tz conversion command --- Plugin/Timezone.pm | 73 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file 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