Okay this original sub was taken from blootbot, and I needed it to format the strings better and worked it into this:
#!/usr/bin/perl -w
use strict;
open UPTIME, "/proc/uptime" or die "cannot open '/proc/uptime'.\n";
my $time = <UPTIME>;
close UPTIME;
$time = sprintf "%.0f", $time =~ /^(\d+)\./;
print time2string($time), "\n";
sub time2string {
my $time = shift;
return if ! defined $time or $time !~ /^\-?\d+$/;
my $prefix;
if ($time < 0) {
$time = - $time;
$prefix = "- ";
}
my $s = $time % 60;
my $m = ($time / 60) % 60;
my $h = ($time / (60 * 60)) % 24;
my $d = int($time / (24 * 60 * 60));
(my $y, $d) = modulus_thingamajig(365, $d);
(my $mo, $d) = modulus_thingamajig(30, $d);
(my $w, $d) = modulus_thingamajig(7, $d);
my @data;
push @data, sprintf "\002%d\002y", $y if $y != 0;
push @data, sprintf "\002%d\002mo", $mo if $mo != 0;
push @data, sprintf "\002%d\002w", $w if $w != 0;
push @data, sprintf "\002%d\002d", $d if $d != 0;
push @data, sprintf "\002%d\002h", $h if $h != 0;
push @data, sprintf "\002%d\002m", $m if $m != 0;
push @data, sprintf "\002%d\002s", $s if $s != 0 or ! @data;
return $prefix . join ' ', @data;
}
sub modulus_thingamajig {
my ($days, $d) = @_;
my $return;
if ($d >= $days) {
$return = int($d / $days);
$d = $d % $days;
} else {
$return = 0;
}
return ($return, $d);
}
Any suggestions/tips on how it might be improved would be greatly appreciated. Thanks.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.