summaryrefslogtreecommitdiff
path: root/unixtime.pl
diff options
context:
space:
mode:
authorLuke Bratch <luke@bratch.co.uk>2017-07-12 15:08:20 +0100
committerLuke Bratch <luke@bratch.co.uk>2017-07-12 15:08:20 +0100
commit347936a79b7e326083559ec8614df1c26a829558 (patch)
tree10855a382c16f68d15ac4c42c4604aa95e3a87f4 /unixtime.pl
parentba7417df7cdd5f291d8cc359c9c5450ffadff905 (diff)
parent955b122dd5ec7560d5b76c6095f024bf9e26560f (diff)
Merge branch 'milliseconds' into 'master'
Milliseconds Added 3 new functions: **Print the current unix timestamp** Run the script with no arguments e.g: joe@phobos ~/blatech/unixtime $ ./unixtime.pl 1499865953 **Convert from time since the Unix epoch, given in milliseconds** Run the script with a timestamp in milliseconds, and add a second argument "ms" e.g: joe@phobos ~/blatech/unixtime $ ./unixtime.pl 1499865571123 ms Wed Jul 12 14:19:31:123 BST 2017 **Print the current time since the Unix epoch in milliseconds** Run the script only with the argument "ms" e.g: joe@phobos ~/blatech/unixtime $ ./unixtime.pl ms 1499866043745 See merge request !1
Diffstat (limited to 'unixtime.pl')
-rwxr-xr-xunixtime.pl23
1 files changed, 22 insertions, 1 deletions
diff --git a/unixtime.pl b/unixtime.pl
index 08a987a..f47cf9f 100755
--- a/unixtime.pl
+++ b/unixtime.pl
@@ -1,4 +1,25 @@
#!/usr/bin/perl
use POSIX qw( strftime );
+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";
+}
-print strftime('%a %b %e %H:%M:%S %Z %Y', localtime( $ARGV[0] ) ) . "\n";