使用Perl语言实现时间加减

程序如下:

#usr/bin/perl
#
# 1.脚本功能:实现时间加减
# 2.脚本使用:perl date.pl 20171231 +1
#             用来计算2017年12月31日的后一天的日期字符串 
#             perl date.pl 20171231 -3
#             用来计算2017年12月31日的前三天的日期字符串
# 3.程序作者:陈宇昂
#
########################################################
use strict;

my $year;      #年  
my $month;     #月
my $day;       #日
my $intMonth;  #数值类型月
my $intDay;    #数值类型日
my $dayNum;    #增加、减少的天数
my $logo=0;    #判断是否为闰年的标志,1为闰年,0为非闰年


#日期的加法运算
sub date_add(){
    
    my $dayMax = 0;  #每月最多的天数

    if( grep /^$intMonth$/,(1,3,5,7,8,10,12) ){
        
        $dayMax = 31;
    }
    elsif( grep /^$intMonth$/,(4,6,9,11) ){
        
        $dayMax = 30; 
    }
    elsif( $logo==1 and $intMonth==2 ){
        
        $dayMax = 29;
    }
    elsif( $logo==0 and $intMonth==2 ){
        
        $dayMax = 28;
    }
    
    if( $intDay + $dayNum <= $dayMax ){
    
        $intDay = $intDay + $dayNum ; 
    }
    else{
    
        while( $intDay + $dayNum > $dayMax ){
        
            $dayNum = $dayNum - ($dayMax - $intDay + 1);
            $intMonth ++;
            if( $intMonth > 12 ){
            
                $year ++;
                if( ($year%4==0 and $year%100!=0) or $year%400==0 ){
    
                    $logo = 1;
                }
                else{
                
                    $logo = 0;
                }
                
                if( $year > 2999 )
                {
                
                    print "Error! The Max Year More Than 2999!\n";
                    exit(1);
                }
                $intMonth = 1;
            }
            $intDay = 1;
            
            if( grep /^$intMonth$/,(1,3,5,7,8,10,12) ){
            
                $dayMax = 31;
            }
            elsif( grep /^$intMonth$/,(4,6,9,11) ){
            
                $dayMax = 30; 
            }
            elsif( $logo==1 and $intMonth==2 ){
            
                $dayMax = 29;
            }
            elsif( $logo==0 and $intMonth==2 ){
            
                $dayMax = 28;
            }                   
        }
        $intDay = $intDay + $dayNum ; 
    }

    $day = ($intDay<10)? "0$intDay":$intDay;
    $month = ($intMonth<10)? "0$intMonth":$intMonth;
    
    print "$year$month$day\n";
}


#日期的减法运算
sub date_subtract(){
    
    my $dayMax = 0;  #每月最多的天数

    if( grep /^$intMonth$/,(1,3,5,7,8,10,12) ){
        
        $dayMax = 31;
    }
    elsif( grep /^$intMonth$/,(4,6,9,11) ){
        
        $dayMax = 30; 
    }
    elsif( $logo==1 and $intMonth==2 ){
        
        $dayMax = 29;
    }
    elsif( $logo==0 and $intMonth==2 ){
        
        $dayMax = 28;
    }
    
    if( $intDay > $dayNum ){
    
        $intDay = $intDay - $dayNum;
    }
    else{
    
        while( $intDay <= $dayNum ){
        
            $dayNum = ($dayNum - $intDay);
            $intMonth --;
            if( $intMonth < 1 ){
            
                $year--;
                if( ($year%4==0 and $year%100!=0) or $year%400==0 ){
    
                    $logo = 1;
                }
                else{
                
                    $logo = 0;
                }
                
                if( $year < 1949 ){
                
                    print "Error! The Min Year Less Than 1949!\n";
                    exit(1);
                }
                $intMonth = 12;
            }
            
            if( grep /^$intMonth$/,(1,3,5,7,8,10,12) ){
        
                $dayMax = 31;
            }
            elsif( grep /^$intMonth$/,(4,6,9,11) ){
                
                $dayMax = 30; 
            }
            elsif( $logo==1 and $intMonth==2 ){
                
                $dayMax = 29;
            }
            elsif( $logo==0 and $intMonth==2 ){
                
                $dayMax = 28;
            }
            
            $intDay = $dayMax;          
        }
        
        $intDay = $intDay - $dayNum;
    }
    
    $day = ($intDay<10)? "0$intDay":$intDay;
    $month = ($intMonth<10)? "0$intMonth":$intMonth;
    
    print "$year$month$day\n";

}


#判断输入的时间参数是否合法
sub judge_date_arg_current(){

    if( !($ARGV[0] =~ /[0-9]{8}/ and substr($ARGV[1],1) =~ /[0-9]+/) ){
    
        print "Error,Time Parameter Illegalr!\n";
        exit(1);
    }
    elsif( $year>=2999 or $year<1949 or $intMonth>12 or $intMonth<1 or $intDay<1 ){
    
        print "Error,Time Parameter Illegalr!\n";
        exit(1);
    }
    elsif( (grep /^$intMonth$/,(1,3,5,7,8,10,12) and $intDay>31) or (grep /^$intMonth$/,(4,6,9,11) and $intDay>30) 
            or ($logo==1 and $intMonth==2 and $intDay>29) or ($logo==0 and $intMonth==2 and $intDay>28) ){
    
        print "Error,Time Parameter Illegal!\n";
        exit(1); 
    }

}

#主函数,程序的入口
sub main(){

    if( scalar @ARGV!=2 or length($ARGV[0])<8 or substr($ARGV[1],0,1) ne "+" and substr($ARGV[1],0,1) ne "-" ){
    
        print "Program Parameter Error,The Correct Format Is : perl date.pl 20171231 +1 / perl date.pl 20171231 -3\n";
        exit(1);
    }
    
    $year   = substr( $ARGV[0],0,4 );
    $month  = substr( $ARGV[0],4,2 );
    $day    = substr( $ARGV[0],6,2 );
    $dayNum = substr( $ARGV[1],1 );
    $intMonth = int($month);
    $intDay   = int($day);
    
    if( ($year%4==0 and $year%100!=0) or $year%400==0 ){
    
        $logo = 1;
    }
    
    &judge_date_arg_current();
    
    if( substr($ARGV[1],0,1) eq "+" ){
    
        &date_add();
    }
    elsif( substr($ARGV[1],0,1) eq "-" ){
    
        &date_subtract();
    }
    
}

#执行主函数
&main();

如发现程序的不足和缺陷欢迎指正!

你可能感兴趣的:(使用Perl语言实现时间加减)