如何将文件创建时间与Perl中的当前时间进行比较?

5 perl time file

我想比较Perl中的当前时间和文件创建时间,但两者的格式不同.localtime采用以下格式:

22116291110813630
Run Code Online (Sandbox Code Playgroud)

和文件创建时间是

Today, December 29, 2008, 2:38:37 PM
Run Code Online (Sandbox Code Playgroud)

我如何比较哪一个更大和他们的区别?

jim*_*tut 15

它比使用stat()和time()/ localtime()更容易.

my $diff = -M $filename;
Run Code Online (Sandbox Code Playgroud)

-M运算符返回文件的"年龄"(自程序启动以来的几天).它记录在-X函数下perldoc -f -X.

  • 我知道这是一个老线程,但无论如何我都会发布回复.-M给出上次修改的"年龄"而不是文件创建.-C会按要求给出正确的值,但我只在一行中使用-X函数(http://perldoc.perl.org/functions/-X.html)并在剩下的时间使用stat. (3认同)

Nat*_*man 13

如果要比较值,可能需要使用localtime标量上下文中的数字和可以获得的inode更改时间stat:

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

哪里:

                 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's owner
                 5 gid      numeric group ID of file's 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;


yst*_*sth 3

localtime返回列表上下文中的值列表。请参阅本地时间文档perlcheat。在你的例子中,看起来这些都混在一起了。在标量上下文中,它返回一个格式化字符串,例如Mon Dec 29 03:16:33 2008. 在大多数平台上,文件索引节点更改时间将从stat某个纪元以来的秒数返回。您应该能够直接将其与time()( not localtime() ) 的结果进行比较。