http://qs321.pair.com?node_id=183838

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:
#!/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.

Replies are listed 'Best First'.
Re: time2string
by Koschei (Monk) on Jul 22, 2002 at 02:41 UTC

    Perhaps useful to you. All hail Time::Duration.

    #!/usr/bin/perl -w use strict; use Time::Piece; use Time::Duration; use IO::File; my ( $uptime ) = ( IO::File->new('/proc/uptime') or die "No! Can't open!: $!\n" )->getline =~ /^ (\d[\d.]+) \s+ (\d[\d.]+)$/x or die "Invalid uptime!\ +n"; my $now = Time::Piece->new; my $boot = $now-$uptime; printf <<"EOF", duration($uptime), $boot->cdate, $now->cdate; Uptime: %s Boot: %s Now: %s EOF
    -- Iain, aka Koschei.
      So module happy... *shakes head* ;)

        I *like* modules =)

        Anyway, that code is normally buried somewhere in a mod_perl handler, so it's not as expensive as it looks. At least it's succinct =)

        -- Iain, aka Koschei.

Re: time2string
by zentara (Archbishop) on Jul 21, 2002 at 21:56 UTC
    This script runs, but I noticed 2 things.
    1. Running it complains about an unitialized value
    at line 37, $prefix. Maybe add an else clause.
    if ($time < 0) { $time = - $time; $prefix = "- "; }else{$prefix=''}

    2. I'm getting a strange chr(2), "shaded box", printed out
    as a separator between each field. In the example below
    'a' is shown where I show a shaded box.
    a8ah a53am a25as
    I can see where it's coming from, it kindof gives it an
    "odometer" effect. :-) Maybe remove the \002 's from
    the push @data lines.
      I've now got the variable declared as my $prefix = ''; Which should fix it? Or should I maybe make another variable to store the value that is going to be returned such as:
      my $return; $return = $prefix if $prefix; $return .= join ' ', @data; return $return;
      Also the \002 are used to produce bold text for IRC.