Get name of active user in script on Mac OS
I don't know how to get time since switch, but you can get the currently active user by checking ownership on /dev/console. There's probably a better way in Perl, but the stat command'll do it:
my $user = `stat -f%Su /dev/console`;