Skip to content

Instantly share code, notes, and snippets.

@ichigotake
Last active October 11, 2015 16:17
Show Gist options
  • Select an option

  • Save ichigotake/3885271 to your computer and use it in GitHub Desktop.

Select an option

Save ichigotake/3885271 to your computer and use it in GitHub Desktop.
月の休日一覧を出力する。 format: $ echo_holidays.pl [year] [month]
#!/bin/env perl
use strict;
use warnings;
use Calendar::Japanese::Holiday;
use Time::Local qw/timelocal/;
use Encode qw/encode/;
my (undef,undef,undef, undef,$now_mon,$now_year) = localtime(time);
my $year = $ARGV[0] || ($now_year+1900);
my $mon = $ARGV[1] || ($now_mon+1);
my $holidays = _get_holidays( $year, $mon );
my @weeks = qw/日 月 火 水 木 金 土/;
print "$year年\n";
print "----\n";
foreach my$d( sort { $a <=> $b } keys %$holidays ) {
my $wday = $weeks[ $holidays->{$d}->{wday} ];
my $name = encode('utf-8', $holidays->{$d}->{name});
print "$mon/$d($wday) $name\n";
}
sub _get_holidays {
my ($year, $mon) = @_;
my (undef,undef,undef, undef,$now_mon,$now_year) = localtime(time);
$mon ||= $now_mon + 1;
$year ||= $now_year + 1900;
my %holidays;
my $holidays = getHolidays($year, $mon);
for my$day( 1..31 ) {
# check date exists
my $time;
eval {
$time = timelocal(0,0,0, $day,$mon-1,$year);
};
last if $@;
my (undef,undef,undef, undef,undef,undef, $wday) = localtime($time);
# add day in $holidays when date is holiday
if ( $wday == 6 || $wday == 0 || isHoliday($year, $mon, $day) ) {
my $name = $holidays->{ $day } || '';
$holidays{ $day } = {
wday => $wday,
name => $name,
};
}
}
return \%holidays;
}
1;
__DATA__
=encoding utf8
=head1 NAME
echo_holidays.pl - 月の休日の一覧を出力します。
=head1 SYNOPSIS
$ perl echo_holidays.pl 2013 3
2013年
----
3/2(土)
3/3(日)
3/9(土)
3/10(日)
3/16(土)
3/17(日)
3/20(水) 春分の日
3/23(土)
3/24(日)
3/30(土)
3/31(日)
=head1 TODO
- 振り替え休日が考慮されていない
- 依存モジュールの定義ファイルを設置
=head1 AUTHOR
ichigotake
=cut
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment