Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

When displaying time from last boot in proc files how can I display it in mm:dd:yy:hh:ss. I want to put it in my code when the file is opened and result of last boot is displayed

Replies are listed 'Best First'.
Re: Time Conversion (guess)
by tye (Sage) on Feb 16, 2004 at 06:37 UTC

    Perhaps you meant something like this?

    open UP, "< /proc/uptime" or die "Can't read /proc/uptime: $!$/"; my @boot= ( localtime time() - ( split ' ', <UP> )[0] )[ reverse 0..5 ]; close UP; $boot[0] += 1900; $boot[1]++; $_= sprintf "%02d", $_ for @boot; my $boot= join( '-', @boot[0..2] ) . '/' . join( ':', @boot[3..5] ); print "Last booted $boot$/";

    Note that I ignored much of your requested format because I don't think that even you want to report seconds but not minutes and I refuse to use 2-digits years, put colon between date parts, nor order date/time fields other then such that they make sorting easy (and doing all three makes a date pretty unrecognizable) -- that is, I follow the international standard. (:

    - tye        

Re: Time Conversion
by Aristotle (Chancellor) on Feb 16, 2004 at 07:25 UTC
    Modelled using tye's date format, but using the very handy strftime from POSIX to avoid date math and string assembly:
    use POSIX qw(strftime); sub uptime { local $/ = " "; open my $fh, '<', '/proc/uptime' or die "Can't open /proc/uptime to read: $!\n"; return scalar <$fh>; } print strftime "Last booted %Y-%m-%d/%H:%M:%S\n", ( localtime( time() - uptime() ) );
    I originally wrote it using an $uptime temporary with a do block initializer, then noticed I can basically switch to a sub by substituting sub uptime for my $uptime = do. Either way makes the code self-documenting.

    Makeshifts last the longest.

Re: Time Conversion
by davido (Cardinal) on Feb 16, 2004 at 03:51 UTC
    That depends on what format it is in to begin with. I'm not familiar with what proc files look like inside. However, the Date::Manip module is great for taking dates/times of just about any format and converting them to other formats.


    Dave

Re: Time Conversion
by iburrell (Chaplain) on Feb 16, 2004 at 20:17 UTC
    Other people have mentioned how to use the uptime to find the date when the machine was started. It is fairly easy to calculate how long the machine has been up.

    /proc/uptime contains the number of seconds that the system has been up. You can use gmtime() to treat this as the number of seconds since the beginning of the epoch, January 1, 1970. The "date" will have the hour, minutes, and seconds. If you want months, you can use the month and day of the month minus 1. If you want just days, the day of the year works.

    # Use the function from the previous reply # to read the value from /proc/uptime my $uptime = uptime(); my @tm = gmtime($uptime); my $string = sprintf("%03d %02d:%02d:%02d", $tm[7], $tm[2], $tm[1], $t +m[0]);

    For longer spans, you will need to use the year (minus 70). Also, you will need to worry about leap years; 1972 was a leap year and adds an extra day. For this, you might want to find a module for handling intervals. DateTime::Duration is a good choice.