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

I hacked this script and now I'm unable to see the events inserted for the first day of every month. There are two modules that go along with it that I did not touch: mysql_manip.pm, and Read_parse.pm. I have tried to debug the code but I am not even sure which variables to set. If you need to see the modules I can email them to you.
use Cwd; use CGI qw(:standard); use CGI::Carp qw(fatalsToBrowser); use mysql_manip; use Read_Parse; $| = 1; $cwd = cwd(); %param = Read_Parse->new("getall"); $mysql = mysql_manip->Connect($mysql_database,$mysql_user,$mysql_passw +ord); print "Content-Type: text/html\n\n"; %nonleapdays = ( "0" => "31", "1" => "28", "2" => "31", "3" => "30", "4" => "31", "5" => "30", "6" => "31", "7" => "31", "8" => "30", "9" => "31", "10" => "30", "11" => "31", ); %leapdays = ( "0" => "31", "1" => "29", "2" => "31", "3" => "30", "4" => "31", "5" => "30", "6" => "31", "7" => "31", "8" => "30", "9" => "31", "10" => "30", "11" => "31", ); $count = $mysql->Select("events","id"); $id = 0; $id = $mysql->[$count] if ($count > 0); $tmpmon = $param{month}; $tmpmon--; if ($param{view}) { ($vday,$vmon,$vyear) = split(/\-/,$param{view}); $count = $mysql->Select("events","id,subject,description","whe +re day=$vday and month=$vmon and year=$vyear"); until ($count == 0) { ($eventid,$subject,$description) = split(/\|/,$mysql->[$count] +); print qq~ <center> <center> <table width="$table_width" border="$table_border" cellspacing="$table +_cellspacing" cellpadding="$table_cellpadding" bordercolor="$t able_bordercolor"> <tr bgcolor=$table_title_bgcolor> <td><font size=$table_title_text_size face=$table_title_text_font colo +r="$table_title_text_color">$vday/$vmon/$vyear - $subject</fon t></td> </tr> <tr bgcolor="$table_day_bgcolor" > <td> <table width=95% border=0><tr><td>$description</td></tr> </table> </td> </tr> </table> </center> ~; $count--; } } $count = undef; @days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday',' +Saturday'); @months = ('January','February','March','April','May','June','July','A +ugust','September','October','November','December'); $timeoffset = $param{jumpto} * 24 ; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = local +time(time + (3600*$timeoffset)); ($Tsec,$Tmin,$Thour,$Tmday,$Tmon,$Tyear,$Twday,$Tyday,$Tisdst) + = localtime(time); $savehour = $hour; $hour = "0$hour" if ($hour < 10); $min = "0$min" if ($min < 10); $sec = "0$sec" if ($sec < 10); $year = 1900 + $year; $Tsavehour = $Thour; $Thour = "0$Thour" if ($Thour < 10); $Tmin = "0$Tmin" if ($Tmin < 10); $Tsec = "0$Tsec" if ($Tsec < 10); $Tyear = 1900 + $Tyear; $sectime = time; $sectime = $sectime + (3600*$timeoffset); $date = "$days[$mday]/$mon/$mday/$year/"; $date .= $sectime; ($currentweekday,$currentmonth,$currentday,$currentyear,$sectime) = sp +lit(/\//,$date); $last = $currentday - (($currentday * 2) + 1); $last = $last + $param{jumpto}; $next; if ($currentyear % 4) { $next = $leapdays{$currentmonth} - $currentday + 1 + $param{jumpto}; } else { $next = $nonleapdays{$currentmonth} - $currentday + 1 + $param +{jumpto}; } $tmp = $year - 25; until ($tmp == ($year + 25)) { $option .= "<option>$tmp</option>\n"; $tmp++; } print <<endofhtml; <html> <head> <title>$title</title> </head> <body bgcolor="$bgcolor" text="$text" link="$link" vlink="$vlink" alin +k="$alink" background="$background"> <center><BR><BR><BR><BR> <h1>$months[$mon] $year</h1> <br> <table width=$table_width border=0><tr><td width=50%> <font face=arial color=white><a href="mysql_calendar.cgi?jumpto=$last" +>View Last Month</a></font> </td><td width=50%> <div align=right><font face=arial color=white><a href="mysql_calendar. +cgi?jumpto=$next">View Next Month</a></font></div> </td></tr></table> <table width="$table_width" border="$table_border" cellspacing="$table +_cellspacing" cellpadding="$table_cellpadding" bordercolor="$t able_bordercolor"> <tr bgcolor=$table_title_bgcolor> <td width=$cell_width><font size=$table_title_text_size face=$tabl +e_title_text_font color="$table_title_text_color">Sunday</font ></td> <td width=$cell_width><font size=$table_title_text_size face=$tabl +e_title_text_font color="$table_title_text_color">Monday</font ></td> <td width=$cell_width><font size=$table_title_text_size face=$tabl +e_title_text_font color="$table_title_text_color">Tuesday</fon t></td> <td width=$cell_width><font size=$table_title_text_size face=$tabl +e_title_text_font color="$table_title_text_color">Wednesday</f ont></td> <td width=$cell_width><font size=$table_title_text_size face=$tabl +e_title_text_font color="$table_title_text_color">Thursday</fo nt></td> <td width=$cell_width><font size=$table_title_text_size face=$tabl +e_title_text_font color="$table_title_text_color">Friday</font ></td> <td width=$cell_width><font size=$table_title_text_size face=$tabl +e_title_text_font color="$table_title_text_color">Saturday</fo nt></td> </tr> <tr bgcolor="$table_day_bgcolor" > endofhtml $day = ($currentday - 1) * 86400; $count; $sectime = $sectime - $day; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($sec +time); $year = $year + 1900; while ($wday != 0) { $wday--; print "<td height=$cell_height valign=top><div align=right><B> +<font size=$table_day_size color=$table_day_color face=$table_ day_face>&nbsp</font></b></div></td>"; $count++; } $desc; foreach $i (@events) { ($eventYEAR,$eventMONTH,$eventDAY,$eventDESCRIPTION) = split(/ +\|\^\|/,$i); if ($eventYEAR == $year && $eventMONTH == $mon && $eventDAY == + $mday) { $desc = $eventDESCRIPTION; } } $color; if ($year == $Tyear && $mon == $Tmon && $mday == $Tmday) { $color = "bgcolor=$table_currentday_bgcolor"; } $tmpmon = $mon; $tmpmon++; #$tmp = $mysql->Select("events","subject,description","where user=\"$u +sername\" and year=\"$year\" and month=\"$tmpmon\" and day=\"$ mday\""); $tmp = $mysql->Select("events","subject,description","where year=\"$ye +ar\" and month=\"$tmpmon\" and day=\"$mday\""); until ($tmp == 0) { ($subject,$description) = split(/\|/,$mysql->[$tmp]) if ($tmp +>= 1); $jumpto = $timeoffset / 24; if ($jumpto) { $jumpto = "jumpto=$jumpto&" } else { $jumpto = +undef } $event .= "<a href=\"mysql_calendar.cgi?$jumpto\view=$mday-$tm +pmon-$year\">$subject</a><br>"; $tmp--; } print "<td height=$cell_height valign=top $color><div align=right><B>< +font size=$table_day_size color=$table_day_color face=$table_d ay_face>$mday</font></b></div><BR><center><table width=80><tr><Td vali +gn=top><font size=-2 color=$table_day_color face=$table_day_fa ce>$desc</font></td></tr></table></center></td>"; $desc = ''; $color = ''; $tmpmon=undef; $tmp=undef; $event = undef; $subject = undef; $description = undef; $count++; $PRIMEmon = $currentmonth + 1; if ($PRIMEmon == 12) { $PRIMEmon = 0; } while ($mon ne $PRIMEmon) { if ($count == 7) { print "</tr><tr bgcolor=\"$table_day_bgcolor\" >\n"; $count = 0; } $sectime = $sectime + 86400; ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) += localtime($sectime); $year = $year + 1900; if ($PRIMEmon ne $mon) { # print "$months[$mon]/$mday/$year<BR>"; foreach $i (@events) { ($eventYEAR,$eventMONTH,$eventDAY,$eventDESCRIPTION) = split(/ +\|\^\|/,$i); if ($eventYEAR == $year && $eventMONTH == $mon && $eventDAY == + $mday) { $desc = $eventDESCRIPTION; } } if ($year == $Tyear && $mon == $Tmon && $mday == $Tmday) { $color = "bgcolor=$table_currentday_bgcolor"; } $tmpmon = $mon; $tmpmon++; #$tmp = $mysql->Select("events","subject,description","where user=\"$u +sername\" and year=\"$year\" and month=\"$tmpmon\" and day=\"$ mday\""); $tmp = $mysql->Select("events","subject,description","where year=\"$ye +ar\" and month=\"$tmpmon\" and day=\"$mday\""); until ($tmp == 0) { ($subject,$description) = split(/\|/,$mysql->[$tmp]) if ($tmp +>= 1); $jumpto = $timeoffset / 24; if ($jumpto) { $jumpto = "jumpto=$jumpto&" } else { $jumpto = +undef } $event .= "<a href=\"mysql_calendar.cgi?$jumpto\view=$mday-$tm +pmon-$year\">$subject</a><br>"; $tmp--; } print "<td height=$cell_height valign=top $color><div align=right><B>< +font size=$table_day_size color=$table_day_color face=$table_d ay_face>$mday</font></b></div><center><table width=80><tr><Td valign=t +op><font size=-2 color=$table_day_color face=$table_day_face>$ event</font></td></tr></table></center></td>"; $count++; $desc = ''; $color = ''; $tmpmon=undef; $tmp=undef; $event = undef; $subject = undef; $description = undef; } } until ($count == 7) { print "<td height=$cell_height valign=top><div align=r +ight><B><font size=$table_day_size color=$table_day_color face =$table_day_face>&nbsp</font></b></div></td>"; $count++; } print "</table>"; print <<endofhtml; <a href="addevent/calendar.cgi">ADD EVENTS</a> endofhtml

Replies are listed 'Best First'.
Re: unable to see entries from 1st day of the month.
by grep (Monsignor) on Mar 28, 2002 at 01:54 UTC

    Right off the bat I see some big problems.

    if ($currentyear % 4) { $next = $leapdays{$currentmonth} - $currentday + 1 + $param{jumpto}; } else { $next = $nonleapdays{$currentmonth} - $currentday + 1 + $param +{jumpto}; }

    This is wrong, leap years have 2 more rules associated with them.

  • If the year is divisible by 4, it is a leap year, UNLESS
  • The year is also divisible by 100, then it's not a leap year, UNLESS
  • The year is also divisible by 400, then it is a leap year
  • Anytime I think to myself someone has probably done this (leap years and date manipulation) before, I think, 'I should look on CPAN'. One CPAN module you'll find is Date::Calc you should run and grab this.

    It's not a hard module to use and I guarentee you'll have your problem (plus others) fixed if you implement it.



    grep
    grep> cd /pub
    grep> more beer
      I have installed Date::Pcalc module which is an all perl version of the Date::Calc. Now could you give me an idea as to how I would use it in this code.
Re: unable to see entries from 1st day of the month.
by rinceWind (Monsignor) on Mar 28, 2002 at 01:27 UTC
    mnlight,

    You say that you have modified a script, and it is no longer working for events on the first day of the month.

    Golden rule of IT, if there is a possibility of a change you have made causing a problem, back it out. Even if you are totally 100% convinced of the innocence of your change, back it out anyway. If this does not fix the problem, this helps to establish your innocence.

    If the problem goes away, you have some fresh data about a new bug you have introduced.

    Please do not waste the time of monks trying to fathom the code, which itself could do with improving, such as use strict, -w, etc. You also have not posted the changes you have made to the code.

    On a practical note, if you do need to debug the script, try Devel::ptkdb - install this module and append -d:ptkdb to the first (#!) line of the program.

Re: unable to see entries from 1st day of the month.
by Fletch (Bishop) on Mar 28, 2002 at 01:56 UTC

    Also consider using something like Date::Calc or Date::Manip to manipulate dates rather than trying to reinvent the wheel.