Date::Simple, Date::Range パッケージを使用して日付の取得・比較を行う。
use strict;
use utf8;
use Date::Simple;
use Date::Range;
# Date::Simple で同じことを実現
#my $today = today();
my $today = Date::Simple::today();
print "Today: " . $today . "\n";
# 指定日の Date::Simple オブジェクトを生成する
my $some_day = Date::Simple::date('2009-01-01');
print $some_day . "\n";
# 1 ヶ月の日数を表示する
print "Days in month: " . Date::Simple::days_in_month($today->year, $today->month) . "\n";
# 当月の初日・末尾を取得
my $first = Date::Simple::date($today->format("%Y-%m-01"));
my $last = Date::Simple::date($today->format("%Y-%m-" . Date::Simple::days_in_month($today->year, $today->month)));
print "First: " . $first . "\n";
print "Last : " . $last . "\n";
# 指定の日が範囲内か確認する
my $range = Date::Range->new($first, $last);
print "Range start: " . $range->start . "\n";
print "Range end : " . $range->end . "\n";
if ($range->includes($some_day)) {
print $some_day . " is between " . $range->start . " and " . $range->end . ".\n";
}
else {
print $some_day . " is NOT between " . $range->start . " and " . $range->end . ".\n";
}
if ($range->includes($today)) {
print $today . " is between " . $range->start . " and " . $range->end . ".\n";
}
else {
print $today . " is NOT between " . $range->start . " and " . $range->end . ".\n";
}
実行結果
Today: 2009-04-24
2009-01-01
Days in month: 30
First: 2009-04-01
Last : 2009-04-30
Range start: 2009-04-01
Range end : 2009-04-30
2009-01-01 is NOT between 2009-04-01 and 2009-04-30.
2009-04-24 is between 2009-04-01 and 2009-04-30.
0 件のコメント:
コメントを投稿