rendler has asked for the wisdom of the Perl Monks concerning the following question:
Okay this original sub was taken from blootbot, and I needed it to format the strings better and worked it into this:
Any suggestions/tips on how it might be improved would be greatly appreciated. Thanks.#!/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); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: time2string
by Koschei (Monk) on Jul 22, 2002 at 02:41 UTC | |
by rendler (Pilgrim) on Jul 22, 2002 at 03:33 UTC | |
by Koschei (Monk) on Jul 22, 2002 at 05:09 UTC | |
Re: time2string
by zentara (Archbishop) on Jul 21, 2002 at 21:56 UTC | |
by rendler (Pilgrim) on Jul 21, 2002 at 22:49 UTC |
Back to
Seekers of Perl Wisdom