From a46542b43eb800d3d807e1ce6c1e15482a113684 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Wed, 12 Jul 2017 14:22:38 +0100 Subject: Add options to convert and print milliseconds, and to show the current unixtime --- unixtime.pl | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'unixtime.pl') diff --git a/unixtime.pl b/unixtime.pl index 08a987a..d6bb1d8 100755 --- a/unixtime.pl +++ b/unixtime.pl @@ -1,4 +1,21 @@ #!/usr/bin/perl use POSIX qw( strftime ); +use Time::HiRes qw( gettimeofday ); + +if (defined $ARGV[0]) { + if ($ARGV[1] eq "ms") { + $seconds = substr $ARGV[0], 0, 10; + $milliseconds = substr $ARGV[0], 10, 3; + $date = strftime('%a %b %e %H:%M:%S:'.$milliseconds.' %Z %Y', localtime( $seconds ) ) . "\n"; + print $date; + } elsif ($ARGV[0] eq "ms") { + ($seconds, $microseconds) = gettimeofday; + $milliseconds = substr $microseconds, 0, 3; + print $seconds . $milliseconds . "\n"; + } else { + print strftime('%a %b %e %H:%M:%S %Z %Y', localtime( $ARGV[0] ) ) . "\n"; + } +} else { + print time . "\n"; +} -print strftime('%a %b %e %H:%M:%S %Z %Y', localtime( $ARGV[0] ) ) . "\n"; -- cgit v1.2.3 From 1a5f471f587b3be5c732cca2f94ec5cfa9bfa108 Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Wed, 12 Jul 2017 14:23:57 +0100 Subject: Add comments --- unixtime.pl | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'unixtime.pl') diff --git a/unixtime.pl b/unixtime.pl index d6bb1d8..2675634 100755 --- a/unixtime.pl +++ b/unixtime.pl @@ -4,18 +4,22 @@ use Time::HiRes qw( gettimeofday ); if (defined $ARGV[0]) { if ($ARGV[1] eq "ms") { + #Convert from timestamp in milliseconds to date $seconds = substr $ARGV[0], 0, 10; $milliseconds = substr $ARGV[0], 10, 3; $date = strftime('%a %b %e %H:%M:%S:'.$milliseconds.' %Z %Y', localtime( $seconds ) ) . "\n"; print $date; } elsif ($ARGV[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"; } } else { + #Print current standard unix timestamp print time . "\n"; } -- cgit v1.2.3 From 955b122dd5ec7560d5b76c6095f024bf9e26560f Mon Sep 17 00:00:00 2001 From: Joe Robinson Date: Wed, 12 Jul 2017 14:32:08 +0100 Subject: Add some spaces on string concatenation to be consistent --- unixtime.pl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'unixtime.pl') diff --git a/unixtime.pl b/unixtime.pl index 2675634..f47cf9f 100755 --- a/unixtime.pl +++ b/unixtime.pl @@ -7,7 +7,7 @@ if (defined $ARGV[0]) { #Convert from timestamp in milliseconds to date $seconds = substr $ARGV[0], 0, 10; $milliseconds = substr $ARGV[0], 10, 3; - $date = strftime('%a %b %e %H:%M:%S:'.$milliseconds.' %Z %Y', localtime( $seconds ) ) . "\n"; + $date = strftime('%a %b %e %H:%M:%S:' . $milliseconds . ' %Z %Y', localtime( $seconds ) ) . "\n"; print $date; } elsif ($ARGV[0] eq "ms") { #Print current millisecond unix timestamp -- cgit v1.2.3