【问题标题】:Perl: Get time-stamp of a file and check is this today or notPerl:获取文件的时间戳并检查是否今天
【发布时间】:2016-08-08 09:19:50
【问题描述】:

在 Perl 中,我如何获取文件的时间戳并检查是否是今天?

谢谢。

亚历克斯

【问题讨论】:

  • 你试过什么?向我们展示您的代码并告诉我们您期望的结果以及它与您的代码提供的结果有何不同。
  • 请查看链接perlmonks.org/…
  • @Arijit:请不要链接到 PerlMonks 的常见问题解答副本。它真的很旧而且过时了(它在页面顶部说!)perldoc.perl.org 的版本将永远是最新的。 How do I get a file's timestamp in perl?.

标签: linux perl


【解决方案1】:

我会(可能)使用-M

http://perldoc.perl.org/functions/-X.html

-M 脚本开始时间减去文件修改时间,以天为单位。

这意味着你可以这样做:

if ( -M $filename < 1 ) { 
    #if file is less than a day old
}

当然,这仅适用于脚本启动,而不是现在,因此不适合长时间运行的脚本。

【讨论】:

  • “不到一天前修改”与“今天修改”不太一样。
  • 您可以设置-M相对于$^T是脚本的开始时间,即my $mtime_abs = $^T + (-M $filename);。但@DaveCross 所说的仍然是真的。
【解决方案2】:

如果您向我们准确展示您的尝试和遇到的问题,如果有帮助的话。

您可以使用File::stat 获取有关文件的信息。

use File::stat;
my $stat = stat($file);

您可以通过在$stat 对象上调用三个不同的方法来获得三个不同的时间戳。

my $ctime = $stat->ctime; # inode change time
my $atime = $stat->atime; # last access time
my $mtime = $stat->mtime; # last modification time

我想你可能想要$mtime,但我不能确定。这些变量中的每一个都包含时间,即自系统纪元以来的秒数(几乎可以肯定是 1970 年 1 月 1 日 00:00)。

您可以使用 Time::Piece 将这些 epoch 值转换为有用的对象。

use Time::Piece;
my $file_date = localtime($mtime);

您可以将其与当前日期进行比较。

if ($file_date->date eq localtime->date) {
  # file was created today
}

【讨论】:

  • 不错。比摆弄本地时间/本地时间更容易。 'sudo cpan -i Time::Piece' 来安装。
  • 无需安装。 Time::Piece 自 Perl 5.10(2007 年)以来一直是标准 Perl 安装的一部分。
  • 在旧机器上可能需要安装。我不得不为 redhat fc19 做这件事。 perl 5 版本 16。
  • Time::Piece - 面向对象 - 绝对是要使用的东西 - 简单得多。但我看到还必须为 CentOS7 7.2.1511 (2015) 安装它。 fc19 是 2013 年。这个 Q 很有趣:stackoverflow.com/questions/2049735/… Time::Piece 在这里的核心模块中:perldoc.perl.org/index-modules-T.html
  • 这是因为 Red Hat 故意破坏了标准 Perl 安装。如果您使用的是 Centos(或 RHEL)6 或更高版本,而不是安装单个模块,您应该 yum install perl-core 修复您的 Perl 安装。更多信息在this old blog post
【解决方案3】:

ways 的其中一个操作方法浓缩成一条线:perl -e '$f = shift; printf "file %s updated at %s\n", $f, scalar localtime((stat $f)[9])' file

【讨论】:

  • 稍微缩短 - perl -E'say "file $ARGV[0] updated at " . localtime((stat $ARGV[0])[9])' file :-)
【解决方案4】:
  1. 阅读 File::stat 并获取文件的时间戳。 How do I get a file's last modified time in Perl?
use File::stat;
 # we don't need anything but mtime
 # my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
 #    $atime, $mtime, $ctime, $blksize, $blocks) = stat($filename);
my $ts = stat($filename)->mtime;
print "ts:$ts";
print " date/time " . localtime($ts) . "\n";

ts:1469028287 date/time Wed Jul 20 16:24:47 2016

哦,看。 mtime 是一个很大的数字。多少 。 .分钟/小时/天/年是什么? (dc 是一个命令行计算器。)

$ dc
1469028287
60/p
24483804
60/p
408063
24/p
17002
356.25/p
47

47 岁。 (使用 dc 进行整数除法获得/损失了一些时间)。现在(2016 年 8 月 8 日星期一 10:58ish)- 46(其中 46 =ishahem= 47)年 = 1/1/1970 00:00:00 = unix 日期/时间戳时代。

  1. 了解当地时间,获取“现在”时间,获取今天从午夜到午夜的时间。 http://perldoc.perl.org/functions/localtime.htmlhttp://www.perlhowto.com/working_with_date_time How do I use Perl's localtime with print to get the timestamp? 请注意,本地时间的年份值为 116(自 1900 年以来的 116 年)。 请注意,本地时间的月份值为 7(8 月为 7。是的,因为 0 = 1 月,1 = 2 月,等等)。
# localtime returns array or string depending on context.
my $time = localtime;
my @time = localtime;
print "time:$time\n";    
print "time array: " . join (":", (@time)) . "\n";

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
use Time::Local;
my $btime = timelocal(0,0,0,$mday,$mon,$year);
my $etime = timelocal(59,59,23,$mday,$mon,$year);
print "btime:$btime " . localtime($btime) . " etime:$etime " . localtime($etime) . "\n";
print "year:$year\n";

time:Mon Aug  8 11:40:33 2016
time array: 33:40:11:8:7:116:1:220:1
btime:1470610800 Mon Aug  8 00:00:00 2016 etime:1470697199 Mon Aug  8 23:59:59 2016
year:116
  1. 检查文件时间戳在午夜时间之间。检查所有的东西。
if (($ts >= $btime) && ($ts <= $etime)) {
   print "File time $ts (".localtime($ts).") is TODAY.\n";
} else {
   print "File time $ts (".localtime($ts).") is NOT today.\n";
   if ($ts < $btime) {
       print "File is BEFORE today. $ts < $btime\n";
   } elsif ($ts > $etime) {
       print "File is in FUTURE. $ts > $etime\n";
   } else {
       print "KERBOOM.\n"
   }
}

【讨论】:

  • “localtime 根据上下文返回数组或整数(ctime)” - 我认为您的意思是“字符串”,而不是“整数”。 localtime 从不返回(单个)整数。
  • 哎呀!是的。谢谢你。我没有正确考虑这一点!
猜你喜欢
  • 2011-08-12
  • 1970-01-01
  • 2020-02-22
  • 2017-01-13
  • 2012-03-12
  • 2012-10-19
  • 1970-01-01
  • 2019-08-21
  • 1970-01-01
相关资源
最近更新 更多