Warm tip: This article is reproduced from serverfault.com, please click

datetime-如何在Perl中使用File :: stat获取文件的本地时间修改?

(datetime - How can I get the local time modification of a file with File::stat in perl?)

发布于 2020-11-30 13:43:23

如何获取以本地时间格式设置的文件修改时间?

通过做这个:

use File::stat;
use Time::Piece;

my $format = '%Y%m%d%H%M';

print Time::Piece->strptime(stat($ARGV[0])->mtime, '%s')->strftime($format);

我得到202011301257的文件是在当地时间11月30日13:57保存的(格林尼治标准时间+01:00)。

既然我能做

print localtime $file->stat->mtime;

print localtime->strftime($format)

我想做类似的事情

print (localtime stat($file)->mtime)->strftime($format);

哪个抛出

Can't locate object method "mtime" via package "1" (perhaps you forgot to load "1"?) 

有什么建议吗?

Questioner
simone
Viewed
0
Shawn 2020-12-01 02:36:59

我想做类似的事情

print (localtime stat($file)->mtime)->strftime($format);

很接近!你的第一个括号位于错误的位置:

#!/usr/bin/env perl
use warnings; # Pardon the boilerplate
use strict;
use feature 'say';
use File::stat;
use Time::Piece;

my $format = '%Y%m%d%H%M';
say localtime(stat($ARGV[0])->mtime)->strftime($format);