当前位置: 代码迷 >> 综合 >> Perl 获取上月下月和最大天数
  详细解决方案

Perl 获取上月下月和最大天数

热度:5   发布时间:2023-12-06 14:28:18.0

                  获取上月,下月,当月的天数,比较常用,故现在整理并分享给大家。

 

 

#! /usr/bin/perl -w
use warnings;
use strict;
use POSIX qw(strftime);
use Time::Local;
use POSIX qw(ceil floor);

my $strtime='2013-12-01';

调用格式:

----------------------------------------------------------------------------------------

my $Submonth=&getSub_month($strtime,1);
print $Submonth."\n";
$Submonth=&getSub_month($strtime,12);
print $Submonth."\n";

my $Submonth=&getAdd_month($strtime,1);

print $Submonth."\n";

$Submonth=&getAdd_month($strtime,12);

print $Submonth."\n";

$Submonth=&getlastmonday($strtime);
print $Submonth."\n";
$Submonth=&getmaxmonday($strtime);
print $strtime ," ....", $Submonth,"\n";

----------------------------------------------------------------------------------------------------

 

相关函数和过程:

sub getSub_month
 {
## this step must less then 12,
 my ($strtime,$step)=@_;
 my ($y,$m,$d) = split('-',$strtime);
##timelocal($sec,$min,$hour,$mday,$mon,$year);
 my $unixtime=timelocal(0,0,0,$d,$m-1,$y);

 my ($sec,my $min,my $hour,my $day,my $mon,my $year,my $weekday,my $yeardate,my $savinglightday) = (localtime($unixtime));
 $day = '01';
 $mon-=$step;
 if($mon<0)
   {
    $year-=1;
    $mon+=12;
 }
 $mon = ($mon < 9)? "0".($mon+1):($mon+1);
 $year += 1900;
 my $now="$year-$mon-$day";
 return $now;
}

 

sub getAdd_month
 {
## this step must less then 12,
 my ($strtime,$step)=@_;
 my ($y,$m,$d) = split('-',$strtime);
##timelocal($sec,$min,$hour,$mday,$mon,$year);
 my $unixtime=timelocal(0,0,0,$d,$m-1,$y);

 my ($sec,my $min,my $hour,my $day,my $mon,my $year,my $weekday,my $yeardate,my $savinglightday) = (localtime($unixtime));
 $day = '01';
 $mon+=$step;
 if($mon>11)
   {
    $year+=1;
    $mon-=12;
 }
 $mon = ($mon < 9)? "0".($mon+1):($mon+1);
 $year += 1900;
 my $now="$year-$mon-$day";
 return $now;
}

sub getlastmonday
{
 my ($curmon)=@_;
 my $nextmon=&getAdd_month($curmon,1);
 my ($y,$m,$d) = split('-',$nextmon);
 my $unixtime=timelocal(0,0,0,$d,$m-1,$y);
    $unixtime-=24*60*60;  ##get before day
 my ($sec,my $min,my $hour,my $day,my $mon,my $year,my $weekday,my $yeardate,my $savinglightday) = (localtime($unixtime));

 $day = ($day < 10)? "0$day":$day;
 $mon = ($mon < 9)? "0".($mon+1):($mon+1);
 $year += 1900;
 my $now = "$year-$mon-$day";
 return $now;
}

sub getmaxmonday
{
 my ($curmon)=@_;
 my $nextmon=&getAdd_month($curmon,1);
 my ($y,$m,$d) = split('-',$nextmon);
 my $unixtime=timelocal(0,0,0,$d,$m-1,$y);
    $unixtime-=24*60*60;  ##get before day
 my ($sec,my $min,my $hour,my $day,my $mon,my $year,my $weekday,my $yeardate,my $savinglightday) = (localtime($unixtime));
 my $now = "$day";
 return $now;
}

 

sub getTime(){
 (my $sec,my $min,my $hour,my $day,my $mon,my $year,my $weekday,my $yeardate,my $savinglightday) = (localtime(time));
 $sec = ($sec < 10)? "0$sec":$sec;
 $min = ($min < 10)? "0$min":$min;
 $hour = ($hour < 10)? "0$hour":$hour;
 $day = ($day < 10)? "0$day":$day;
 $mon = ($mon < 9)? "0".($mon+1):($mon+1);
 $year += 1900;
 my $now = "$year-$mon-$day $hour:$min:$sec";
 return $now;
}

 

以上函数可以方便的计算指定日期的上N个月,下N 个月,和获取指定日期的月份最后一日和指定日期月份有多少天。

现在的函数适合月份跨度是12个月内的。