Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Calendar Array

by rob_au (Abbot)
on Sep 09, 2001 at 14:18 UTC ( [id://111266]=sourcecode: print w/replies, xml ) Need Help??
Category: Miscellaneous
Author/Contact Info
Description: For a web application that I have been working on, I needed to display a small calendar with each day of the month lined up within day of the week columns. After searching the site, most answers to questions asking for something like this direct people to existing CPAN modules such as HTML::CalendarMonthSimple and PlotCalendar::Month. The problems with these solutions for my project is that I wanted to have more control over the HTML formatting output by the calendar routine.

The result is this snippet of code which returns a data structure which can be passed directly to HTML::Template for display as desired. eg.

<table border="0" cellpadding="2" cellspacing="0" width="140"> <tr> <td align="left" colspan="1" rowspan="1" valign="middle" width="20 +"> <font color="#000000" face="Tahoma, Verdana, Arial" size="2"><b> +S</b></font> </td> <td align="left" colspan="1" rowspan="1" valign="middle" width="20 +"> <font color="#000000" face="Tahoma, Verdana, Arial" size="2"><b> +M</b></font> </td> <td align="left" colspan="1" rowspan="1" valign="middle" width="20 +"> <font color="#000000" face="Tahoma, Verdana, Arial" size="2"><b> +T</b></font> </td> <td align="left" colspan="1" rowspan="1" valign="middle" width="20 +"> <font color="#000000" face="Tahoma, Verdana, Arial" size="2"><b> +W</b></font> </td> <td align="left" colspan="1" rowspan="1" valign="middle" width="20 +"> <font color="#000000" face="Tahoma, Verdana, Arial" size="2"><b> +T</b></font> </td> <td align="left" colspan="1" rowspan="1" valign="middle" width="20 +"> <font color="#000000" face="Tahoma, Verdana, Arial" size="2"><b> +F</b></font> </td> <td align="left" colspan="1" rowspan="1" valign="middle" width="20 +"> <font color="#000000" face="Tahoma, Verdana, Arial" size="2"><b> +S</b></font> </td> </tr> <tmpl_loop name="row"> <tr> <tmpl_loop name="day"><td align="left" colspan="1" rowspan="1" val +ign="middle" width="20"> <font color="#333333" face="Tahoma, Verdana, Arial" size="2"><tm +pl_var name="day"></font> </td> </tmpl_loop> </tr></tmpl_loop> </table>

In addition to this, for the project for which this was developed for, I passed an additional key that referenced a style dictated by the cascading style sheet for the HTML template. The flexibility of data set and template integration in this manner (in my opinion) surpasses that of some of the existing CPAN calendar modules - An example of this code in practice can be seen here.
#!/usr/bin/perl -w

use Data::Dumper;
use Date::Calc qw/Day_of_Week Days_in_Month/;

use strict;

print STDOUT Dumper(&calendar_array);
exit 0;


sub calendar_array (;$$) {
    my $year = ($_[0] =~ /^\d{4}$/) ? $_[0] : (((localtime)[5]) + 1900
+);
    my $month = (($_[1] =~ /^\d+$/) && (($_[1] > 0) && ($_[1] < 13))) 
+? $_[1] : (((localtime)[4]) + 1);
    my (@calendar, @output);
    my $day = 1 - Day_of_Week($year, $month, 1);
    while ($#calendar < 41) {
        push (@calendar, (($day <= 0) || ($day > Days_in_Month($year, 
+$month))) ? '' : $day);
        ++$day;
    }
    for (my $row = 0; $row < 6; $row++) {
        my @columns;
        for (my $col = 0; $col < 7; $col++) {
            push (@columns, {
                'day'       =>  $calendar[$row * 7 + $col],
            });
        }
        push (@output, {
            'row'  =>  \@columns
        });
    }
    return \@output;
}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://111266]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-18 17:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found