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.


In reply to Re: Time Conversion by iburrell
in thread Time Conversion by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.