How to compare file creation time with current time in Perl?

I want to compare the current time and file creation time in Perl, but both of them are in a different format. localtime in this format:

22116291110813630

and file creation time

Today, December 29, 2008, 2:38:37 PM

How to compare which one is larger and their difference?

+5
source share
4 answers

This is even simpler than using stat () and time () / localtime ().

my $diff = -M $filename;

The -M operator returns the "age" of the file (a few days after the program starts). It is documented in - function X or in perldoc -f -X.

+14
source

, , localtime inode stat:

               ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
                  $atime,$mtime,$ctime,$blksize,$blocks)
                      = stat($filename);

:

                 0 dev      device number of filesystem
                 1 ino      inode number
                 2 mode     file mode  (type and permissions)
                 3 nlink    number of (hard) links to the file
                 4 uid      numeric user ID of file owner
                 5 gid      numeric group ID of file owner
                 6 rdev     the device identifier (special files only)
                 7 size     total size of file, in bytes
                 8 atime    last access time in seconds since the epoch
                 9 mtime    last modify time in seconds since the epoch
                10 ctime    inode change time in seconds since the epoch (*)
                11 blksize  preferred block size for file system I/O
                12 blocks   actual number of blocks allocated

, 9:


$mtime = ( stat $filename )[9];
$current_time = time;

$diff = $current_time - $mtime;

+13

localtime . . perlcheat. , . Mon Dec 29 03:16:33 2008. inode stat . time() ( localtime()).

+3

These two functions thanks to jimtut's answer. fileage prints the number of seconds as an integer, ideally suited for use in the shell of a file since it was created. fileage is the answer to the above question, while the data prints the same for the contents of the file as the answer I was looking for, I am sure that both of them will be useful.

function fileage {
  perl -e 'printf "%i\n", 60 * 60 * 24 * -C "'"${1:?Must provide a file name}"'"'
}

function dataage {
  perl -e 'printf "%i\n", 60 * 60 * 24 * -M "'"${1:?Must provide a file name}"'"'
}
0
source

All Articles