`

perl用Time::Local得到时间相差

    博客分类:
  • perl
阅读更多
#!/usr/bin/perl
use strict;
use Time::Local;

sub gettimemun(){
   my ($time,$timelc,$sec,$min,$hour,$mday,$mon,$year);
   $time = @_[0];
   my @time = reverse(split /-|:|\s|\//,$time);
   foreach(@time){
  s/^0//;
  $timelc .= $_.",";
   }
   $timelc =~ s/,$//;
   ($sec,$min,$hour,$mday,$mon,$year) = split /,/,$timelc;
   print "sec:$sec\n min:$min\n hour: $hour\n mday: $mday\n mon: $mon\n year: $year\n";
$year -= 1900;
$mon -= 1;
$timelc = timelocal($sec,$min,$hour,$mday,$mon,$year);
return $timelc;
}

my $mdatetime="2011-05-19 14:17:20";
my $cdatetime="2011-05-19 15:10:12";
my $mtimesec=&gettimemun($mdatetime);
my $ctimesec=&gettimemun($cdatetime);
my $difminute=int(($ctimesec-$mtimesec));#得到两个时间相差秒数
print $mtimesec." ".$ctimesec." ".$difminute."\n";
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics