#最近做東西老用到處理時間的問題,索性寫幾個函數。#!/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."}}