| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# Little perl script which converts the time in epoch seconds into a |
| 4 |
# use viewable time/date. |
| 5 |
# |
| 6 |
|
| 7 |
use strict; |
| 8 |
|
| 9 |
use POSIX qw(strftime); |
| 10 |
|
| 11 |
sub usage { |
| 12 |
print qq[Usage: tsecs [-h] [-f strftime-format] [epoch1 [epoch2 [...]]] |
| 13 |
-h Specify the times as hex values instead of decimal. |
| 14 |
-f Specify the strftime format string to use to display the time. |
| 15 |
|
| 16 |
You can give a list times in epoch seconds to convert on the command line. |
| 17 |
The time "now" will display the current date and secs. |
| 18 |
If no times are given it will print out the current date and secs. |
| 19 |
]; |
| 20 |
exit 1; |
| 21 |
} |
| 22 |
|
| 23 |
my $now = time; |
| 24 |
@ARGV = ( $now ) unless @ARGV; |
| 25 |
|
| 26 |
my $time_format = "%a %b %e %H:%M:%S %Y %Z"; |
| 27 |
my $hex_b = 0; |
| 28 |
my @times; |
| 29 |
|
| 30 |
while ( @ARGV ) { |
| 31 |
$_ = shift; |
| 32 |
/^-f/ && do { $time_format = shift || usage(); next; }; |
| 33 |
/^-h/ && do { $hex_b = 1; next; }; |
| 34 |
/^-/ && usage(); |
| 35 |
/^now$/ && do { $times[++$#times] = $now; next; }; |
| 36 |
$times[++$#times] = ($hex_b ? hex $_ : $_); |
| 37 |
} |
| 38 |
|
| 39 |
foreach my $secs ( @times ) { |
| 40 |
my $time_str = strftime $time_format, localtime($secs); |
| 41 |
print "Time in seconds for $secs is '$time_str'\n"; |
| 42 |
} |