summaryrefslogtreecommitdiff
path: root/unixtime.pl
blob: e75a786d65a0df0afcc628e0d14a025c9182b63e (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
#!/usr/bin/perl
use POSIX qw( strftime );
use Time::HiRes qw( gettimeofday );

#Handle IRC input, multiple words is received as one argument
if (@ARGV eq 1) {
    @args = split(/ /, $ARGV[0]);
} else {
    @args = @ARGV;
}

if (defined $args[0] and $args[0] ne "") {
    if ($args[1] eq "ms") {
        #Convert from timestamp in milliseconds to date
        $seconds = substr $args[0], 0, -3;
        $milliseconds = substr $args[0], -3;
        $date =  strftime('%a %b %e %H:%M:%S:' . $milliseconds . ' %Z %Y', localtime( $seconds ) ) . "\n";
        print $date;
    } elsif ($args[0] eq "ms") {
        #Print current millisecond unix timestamp
        ($seconds, $microseconds) = gettimeofday;
        $milliseconds = substr $microseconds, 0, 3;
        print $seconds . $milliseconds . "\n";
    } else {
        #Convert standard unix timestamp to date
        print strftime('%a %b %e %H:%M:%S %Z %Y', localtime( $args[0] ) ) . "\n";
    }
} else {
    #Print current standard unix timestamp
    print time . "\n";
}