<?xml version="1.0" encoding="windows-1252"?>
<node id="808" title="How do I get a file's timestamp in perl?" created="1999-10-12 23:42:27" updated="2005-08-15 07:33:58">
<type id="834">
perlfaq nodetype</type>
<author id="519">
faq_monk</author>
<data>
<field name="doctext">


&lt;P&gt;
If you want to retrieve the time at which the file was last read, written,
or had its meta-data (owner, etc) changed, you use the &lt;STRONG&gt;-M&lt;/STRONG&gt;,
&lt;STRONG&gt;-A&lt;/STRONG&gt;, or &lt;STRONG&gt;-C&lt;/STRONG&gt; filetest operations as documented in [perlman:perlfunc|the perlfunc manpage]. These retrieve the age of the file (measured against the start-time of your program) in days as a floating point number. To retrieve the ``raw'' time in seconds since the epoch, you would call the stat function, then use 
&lt;CODE&gt;localtime(),&lt;/CODE&gt; 
&lt;CODE&gt;gmtime(),&lt;/CODE&gt; or POSIX::strftime() to convert this into human-readable form.

&lt;P&gt;
Here's an example:

&lt;P&gt;
&lt;PRE&gt;    $write_secs = (stat($file))&amp;#091;9&amp;#093;;
    printf &amp;quot;file %s updated at %s\n&amp;quot;, $file,
        scalar localtime($write_secs);
&lt;/PRE&gt;
&lt;P&gt;
If you prefer something more legible, use the File::stat module (part of
the standard distribution in version 5.004 and later):

&lt;P&gt;
&lt;PRE&gt;    use [perlman:File::stat|File::stat];
    use Time::localtime;
    $date_string = ctime(stat($file)-&amp;gt;mtime);
    print &amp;quot;file $file updated at $date_string\n&amp;quot;;
&lt;/PRE&gt;
&lt;P&gt;
Error checking is left as an exercise for the reader.

&lt;P&gt;
</field>
</data>
</node>
