我想比较Perl中的当前时间和文件创建时间,但两者的格式不同.localtime采用以下格式:
22116291110813630
和文件创建时间是
Today, December 29, 2008, 2:38:37 PM
我如何比较哪一个更大和他们的区别?
它比使用stat()和time()/ localtime()更容易.
my $diff = -M $filename;
-M运算符返回文件的"年龄"(自程序启动以来的几天).它记录在-X函数下或perldoc -f -X
.
如果要比较值,可能需要使用localtime
标量上下文中的数字和可以获得的inode更改时间stat
:
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, $atime,$mtime,$ctime,$blksize,$blocks) = stat($filename);
哪里:
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
所以你想要第9场:
$mtime = ( stat $filename )[9];
$current_time = time;
$diff = $current_time - $mtime;