获取当时时间字符串:
sub getTime
{
my $time = shift || time();
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time);
$year += 1900;
$mon ++;
$min = '0'.$min if length($min) < 2;
$sec = '0'.$sec if length($sec) < 2;
$mon = '0'.$mon if length($mon) < 2;
$mday = '0'.$mday if length($mday) < 2;
$hour = '0'.$hour if length($hour) < 2;
my $weekday = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat')[$wday];
return "$year$mon$mday$hour$min$sec";
}
把YYYY-MM-DD HH:MI:SS或者YYYY/MM/DD HH:MI:SS格式的时间字符串转换为时间。然后可以相互比较。两个时间比较后得到的差值是秒差。
sub time_local {
my $time = shift;
my ($sec,$min,$hour,$mday,$mon,$year);
my @time = reverse(split /-|:|/|s/,$time);
while((my $t = @time) < 6){unshift @time,00;$t += 1;}
my $timel;
foreach(@time){
s/^0(.){1}/$1/;
$timel .= $_.",";
}
$timel =~ s/,$//;
($sec,$min,$hour,$mday,$mon,$year) = split /,/,$timel;
$timel = timelocal($sec,$min,$hour,$mday,$mon,$year);
return $timel;
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/47869/viewspace-1038958/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/47869/viewspace-1038958/