# date.pl
# 内容 :当日・前日・前営業日、前月末日・当月末日・翌月末日を取得する
# Copyright (c) 2002-2011 Mitsuo Minagawa, All rights reserved.
# (minagawa@fb3.so-net.ne.jp)  
# 使用方法 : c:\>perl date.pl
use Time::Local qw( timelocal );
use Calendar::Japanese::Holiday;

open(OUT1,">output.txt");

#
# 当日の日時を取得
#
($sec,$min,$hours,$day,$month,$year,$wday,$yday,$isdst)	= localtime();
$year	=	$year	+	1900;
$month	=	$month	+	1;
print	OUT1	"当日 = ",$year,"/",$month,"/",$day," ",$hours,":",$min,":",$sec."\n";
#
# 前日の日時を取得:24時間×60分×60秒前
#
($sec,$min,$hours,$day,$month,$year,$wday,$yday,$isdst)	= localtime(time - 86400);	
$year	=	$year	+	1900;
$month	=	$month	+	1;
print	OUT1	"前日 = ",$year,"/",$month,"/",$day," ",$hours,":",$min,":",$sec."\n";

# 前営業日の日付を取得する（国民の祝日も考慮する）
# （火曜日から金曜日までは前日、月曜日は、前週の金曜日とする）
#
($sec,$min,$hours,$day,$month,$year)	= (localtime(time - 86400))[0,1,2,3,4,5];
$w_time = timelocal( $sec, $min, $hours, $day, $month, $year );
$w_year		=	$year	+	1900;
$w_month	=	$month	+	1;
$w_day		=	$day;
#while	($wday == 0 or $wday == 6)		{	#土曜日・日曜日は繰り返す
while	(($wday == 0 or $wday == 6)  #土曜日・日曜日は繰り返す
	or	(isHoliday($w_year, $w_month, $w_day)))	{ #国民の休日は除く
		$w_time	=	$w_time	-	86400;
		($day,$month,$year,$wday)	=	(localtime($w_time))[3,4,5,6];
		$w_year		=	$year	+	1900;
		$w_month	=	$month	+	1;
		$w_day		=	$day;
}
print	OUT1	"前営業日 = ",$w_year,"/",$w_month,"/",$w_day,"\n";
#
# 前月末日を取得:(当月1日の前日)
#
($sec,$min,$hours,$day,$month,$year)	= (localtime())[0,1,2,3,4,5];

$day 	=	1;

$w_time = timelocal( $sec, $min, $hours, $day, $month, $year );

($day,$month,$year)	= (localtime($w_time - 86400))[3,4,5];
$year	=	$year	+	1900;
$month	=	$month	+	1;
print	OUT1	"前月末日 = ",$year,"/",$month,"/",$day,"\n";

#
# 当月末日を取得:(翌月1日の前日)
#
($sec,$min,$hours,$day,$month,$year)	= (localtime())[0,1,2,3,4,5];

$day 	=	1;

if		($month		==	11)		{	#12月なら翌年1月
		$year		=	$year	+	1;
		$month		=	1;
}
else						{
		$month		=	$month	+	1;
}

$w_time = timelocal( $sec, $min, $hours, $day, $month, $year );

($day,$month,$year)	= (localtime($w_time - 86400))[3,4,5];
$year	=	$year	+	1900;
$month	=	$month	+	1;
print	OUT1	"当月末日 = ",$year,"/",$month,"/",$day,"\n";

#
# 翌月末日を取得:(翌々月1日の前日)
#
($sec,$min,$hours,$day,$month,$year)	= (localtime())[0,1,2,3,4,5];

$day 	=	1;

if		($month		==	11)		{	#12月なら翌年2月
		$year		=	$year	+	1;
		$month		=	2;
}
elsif	($month		==	10)		{	#11月なら翌年1月
		$year		=	$year	+	1;
		$month		=	1;
}
else						{
		$month		=	$month	+	2;
}

$w_time = timelocal( $sec, $min, $hours, $day, $month, $year );

($day,$month,$year)	= (localtime($w_time - 86400))[3,4,5];
$year	=	$year	+	1900;
$month	=	$month	+	1;
print	OUT1	"翌月末日 = ",$year,"/",$month,"/",$day,"\n";

close(OUT1);
