月末日取得関数(うるう年計算)
以下の関数(getMonthEndDate)は、年を入力に、その年の全月の末日のテーブルを出力します。
use strict; print getMonthEndDate(2008), "\n"; sub getMonthEndDate { my ($year) = @_; my @enddatetbl = (31,28,31,30,31,30,31,31,30,31,30,31); if ($year =~ m/\D/) { return (); } if ($year % 4 == 0) { if ($year % 100 == 0) { if ($year % 400 == 0) { $enddatetbl[1] = 29; } else { $enddatetbl[1] = 28; } } else { $enddatetbl[1] = 29; } } else { $enddatetbl[1] = 28; } return @enddatetbl; }
うるう年計算
手順1.確認する年が 4 で割り切れる場合は手順2.へ、そうでない場合は「その年はうるう年でない」。
手順2.その年が 100 で割り切れる場合は手順3.へ、そうでない場合は「その年はうるう年である」。
手順3.その年が 400 で割り切れる場合は「その年はうるう年である」、そうでない場合は「その年はうるう年でない」。