Perl 创建时间范围为mtime的文本文件

Perl 创建时间范围为mtime的文本文件,perl,Perl,我找到了下面的一行,它创建了50个文本文件 perl -MPath::Tiny -wE' path("dir/s".$_.".txt")->spew("some data...$_\n") for 1..50 但它会创建所有以mtime作为当前时间的文件 mtime是否可能介于日期范围之间?。比如2016年1月至2018年12月的文本文件 我知道我必须使用Time::Piece模块,但在我的RHEL Perl(5.010)中没有看到Time::Piece模块。中有touch方法,

我找到了下面的一行,它创建了50个文本文件

perl -MPath::Tiny -wE'
    path("dir/s".$_.".txt")->spew("some data...$_\n") for 1..50
但它会创建所有以mtime作为当前时间的文件

mtime是否可能介于日期范围之间?。比如2016年1月至2018年12月的文本文件


我知道我必须使用Time::Piece模块,但在我的RHEL Perl(5.010)中没有看到Time::Piece模块。

中有
touch
方法,其余的是关于分割所需的日期范围。请注意,下面的脚本创建的文件(默认为10个)名称非常简单,因此请在全新的目录中运行,以免覆盖某些内容

perl -MPath::Tiny -MTime::Piece -wE'
    $n = shift // 10; 
    ($beg, $end) = map { 
        Time::Piece->strptime($_, "%Y%m%d")->epoch 
    } (20160101, 20181201); 
    $step = int ($end-$beg)/$n; 
    for (1..$n) { 
        $f = path("s$_.txt"); 
        $f->spew("data..$_\n"); 
        $f->touch($beg += $s)
' 5
将调用链接到所需的
path(..)->spew(..)->touch(..)
不起作用,因为
spew
返回true/false而不是对象。(触摸按钮确实返回对象,因此可以将方法链接到该对象,但在这里它必须跟随
spew
,因为
spew
更改了修改时间。)

上面的代码创建文件

-rw-rw-r--. 1 xxx 8 Jul 31 2016 s1.txt -rw-rw-r--. 1 xxx 8 Mar 1 2017 s2.txt -rw-rw-r--. 1 xxx 8 Sep 30 2017 s3.txt -rw-rw-r--. 1 xxx 8 May 1 2018 s4.txt -rw-rw-r--. 1 xxx 8 Nov 30 2018 s5.txt 其中,
$mday
1
开始,而其他所有从
0
开始。请注意,虽然传统上提供的年份受
+1900
约束(因此2016年为
116
),但也接受正常年份。所以

$beg = timelocal 0, 0, 0,  1,  0, 2016;
$end = timelocal 0, 0, 0, 31, 11, 2018;
或者,为了更灵活地安排日期

($beg, $end) = map { 
    ($yr, $m, $d) = unpack "A4A2A2"; 
    timelocal 0, 0, 0, $d, $m-1, $yr 
} (20160101, 20181231)


†  缺少
Time::Piece
的问题如中所述。也可以查看问题和聊天下方的评论。

看起来您可以通过Path::Tiny
.touch($seconds)
方法来设置文件的mtime和atime,但我现在无法测试。您尝试了什么?你有什么问题?我建议您先阅读文档,好吗?
“dir/s.”$\u0.txt“
是编写
“dir/s$\u0.txt”
@DaveCross.的一种奇怪的方式。。我一直在尝试,但在我的RHEL Perl 5.010中没有看到Time::Piece模块。Time::Piece已经是Perl 5.10的核心(它本身非常古老:2007)。您是否同时安装了
perl
perlcore
软件包。。非常感谢。事实上,没有计时片我很挣扎module@stack0114106嗯?我认为时间是核心,因为,嗯,永远。该文件列出了2000年。你确定你的系统正常吗?如果你不知道该怎么做,你有什么日期时间模块?有没有可能在没有时间段模块的情况下得到历元范围?。。您可以将其作为变体添加到同一文档中answer@stack0114106我正要问:)。有了它,你就有了
$sec\u epoch=timelocal sec,min,hour,day,mon,year在这里测试,威尔edit@stack0114106编辑
($beg, $end) = map { 
    ($yr, $m, $d) = unpack "A4A2A2"; 
    timelocal 0, 0, 0, $d, $m-1, $yr 
} (20160101, 20181231)