zoukankan      html  css  js  c++  java
  • Perl 处理日期的自定义函数

    #最近做东西老用到处理时间的问题,索性写几个函数。
    #!/usr/bin/perl -w
    #===============================================================================
    # Copyright (c) 2009, 
    # All rights reserved.
    # 摘    要 :一些常用的时间处理函数
    # 作    者 :2009-12-15 xingwang
    # 版    本 :0.01
    #===============================================================================
    use strict;
    
    # ------------------------------
    # &subtime(date,n)
    # 计算 某天的前 n 天或者后 n 天
    # 参数:日期,前n天(n)或后n天(-n)
    # ------------------------------
    sub subtime {
    my ($date,$number) = @_;
    use HTTP::Date;
    my $n = str2time($date);
    $n += 86400 * $number;
    my ($s,$mm,$h,$d,$m,$y) = (localtime($n))[0,1,2,3,4,5];
    $y += 1900;
    $m ++;
    if ( $date =~ /:/ ) {
       return sprintf("%4d-%02d-%02d %02d:%02d:%02d",$y,$m,$d,$h,$mm,$s);
    } else {
       return sprintf("%4d-%02d-%02d",$y,$m,$d);
    }
    }
    print &submonth($ARGV[0],$ARGV[1]),"\n";
    # ------------------------------
    # &submonth(date,n)
    # 计算 某天的前 n 月或者后 n 月
    # 参数:日期,前n月(n)或后n月(-n)
    # ------------------------------
    sub submonth {
    my ($date,$number) = @_;
    use HTTP::Date;
    
    my $n_temp = str2time($date);
    my ($m_temp,$y_temp) = (localtime($n_temp))[4,5];
    $y_temp += 1900;
    $m_temp ++;
    my %hs_monthDays = ('1'=>'31','3'=>'31','5'=>'31','7'=>'31','8'=>'31','10'=>'31','12'=>'31','4'=>'30','6'=>'30','9'=>'30','11'=>'30');
    $hs_monthDays{'2'} = ($y_temp%4==0&&$y_temp%100!=0) || $y_temp%400==0 ? 29 : 28;
    
    
    my $n = str2time($date);
    my $monthdays = $hs_monthDays{$m_temp};
    $n -= 86400 * $monthdays * $number;
    
    my ($s,$mm,$h,$d,$m,$y) = (localtime($n))[0,1,2,3,4,5];
    $y += 1900;
    $m ++; 
    
    if ( $date =~ /:/ ) {
       return sprintf("%4d-%02d-%02d %02d:%02d:%02d",$y,$m,$d,$h,$mm,$s);
    } else {
       return sprintf("%4d-%02d-%02d",$y,$m,$d);
    }
    }
    
    # ------------------------------
    # &nowtime()
    # 当前时间
    # ------------------------------
    sub nowtime {
    my ( $s,$min,$h,$d,$m,$y ) = (localtime(time()))[0,1,2,3,4,5];
    $y += 1900;
    $m ++;
    return sprintf("%4d-%02d-%02d %02d:%02d:%02d",$y,$m,$d,$h,$min,$s);
    }
    
    # ------------------------------
    # &nowdate()
    # 当前日期
    # ------------------------------
    sub nowdate {
    my ( $d,$m,$y ) = (localtime(time()))[3,4,5];
    $y += 1900;
    $m ++;
    return sprintf("%4d-%02d-%02d",$y,$m,$d);
    }
    
    # ----------------------------------------------------
    # &formatdate()
    # 格式化日期
    # 输入的格式:
    # yyyy-mm-dd、yyyy-m-d、yyyymmdd、yyyy/mm/dd、
    # yyyy/m/d、yyyy-mm、yyyy-m、yyyymm、yyyy/mm、yyyy/m
    # ----------------------------------------------------
    sub formatdate {
    my $date = shift;
    my $type = shift;
    
    $date =~ /^(\d{4})[-\/](\d{2}|\d)[-\/](\d{2}|\d)$/g; # yyyy-mm-dd yyyy/mm/dd
    $date =~ /^(\d{4})(\d{2})(\d{2})$/;       # yyyymmdd
    $date =~ /^(\d{4})(\d{2})$/;        # yyyymm
    $date =~ /^(\d{4})[-\/](\d{2}|\d)$/g;      # yyyy-mm yyyy/mm
    
    die "Format Date Erro:The date you input( $date ) is not correct." unless ( $1 or $2 );
    
    if ( $type eq 'yyyymm' ) {
       return sprintf("%04d%02d",$1,$2);
    } elsif ( $type eq 'yyyymmdd' and $3) {
       return sprintf("%04d%02d%02d",$1,$2,$3);
    } elsif ( $type eq 'yyyy-mm-dd' and $3) {
       return sprintf("%04d-%02d-%02d",$1,$2,$3);
    } elsif ( $type eq 'yyyy-mm') {
       return sprintf("%04d-%02d",$1,$2);
    } elsif ( $type eq 'alldays' or $type eq 'endday' ) {
       my $hs_monthDays = {'01'=>'31','03'=>'31','05'=>'31','07'=>'31','08'=>'31','10'=>'31','12'=>'31','04'=>'30','06'=>'30','09'=>'30','11'=>'30'};
       $hs_monthDays->{'02'} = ($1%4==0&&$1%100!=0)||$1%400==0 ? 29 : 28;
       return $hs_monthDays->{$2};
    } else {
       die "Format Date Erro:Return date ( Date = $date ,Type = $type ) is not correct."
    }
    }
    
    
    
  • 相关阅读:
    【poj1733】 Parity game
    【poj1018】 Communication System
    【poj1017】 Packets
    【poj1568】 Find the Winning Move
    【poj1085】 Triangle War
    【bzoj1082】 SCOI2005—栅栏
    【codevs1086】 栈
    【bzoj3240】 Noi2013—矩阵游戏
    【bzoj1951】 Sdoi2010—古代猪文
    中国剩余定理学习笔记
  • 原文地址:https://www.cnblogs.com/LCX/p/1641537.html
Copyright © 2011-2022 走看看