| 1 | package Calendar::Any::Util::Calendar; | ||
| 2 | { | ||
| 3 | $Calendar::Any::Util::Calendar::VERSION = '0.5'; | ||
| 4 | } | ||
| 5 | use Calendar::Any; | ||
| 6 | |||
| 7 | require Exporter; | ||
| 8 | our @ISA = qw(Exporter); | ||
| 9 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||
| 10 | calendar $week_start_day | ||
| 11 | ) ] ); | ||
| 12 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 13 | our $week_start_day = 0; | ||
| 14 | |||
| 15 | sub calendar { | ||
| 16 | my ($month, $year, $package) = @_; | ||
| 17 | if ( defined($package) && $package =~ /China|Chinese/) { | ||
| 18 | chinese_simple($month, $year); | ||
| 19 | } else { | ||
| 20 | generic_simple(@_); | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | sub generic_simple { | ||
| 25 | my ($month, $year) = @_; | ||
| 26 | my @month = generic_calendar(@_); | ||
| 27 | my $cal = "$Calendar::Any::month_name[$month-1] $year\n\n"; | ||
| 28 | $cal .= join(" ", map { | ||
| 29 | substr($Calendar::Any::weekday_name[$_], 0, 2) | ||
| 30 | } $week_start_day..6, 0..($week_start_day-1) ). "\n"; | ||
| 31 | $cal .= join("\n", map { | ||
| 32 | join(" ", map { $_ ? sprintf "%2d", $_ : ' ' } @$_); | ||
| 33 | } @month); | ||
| 34 | return $cal; | ||
| 35 | } | ||
| 36 | |||
| 37 | sub generic_calendar { | ||
| 38 | my ($month, $year, $package) = @_; | ||
| 39 | defined($package) || ($package = "Gregorian"); | ||
| 40 | my $module = "Calendar::Any::$package"; | ||
| 41 | eval("require $module"); | ||
| 42 | if ( $@ ) { | ||
| 43 | die "Can't load module $module: $@!\n" | ||
| 44 | } | ||
| 45 | my $start = $module->new(-month => $month, -day => 1, -year => $year); | ||
| 46 | my $weekday = ($start->weekday-$week_start_day) % 7; | ||
| 47 | my $last = $start->last_day_of_month; | ||
| 48 | my @month = ((undef)x$weekday, 1..$last, (undef)x ( 7 - (($weekday+$last)%7 || 7) )); | ||
| 49 | return map { [@month[$_*7..$_*7+6]] } 0..(@month/7-1); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub chinese_simple { | ||
| 53 | my ($month, $year) = @_; | ||
| 54 | my ($months, $newmonth) = chinese_calendar(@_); | ||
| 55 | # print it | ||
| 56 | my $cal = center_han_string( | ||
| 57 | sprintf("%d年%d月 %s年%s", $year, $month, | ||
| 58 | $newmonth->[-1][1]->sexagesimal_name, | ||
| 59 | join(",", map { sprintf "%s%s%d日始", $_->[1]->month_name(), | ||
| 60 | ($_->[1]->last_day_of_month > 29 ? "大" : "小"), | ||
| 61 | $_->[0] } @$newmonth)), | ||
| 62 | 9*7-3) . "\n"; | ||
| 63 | $cal .= join(" ", map { | ||
| 64 | substr($Calendar::Any::weekday_name[$_], 0, 3) . " " . $Calendar::Any::Chinese::weekday_name[$_] | ||
| 65 | } $week_start_day..6, 0..($week_start_day-1)) . "\n"; | ||
| 66 | foreach ( @$months ) { | ||
| 67 | foreach ( @$_) { | ||
| 68 | my $str; | ||
| 69 | if ( $_ ) { | ||
| 70 | $str = sprintf("%2d", $_->[0]); | ||
| 71 | my $cdate = $_->[1]; | ||
| 72 | if ( exists $cdate->{jieqi} ) { | ||
| 73 | $str .= $Calendar::Any::Chinese::jieqi_name[$cdate->{jieqi}].' ' x 2; | ||
| 74 | } elsif ( $cdate->day == 1 ) { | ||
| 75 | # make sure the month name is less than 3 character. the only | ||
| 76 | # exception is '闰十一月' | ||
| 77 | my $month_name = $cdate->month_name; | ||
| 78 | Encode::_utf8_on($month_name); | ||
| 79 | if (length($month_name) >= 3) { | ||
| 80 | $month_name = substr($month_name, 0, 3); | ||
| 81 | } else { | ||
| 82 | $month_name .= " "; | ||
| 83 | } | ||
| 84 | Encode::_utf8_off($month_name); | ||
| 85 | $str .= $month_name; | ||
| 86 | } else { | ||
| 87 | $str .= $cdate->day_name.' 'x2; | ||
| 88 | } | ||
| 89 | } else { | ||
| 90 | $str = ' ' x 8; | ||
| 91 | } | ||
| 92 | $cal .= $str . " "; | ||
| 93 | } | ||
| 94 | $cal .= "\n"; | ||
| 95 | } | ||
| 96 | return $cal; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub chinese_calendar { | ||
| 100 | require Encode; | ||
| 101 | require Calendar::Any::Chinese; | ||
| 102 | my ($month, $year) = @_; | ||
| 103 | unless ( $month=~ /^\d+$/ && $year =~ /^\d+$/ && $month>0 && $month<13 ) { | ||
| 104 | pod2usage(); | ||
| 105 | } | ||
| 106 | my $tz = Calendar::Any::Chinese::timezone($year); | ||
| 107 | my $start = Calendar::Any->new_from_Gregorian($month, 1, $year); # first date of Gregorian | ||
| 108 | my $weekday = ($start->weekday - $week_start_day) % 7; # weekday of first date | ||
| 109 | my $last = $start->last_day_of_month; # last day of this month | ||
| 110 | my @month = ((undef)x$weekday, 1..$last, (undef)x ( 7 - (($weekday+$last)%7 || 7) )); | ||
| 111 | my $adate = $start->absolute_date-1; | ||
| 112 | my @newmonth; | ||
| 113 | foreach ( $weekday..$#month ) { | ||
| 114 | last unless defined $month[$_]; | ||
| 115 | my $cdate = Calendar::Any::Chinese->new($adate+$month[$_]); | ||
| 116 | $month[$_] = [$month[$_], $cdate]; | ||
| 117 | push @newmonth, $month[$_] if $cdate->day == 1; | ||
| 118 | } | ||
| 119 | $start = $month[$weekday]->[1]; | ||
| 120 | # mark up jieqi | ||
| 121 | my $first_jieqi = Calendar::Any::Chinese::next_jieqi_date($adate, $tz); | ||
| 122 | my $second_jieqi = Calendar::Any::Chinese::next_jieqi_date($first_jieqi+1, $tz); | ||
| 123 | $adate++; | ||
| 124 | $month[$first_jieqi-$adate+$weekday]->[1]->{jieqi}=2*($month-1); | ||
| 125 | $month[$second_jieqi-$adate+$weekday]->[1]->{jieqi}=2*$month-1; | ||
| 126 | return ([map { [ @month[$_*7..$_*7+6]] } 0..(@month/7-1)], \@newmonth); | ||
| 127 | } | ||
| 128 | |||
| 129 | sub center_han_string { | ||
| 130 | require Encode; | ||
| 131 | my ($str, $len) = @_; | ||
| 132 | my $enc = $str; | ||
| 133 | Encode::_utf8_on($enc); | ||
| 134 | $enc = Encode::encode('cp936', $enc); | ||
| 135 | my $pad = ($len-length($enc))/2; | ||
| 136 | return ' ' x $pad . $str . ' ' x $pad; | ||
| 137 | } | ||
| 138 | |||
| 139 | 1; | ||
| 140 | |||
| 141 | __END__ |