From dcb2720df0a410bdef8714ef983a8d860009c969 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Wed, 12 Jul 2017 15:21:31 +0100 Subject: Handle argument being an empty string in the same way as undefined --- unixtime.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unixtime.pl b/unixtime.pl index f47cf9f..ca99311 100755 --- a/unixtime.pl +++ b/unixtime.pl @@ -2,7 +2,7 @@ use POSIX qw( strftime ); use Time::HiRes qw( gettimeofday ); -if (defined $ARGV[0]) { +if (defined $ARGV[0] and $ARGV ne "") { if ($ARGV[1] eq "ms") { #Convert from timestamp in milliseconds to date $seconds = substr $ARGV[0], 0, 10; -- cgit v1.2.3 From ec1620e57b8ea542ccd8e703539bc14c3e2cf40e Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Wed, 12 Jul 2017 15:35:56 +0100 Subject: Handle IRC single argument input --- unixtime.pl | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/unixtime.pl b/unixtime.pl index ca99311..d6fff26 100755 --- a/unixtime.pl +++ b/unixtime.pl @@ -2,21 +2,28 @@ use POSIX qw( strftime ); use Time::HiRes qw( gettimeofday ); -if (defined $ARGV[0] and $ARGV ne "") { - if ($ARGV[1] eq "ms") { +#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 $ARGV[0], 0, 10; - $milliseconds = substr $ARGV[0], 10, 3; + $seconds = substr $args[0], 0, 10; + $milliseconds = substr $args[0], 10, 3; $date = strftime('%a %b %e %H:%M:%S:' . $milliseconds . ' %Z %Y', localtime( $seconds ) ) . "\n"; print $date; - } elsif ($ARGV[0] eq "ms") { + } 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( $ARGV[0] ) ) . "\n"; + print strftime('%a %b %e %H:%M:%S %Z %Y', localtime( $args[0] ) ) . "\n"; } } else { #Print current standard unix timestamp -- cgit v1.2.3 From 140c8fc8020f677c9326103a7cb718ae482e0226 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Wed, 12 Jul 2017 15:36:45 +0100 Subject: Improve handling of milliseconds to handle any length --- unixtime.pl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/unixtime.pl b/unixtime.pl index d6fff26..e75a786 100755 --- a/unixtime.pl +++ b/unixtime.pl @@ -12,8 +12,8 @@ if (@ARGV eq 1) { 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, 10; - $milliseconds = substr $args[0], 10, 3; + $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") { -- cgit v1.2.3