将旧的Unix日期转换为Perl并进行比较 [英] Convert Old Unix Date to Perl and compare
问题描述
要求-我的文件名为 Rajesh.1202242219。数字只不过是日期 date’+%y%m’%d’%H’%M’
格式。
现在,我试图编写一个perl脚本以从文件名中提取数字,并与当前系统日期和时间进行比较,并根据此比较的输出,使用perl打印一些值。
Requirement - I have file name called "Rajesh.1202242219". Numbers are nothing but a date "date '+%y''%m''%d''%H''%M'
" format.
Now I am trying to write a perl script to extract the numbers from file name and compare with current system date and time and based on output of this comparison, print some value using perl.
方法:
从文件名中提取数字:
if ($file =~ /Rajesh.(\d+).*/) {
print $1;
}
将此时间转换为perl可读的时间
my $sec = 0; # Not Feeded
my $min = 19;
my $hour = 22;
my $day = 24;
my $mon = 02 - 1;
my $year = 2012 - 1900;
my $wday = 0; # Not Feeded
my $yday = 0; # Not Feeded
my $unixtime = mktime ($sec, $min, $hour, $day, $mon, $year, $wday, $yday);
print "$unixtime\n";
my $readable_time = localtime($unixtime);
print "$readable_time\n";
查找当前时间并进行比较...
my $CurrentTime = time();
my $Todaydate = localtime($startTime);
但是这里的问题是,我没有得到如何从<$ c中提取2位数字的解决方案$ c> $ 1 并分配给 $ sec
, $ min
等。任何帮助?
But the problem here is, I am not getting solution of how to extract 2 digit from $1
and assign to $sec
, $min
, etc. Any help?
此外,如果您对此问题陈述有好的解决方法,请与我分享
Also, if you have good approach for this problem statement, Please share with me
推荐答案
我认为 解包
可能更合适。
I think unpack
might be a better fit.
if ( my ( $num ) = $file =~ /Rajesh.(\d+).*/ ) {
my ( $year, $mon, $day, $hour, $min ) = unpack( 'A2 A2 A2 A2 A2', $num );
my $ts = POSIX::mktime( 0, $min, $hour, $day, $mon - 1, $year + 100 );
...
}
这篇关于将旧的Unix日期转换为Perl并进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!