码迷,mamicode.com
首页 > 其他好文 > 详细

perl时间处理

时间:2018-06-15 10:55:08      阅读:157      评论:0      收藏:0      [点我收藏+]

标签:sub   argv   bsp   str   ace   for   time   use   perl   


#!/usr/bin/perl

use Time::Local;

my $time1="Thu Jun 14 10:03:53 CST 2018";

my $time2="Thu Jun 14 03:07:09 2018";


sub get_mon

{

($p1)=@_;

my $mon=0;

my $num=-1;

my $monstr="Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec";

my @monlist= split / /, $monstr;

foreach $m (@monlist){

$num=$num+1;

if ($p1 eq $m){

$mon=$num;

}

}

return $mon;

}

   

sub get_istat_time

{

my $TIME1 = "";

   ($p)=@_;

   #my $time1="Thu Jun 14 10:03:53 CST 2018";

   #my $time2="Thu Jun 14 03:07:09 2018";

   if ($p=~/(\w{3})(\s+)(\w{3})(\s+)(\d{2})(\s+)(\d{2})(\:)(\d{2})(\:)(\d{2})(\s+)(\d{4})(.*)/)

      {

        #my $p1="$1";

        #printf "$1,$3,$5,$7,$9,$11,$13,$15\n";

$sec=$11;

$min=$9;

$hour=$7;

$day=$5;

$mon=get_mon $3;

$year=$13;

#printf "$sec, $min, $hour, $day, $mon, $year\n";

      }

elsif ($p=~/(\w{3})(\s+)(\w{3})(\s+)(\d{2})(\s+)(\d{2})(\:)(\d{2})(\:)(\d{2})(\s+)(\w{3})(\s+)(\d{4})(.*)/)

      {

        #my $p1="$1";

        #printf "$1,$3,$5,$7,$9,$11,$13,$15\n";

$sec=$11;

$min=$9;

$hour=$7;

$day=$5;

$mon=get_mon $3;

$year=$15;

#printf "$sec, $min, $hour, $day, $mon, $year\n";

      }

  

   $TIME1 = timelocal($sec, $min, $hour, $day, $mon, $year);

   

   return $TIME1;

}


sub help()

{

  print "usage:\n";

  print "get_time \"Thu Jun 14 10:03:53 CST 2018\"\n";

  print "get_time \"Thu Jun 14 03:07:09 2018\"\n";

  exit 1;

}



sub main()

{

        if(@ARGV<1)

  {

          help();

  }

       

printf(get_istat_time($ARGV[0]));

printf("\n");    

}


main


perl时间处理

标签:sub   argv   bsp   str   ace   for   time   use   perl   

原文地址:http://blog.51cto.com/11310506/2129628

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!