如何获取文件';Perl中上次修改的时间是多少?

如何获取文件';Perl中上次修改的时间是多少?,perl,file-io,Perl,File Io,假设我有一个文件句柄$fh。我可以用-e$fh检查它的存在,或者用-s$fh或检查它的文件大小。如何获取上次修改的时间戳?使用内置函数。或者更具体地说: my $modtime = (stat($fh))[9] 您可以使用stat()或File::stat模块 perldoc -f stat 您需要stat调用和文件名: my $last_mod_time = (stat ($file))[9]; Perl还有一个不同的版本: my $last_mod_time = -M $file;

假设我有一个文件句柄
$fh
。我可以用
-e$fh
检查它的存在,或者用
-s$fh
或检查它的文件大小。如何获取上次修改的时间戳?

使用内置函数。或者更具体地说:

my $modtime = (stat($fh))[9]
您可以使用stat()或File::stat模块

perldoc -f stat

您需要stat调用和文件名:

my $last_mod_time = (stat ($file))[9];
Perl还有一个不同的版本:

my $last_mod_time = -M $file;
但该值与程序启动的时间有关。这对于排序之类的事情很有用,但您可能需要第一个版本

my @array = stat($filehandle);
修改时间以Unix格式存储在$array[9]中

或者明确地说:

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

  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
纪元是1970年1月1日格林威治标准时间00:00


更多信息请参见。

我想您正在寻找
stat
函数(
perldoc-f stat

特别是,返回列表的第9个字段(第10个,索引#9)是自纪元以来文件的最后一次修改时间(以秒为单位)

因此:


my$last_modified=(stat($fh))[9]

调用内置函数
stat($fh)
返回一个数组,其中包含有关传入的文件句柄的以下信息(来自):

此数组中的元素编号9将为您提供自历元(1970年1月1日00:00 GMT)以来的最后一次修改时间。由此可以确定当地时间:

my $epoch_timestamp = (stat($fh))[9];
my $timestamp       = localtime($epoch_timestamp);
或者,您可以使用内置模块
File::stat
(从Perl 5.004开始提供)来实现更面向对象的接口

为了避免上一个示例中需要的幻数9,另外使用
Time::localtime
,这是另一个内置模块(从Perl 5.004开始也包括在内)。这些因素共同导致一些(可以说)更清晰的代码:

use File::stat;
use Time::localtime;
my $timestamp = ctime(stat($fh)->mtime);

如果您只是比较两个文件以查看哪个文件较新,那么
-C
应该可以工作:

if (-C "file1.txt" > -C "file2.txt") {
{
    /* Update */
}
还有
-M
,但我认为这不是你想要的。幸运的是,通过谷歌搜索这些文件运营商的文档几乎是不可能的。

在我的系统上,
stat
只返回一个祝福

$VAR1 = bless( [
                 102,
                 8,
                 33188,
                 1,
                 0,
                 0,
                 661,
                 276,
                 1372816636,
                 1372755222,
                 1372755233,
                 32768,
                 8
               ], 'File::stat' );
您需要像这样提取
mtime

my @ABC = (stat($my_file));

print "-----------$ABC['File::stat'][9] ------------------------\n";


这是一个非常古老的线程,但我尝试使用该解决方案,但无法从File::stat(Perl 5.10.1)中获取信息

我必须做到以下几点:

my $f_stats = stat($fh);
my $timestamp_mod = localtime($f_stats->mtime);
print "MOD_TIME = $timestamp_mod \n";

我只是想分享一下,以防其他人也有同样的问题。

在谷歌
-M
中添加引号“-M”,因为
-X
会删除具有
X
的结果。。。顺便说一下,“-M”是OP想要的。使用“解释-M(内容修改)和-C(inode更改)之间的区别”会更自然:是否应该更正时区更改的
localtime
?例如:在PST中创建文件,然后从CET中读取
mtime
。我得到了令人困惑的结果。提到
file::stat
的使用是误导性的,因为这将提供一种面向对象的方式来访问stat信息,而直接访问数组元素则不会不工作。无需使用
文件::stat
访问例如
(stat($fh))[9]
print "-----------$ABC[0][9] ------------------------\n";
my $f_stats = stat($fh);
my $timestamp_mod = localtime($f_stats->mtime);
print "MOD_TIME = $timestamp_mod \n";