phpman > perldoc > timestamp(14)

Markdown | JSON | MCP    

Found in /usr/share/perl/5.34/pod/perlfaq5.pod
  How do I get a file's timestamp in perl?
    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 -A, -M,
    or -C file test operations as documented in perlfunc. These retrieve the
    age of the file (measured against the start-time of your program) in
    days as a floating point number. Some platforms may not have all of
    these times. See perlport for details. To retrieve the "raw" time in
    seconds since the epoch, you would call the stat function, then use
    "localtime()", "gmtime()", or "POSIX::strftime()" to convert this into
    human-readable form.

    Here's an example:

        my $write_secs = (stat($file))[9];
        printf "file %s updated at %s\n", $file,
            scalar localtime($write_secs);

    If you prefer something more legible, use the File::stat module (part of
    the standard distribution in version 5.004 and later):

        # error checking left as an exercise for reader.
        use File::stat;
        use Time::localtime;
        my $date_string = ctime(stat($file)->mtime);
        print "file $file updated at $date_string\n";

    The POSIX::strftime() approach has the benefit of being, in theory,
    independent of the current locale. See perllocale for details.

  How do I set a file's timestamp in perl?
    You use the utime() function documented in "utime" in perlfunc. By way
    of example, here's a little program that copies the read and write times
    from its first argument to all the rest of them.

        if (@ARGV < 2) {
            die "usage: cptimes timestamp_file other_files ...\n";
        }
        my $timestamp = shift;
        my($atime, $mtime) = (stat($timestamp))[8,9];
        utime $atime, $mtime, @ARGV;

    Error checking is, as usual, left as an exercise for the reader.

    The perldoc for utime also has an example that has the same effect as
    touch(1) on files that *already exist*.

    Certain file systems have a limited ability to store the times on a file
    at the expected level of precision. For example, the FAT and HPFS
    filesystem are unable to create dates on files with a finer granularity
    than two seconds. This is a limitation of the filesystems, not of
    utime().

Generated by phpman v4.0 Author: Che Dong Under GNU General Public License
2026-06-15 20:43 @216.73.217.83
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)
Valid XHTML 1.0 TransitionalValid CSS!