Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Monday December 16, 2002
03:24 PM

Chicago.pm meeting dates

[ #9493 ]

For a long time the Chicago.pm website did not have up-to-date meeting dates. The groups meets every third Monday (tonight, for instance), so the next meeting date is not hard to calculate, but most people cannot do it very easily. We can; however, simply publish the dates so even the people who do know how to do that sort of math in their heads can check their work. Formally somebody did this by hand, and as with anything that requires human interaction, sometimes it did not happen.

The Date::* modules in Perl do some amazingly things. In Date::Manip, the ParseRecur function can compute recurring dates like "every third monday". I need to tell it a date on which the event did (or will) occur, a starting date, and an ending date. In this case, Chicago.pm had a meeting on July 15th, so that is my base date. Date::Manip understands "today" as a date, so I use that as my start date, and I want a date four months in the future (to get at least 3 upcoming meeting dates)---DateCalc understands "+ 4 months".

I also have to give thee ParseRecur function a string that describes how the event repeats, in this case "0:1*3:1:0:0:0". The "1*3" says the third thingy of every month, and the "1" following that says "Monday", the second day in the week, counting from 0. The format is a bit mind-bending, but the function's documentation has several examples.

Amazing. This is not an easy calculation, but my actual task is really easy---I can do it by hand simply by looking at my calendar. I want to automate this so I do not have to look at my calendar, not because it is hard. The Date::Manip module lets me translate the date description into a lit bit of code and it does the rest.

In the HTML I use two server-side includes---one that inserts the next meeting date, and one for the upcoming dates.

<!--#include virtual="next_meeting.txt" -->
 
<!--#include virtual="meeting_dates.txt" -->

I only need to get the right information into those files. I add my script to crontab and finally, in over ten years of unix experience (mostly not system administration), I get to use the weekday column of the crontab. I run the command every Monday at 9 pm (any meeting should be in progress). Three out of four times it outputs the same thing already in the files. That inefficiency is better than me forgetting to update the web page.

0 21 * * 1 /usr/home/comdog/bin/chi-pm-dates.pl

The code is very simple. I love the Date::Manip module.

#!/usr/bin/perl
 
use Date::Manip;
 
# save the string to replace the upcoming meeting times placeholder
# in the HTML
open FILE, "> /usr/home/comdog/chicago/meeting_dates.txt";
 
# Every third monday
my @dates = &ParseRecur("0:1*3:1:0:0:0",'July 15, 2002',
                'today',
                &DateCalc( "today","+ 4 months " )
                );
 
foreach my $date ( @dates )
        {
        printf FILE qq|<li>%s\n|,
        &UnixDate($date, "%B %d, %Y"),
        }
 
close FILE;
 
# save the string to replace the next meeting time placeholder
# in the HTML
open FILE, "> /usr/home/comdog/chicago/next_meeting.txt";
print FILE &UnixDate($dates[0], "%B %d, %Y"), " at 7:30pm";
close FILE;