]> ruin.nu Git - NDIRC.git/blobdiff - Commands/PA.pm
Converted the .time command
[NDIRC.git] / Commands / PA.pm
index 2440feaffc747649557f4f7de524144d0cad0f6c..633d2fee5d9a41ea94a584843be521f56fe8df82 100644 (file)
@@ -21,6 +21,7 @@ package NDIRC::Commands::PA;
 
 use strict;
 use warnings;
+use feature ':5.10';
 
 use Moose;
 use MooseX::MethodAttributes;
@@ -81,4 +82,27 @@ FROM galaxies WHERE x = ? AND y = ? AND tick = (SELECT max(tick) from galaxies)
        }
 }
 
+sub time
+       : Help(syntax: .time [tick] [timezone] | Gives the time at the specied tick. Assumes GMT if no timezone is given and current tick if no tick is given.)
+{
+       my ($self, $c, $msg) = @_;
+       my ($tick,$timezone) = $msg =~ /^(\d+)?\s*(\S+)?$/ or die 'ARGS';
+
+       eval {
+               $tick //= $c->model->selectrow_array(q{SELECT tick()});
+               $timezone //= 'GMT';
+               my $query = $c->model->prepare(q{
+SELECT date_trunc('seconds',now() + (($1 - tick()) || ' hr')::interval) AT TIME ZONE $2
+                       });
+               $query->execute($tick,$timezone);
+               my $time = $query->fetchrow_array;
+               $c->reply("Time at tick <b>$tick</b>, timezone <b>$timezone</b>: <b>$time</b>");
+       };
+       given ($@){
+               when(/time zone "(.+?)" not recognized/){
+                       $c->reply("<c04>$1</c> is not a valid timezone.");
+               }
+               die $@ if $@;
+       }
+}
 1;