Perl 處理日期的自訂函數

來源:互聯網
上載者:User
#最近做東西老用到處理時間的問題,索性寫幾個函數。#!/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/mmdie "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."}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.