Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

How do I interpret mtime with stat()?

by Anonymous Monk
on Oct 21, 2000 at 10:35 UTC ( [id://37789]=perlquestion: print w/replies, xml ) Need Help??

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

With: $mtime = (stat $filename)[9]; I get a number. What do I do with that number?

Originally posted as a Categorized Question.

Replies are listed 'Best First'.
Re: How do I interpret mtime with stat()?
by mitd (Curate) on Oct 21, 2000 at 20:43 UTC

    mtime as returned by (stat($filename))[9] is seconds since the epoch on your particular platform.
    To see when the beginning of your machine's epoch was:

    print scalar gmtime(0);

    Some modules useful for working with date/time:

      Bollocks! I messed up CPAN links here are the corrected links:

      Date::Format
      Date::Calc
      Date::Manip

      Thank the gods! I had time on my side.

      <a href="http://www.georgian.net/~mitd>MitD -- Made in the Dark
      'My favourite colour appears to be grey.'

Re: How do I interpret mtime with stat()?
by extremely (Priest) on Oct 21, 2000 at 11:33 UTC

    mtime is the number of seconds since 1970 (at least on UNIX-ish systems).
    You can compare it to time() to see how old the file is.

    my $mtime = (stat $filename)[9]; my $age = time() - $mtime; die "HEY! $filename was created in the future!\n" if $age < 0; print "$filename is $age seconds old\n";
      *sigh* time only has one "t" in it. Promise...

      --
      $you = new YOU;
      honk() if $you->love(perl)

Re: How do I interpret mtime with stat()?
by Klem (Initiate) on Dec 21, 2001 at 04:12 UTC
    If you don't need the other stuff that comes from stat(), you may want to consider using the -M file test operator, which yields the age (since 'last modified') for the file, in (fractional) days.
      Actually, it's the last modified for a file, from the moment the script started. If your script has been running for 48 hours (it could happen), then -M is totally off course. stat is in this case a much safer approach.

      Greetz
      Beatnik
      ... Quidquid perl dictum sit, altum viditur.
        Well, actually the filetest operators compute their deltas against the value of $^T, which is initialized during startup. However, you may reset it if you think it's value might be getting stale:
        $^T = time(); $age = -M $file;

        -Blake

Re: How do I interpret mtime with stat()?
by Anonymous Monk on Aug 13, 2003 at 16:57 UTC
    if you want to know everything about the time, use the localtime function:
    my $mtime = (stat $filename)[9]; my($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($m +time);
    All list elements are numeric, and come straight out of a struct tm. ... In particular this means that #mon has the range 0..11, $wday has the range 0..6, and the year has had 1,900 subtracted from it. (You can remember which ones are 0-based because those are the ones you're always using as subscripts into 0-based arrays containing the month and day names.)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://37789]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-03-19 03:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found