2007-01-02
■[Perl] Date::Calc で日付計算
日付に関する操作を行うには、Date::Calc 使うと簡単。
$ perl -MCPAN -e shell
cpan> install Date::Calc
$ vi calcdate.pl
use strict;use warnings;
use Date::Calc qw(:all);
#現在のタイムスタンプ
my ($year, $month, $day, $hour, $min, $sec) = Today_and_Now();
printf ("Timestamp is $year/$month/$day $hour:$min:$sec\n");
#今日の日付
($year, $month, $day) = Today();
printf ("Today is $year/$month/$day\n");
#昨日の日付
my ($d_year, $d_month, $d_day) = Add_Delta_Days($year, $month, $day, -1);
printf ("Yesterday is $d_year/$d_month/$d_day\n");
#一年三ヶ月後の日付
($d_year, $d_month, $d_day) = Add_Delta_YM($year, $month, $day,+1,+3);
printf ("Add_Delta_YM is $d_year/$d_month/$d_day\n");
#指定した日付が実在するかどうか確認。
my $res = check_date(2007, 2, 31);
printf ("2007/2/31 is $res\n");
$res = check_date(2007, 1, 1);
printf ("2007/1/1 is $res\n");
exit;
$ perl calcdate.pl
Timestamp is 2007/1/2 14:31:31Today is 2007/1/2
Yesterday is 2007/1/1
Add_Delta_YM is 2008/4/2
2007/2/31 is 0
2007/1/1 is 1
参考
