Here's one that I wrote a while ago using Time::Local. It works out for a plain text file but if you wanted to print it to a web page, the $cal string would need carriage returns of <br> instead of "\n" and whitespace padding using &nbsp. This method of incrementing the days doesn't rely on adding '86400' to your previous day.
#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
my @names = qw/ JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC/;
my $year = shift ||'2007';
for my $month (0..11) {
print " $names[$month] $year\n";
print calendar($year, $month), "\n\n";
}
sub calendar {
my ($year, $month) = @_;
my @mon_days = qw/31 28 31 30 31 30 31 31 30 31 30 31/;
++$mon_days[1] if $year % 4 == 0 && ($year % 400 == 0 || $year % 1
+00 != 0);
my $cal = " Sun Mon Tue Wed Thu Fri Sat\n";
# Months are indexed beginning at 0
my $time = timegm(0,0,0,1,$month,$year);
my $wday = (gmtime $time)[6];
$cal .= " " x $wday;
my $mday = 1;
while ($mday <= $mon_days[$month]) {
$cal .= sprintf "%4s", $mday++;
$cal .= "\n" if ($wday + $mday -1) % 7 == 0;
}
return $cal;
}
Chris
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.