徐小玉的博客。
分类:
2008-04-03 13:07:07
#!/usr/bin/perl -w
########核心问题: 二维数组与年月日的计算。
use Time::Local;
my $stdin = shift;
my ($mmm,$yyyy) = split (/\//,$stdin) ;
$mm=$mmm-1;
if ($mmm eq 1)
{
$mm=12;
}
my $epoch_seconds = timelocal(0, 0, 0, 1, $mm, $yyyy);
#为了计算某个二月份到底是28还是29天,采取计算它的下一个月是这年的第60天还是59天,如果是59天,说明不是闰年,2月该有28天。
my $epoch_seconds_Feb=timelocal(0, 0, 0, 1, $mmm, $yyyy);
my $yday_Feb=(localtime($epoch_seconds_Feb))[7];
my ($seconds, $minutes, $hours, $day_of_month, $month, $year,
$wday, $yday, $isdst) = localtime($epoch_seconds);
my $year_show=$year+1900;
print "$year_show 年 $month 月:\n";
printf "%-8s%-8s%-8s%-8s%-8s%-8s%-8s \n","SUN","MON","TUE","WEN","THU","FRI","SAT";
# 调用计算日期的函数
my ($c_month,$c_year);
&get_count_of_month;
#定义一个二维数组,初始为空,存贮每日的日期,5行7列
my @array=(["","","","","","",""],["","","","","","",""],["","","","","","",""],["","","","","","",""],["","","","","","",""]);
my ($line,$row);
$row=$wday;
$line=0;
my $k;
for ($k=1;$k <= $c_month;$k++)
{
$array[$line][$row]= $k;
$row++;
if ($row eq 7)
{
$line++;
$row=0;
}
}
#######打印出来。
my $i;
my $j;
for ($j=0;$j<=4;$j++)
{
for ($i=0;$i<=6;$i++)
{
printf "%-8s",$array[$j][$i];
}
print "\n";
}
#得到一个月有多少天的函数
sub get_count_of_month
{
$c_year=$year+1900;
if ($month eq 0 || $month eq 2 || $month eq 4 || $month eq 6 || $month eq 7 || $month eq 9 || $month eq 11)
{
$c_month=31;
}
else
{
if ( $month eq 3 || $month eq 5 || $month eq 8 || $month eq 10)
{
$c_month=30;
}
###########计算2月份的日期。
if ( $yday_Feb eq 60)
{
$c_month=29;
}
if ( $yday_Feb eq 59)
{
$c_month=28;
}
###########
}
}