加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

如何获取文件的最后修改时间在Perl?

发布时间:2020-12-15 21:28:20 所属栏目:大数据 来源:网络整理
导读:假设我有一个文件句柄$ fh。我可以检查它的存在与-e $ fh或其文件大小与-s $ fh或 a slew of additional information about the file.如何获得其最后修改的时间戳? 解决方法 您可以使用内置的模块File :: stat(包含在Perl 5.004中)。 调用stat($ fh)返回一
假设我有一个文件句柄$ fh。我可以检查它的存在与-e $ fh或其文件大小与-s $ fh或 a slew of additional information about the file.如何获得其最后修改的时间戳?

解决方法

您可以使用内置的模块File :: stat(包含在Perl 5.004中)。

调用stat($ fh)返回一个数组,其中包含有关传入(从perlfunc man page for stat)的文件句柄的以下信息:

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 since the epoch
  9 mtime    last modify time since the epoch
 10 ctime    inode change time (NOT creation time!) since the epoch
 11 blksize  preferred block size for file system I/O
 12 blocks   actual number of blocks allocated

此数组中的第9个元素将为您提供自时代(1970年1月1日格林尼治时间1970年1月1日)以来的最后修改时间。从中你可以确定当地时间:

my $epoch_timestamp = (stat($fh))[9];
my $timestamp       = localtime($epoch_timestamp);

为了避免在前面的例子中需要的幻数9,另外使用Time :: localtime,另一个内置模块(也包括在Perl 5.004中)。这需要一些(可以说)更清晰的代码:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读