JBUK has asked for the wisdom of the Perl Monks concerning the following question:

Hi all

I am trying to produce a calender, which I have managed to do using perl modules but I want to avoid using any for this particular task.

I have only got a start of this, and I am really unsure of how to produce the tables in the correct column (Mo, Tu, We, Th, Fr.....) Any suggestions would be great
$start = 1; $end = 31 if $month_no eq "1"; $end = 29 if $month_no eq "2"; $end = 31 if $month_no eq "3"; $end = 30 if $month_no eq "4"; $end = 31 if $month_no eq "5"; $end = 30 if $month_no eq "6"; $end = 31 if $month_no eq "7"; $end = 31 if $month_no eq "8"; $end = 30 if $month_no eq "9"; $end = 31 if $month_no eq "10"; $end = 30 if $month_no eq "11"; $end = 31 if $month_no eq "12"; produce html tables ? etc


Thanks

Replies are listed 'Best First'.
(Ovid) Re: Producing Calender
by Ovid (Cardinal) on Jan 16, 2002 at 04:44 UTC

    JBUK wrote:

    I am trying to produce a calender, which I have managed to do using perl modules but I want to avoid using any for this particular task.

    You have a lot of duplicated code and it happens to give an incorrect answer for February. Code like the following is why I would use a proper module:

    sub days_in_month { my ( $month, $year ) = @_; my @days_in_month = qw/ 31 28 31 30 31 30 31 31 30 31 30 31 /; if ( $month == 1 ) { return 29 if is_leap_year( $year ); } return $days_in_month[ $month ]; } sub is_leap_year { my $year = shift; return ( 0 == $year % 4 and 0 != $year % 100 or 0 == $year % 400 ) + ? 1 : 0; }

    &days_in_month will accurately return the number of days in a given month for and a given year, but it does assume that the month range is zero to eleven. It should be easy to modify, but it shows how apparently simple tasks can quickly become complex. However, I didn't check to see if I have a valid year or month. If other's use your module, they will use it wrong and you'll have to account for that, too :)

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Producing Calender
by rob_au (Abbot) on Jan 16, 2002 at 05:02 UTC
    It just so happens that I have posted some code previously that does just as you ask - It can be found here.

    The only module that this code uses is Date::Calc and what it produces is a data structure for the days of the month, sorted into days of the week columns for display via HTML::Template - An example of the HTML template code and a link to example usage is also provided.

     

    Update

     

    With respect to the desire to perform this calendar function without any additional modules and in light of Ovid's excellent direction above, the Date::Calc methods can be replaced as follows:

    sub Day_of_Week { my ( $year, $month, $day ) = @_; if ( $month < 3 ) { $month += 12; --$year; } my $day_of_week = $day + 2 * $month + int( .6 * ( $month + 1) ) + +$year + int( $year / 4 ) - int( $year / 100 ) + int( $year / 400) + 2 +; $day_of_week = int(( $day_of_week / 7 - int( $day_of_week / 7 )) * + 7 + .5); return $day_of_week; } # From Ovid's post at node 139108 (with some modifications) # sub Days_in_Month { my ( $year, $month ) = @_; my @days_in_month = qw/ 31 28 31 30 31 30 31 31 30 31 30 31 /; if ( --$month == 1 ) { return 29 if leap_year( $year ); } return $days_in_month[ $month ]; } sub leap_year { my $year = shift; return ( 0 == $year % 4 and 0 != $year % 100 or 0 == $year % 400 ) + ? 1 : 0; }

    These subroutines implement the similarly named methods from the Date::Calc module and allow the use of the code here without the Date::Calc module. Note that I have made a couple of small changes to Ovid's code from (Ovid) Re: Producing Calender so that the subroutine behaviour falls in line with that of the modules from Date::Calc.

     

    perl -e 's&&rob@cowsnet.com.au&&&split/[@.]/&&s&.com.&_&&&print'

Re: Producing Calender
by gav^ (Curate) on Jan 16, 2002 at 04:57 UTC
      Problems arise when you search for calender instead of calendar I think.

      At least someone searching for "calender" on perlmonks will find some answers now :)

      Tiago
Re: Producing Calender
by dws (Chancellor) on Jan 16, 2002 at 04:42 UTC
    I am trying to produce a calender, which I have managed to do using perl modules but I want to avoid using any for this particular task.

    If you're looking to borrow code to get this done, you're welcome to mine. I went through the same exercise a couple of years ago. My calendar code, which produces and HTML table, is here.

    The interesting part is figuring out what day of the week the month starts on.