the complete webmaster
tutorials reviews reference
ASP
CGI
FrontPage
HTML
Java
JavaScript

home / articles / cgi

HTML Calendar

This week, I'll start a multi-week project and describe how to build a full-featured calendar control CGI in Perl. First, we'll need to decide how to represent dates for the calendar. Perl has many powerful and useful built-in functions. However, it doesn't really have many date-manipulation features beyond what's available in the standard C library. The CPAN archive undoubetly has a few useful ones. But, why not just come up with our own?

The simplest method might be to use conventional the month, day and year: August 19, 1997. It could be represented internally in Perl as a string: "08/19/1997". Or, we could use three separate integer variables, $month, $day, $year. Using the object oriented extentions available in Perl5, we could create a Date class that completely encapsulates the date functions. However, because the day, month and year are stored separately, we'd need to deal with questions like these:

  • What's the date tomorrow?
  • What's the first day of this month? Is it Sunday? Monday?
  • How many days are in this month?
All these questions can be answered with a little bit of clever programming. We know that most months have 30 or 31 days and could consider whether this is a leap year or not and adjust the number of days in February accordingly. This all seems overly complicated.

A much simpler approach would use a serial date format. This defines January 1st to be 1. Then, January 2nd=2,..., February 1st=32, etc. So the last day of the year (December 31st) is 365 (or 366). This way we could easily determine what the date is tomorrow (add 1 to today's date) and which day of the week it is (serial_date MOD 7). To know how many days are in this month, we'd need to consider how to convert conventional dates (Month, Day, Year) into this serial format.

One method for storing serial dates is using the Julian Day Count. Julian Day 0 is January 1, 4713 BC at 12:00 GMT. For August 19, 1997, the Julian Day is 2,450,679. That means that more than 2.4 million days have passed since January 1, 4713 BC. A Modified Julian Date (MJD) is commonly used to record the date and time. This is a sequence of day numbers, with the value 0 MJD equal to 00:00 hours UTC on November 17, 1858. MJD is commonly represented by a number with five significant digits. The current version of my HTML Calendar uses the Julian Day Count. Soon, I'll convert it to use the MJD and include the ability to schedule events at a particular time.

The source code for the HTML Calendar is available. It's a simple CGI program that will display a calendar and allow you to change which month is displayed.

It will display a calendar similar to the following:

August 1997

Sun Mon Tue Wed Thu Fri Sat
27 28 29 30 31 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6

More next week.

Author: Doug Steinwand
Date: [08/19/97]

More articles about CGI
More articles by Doug Steinwand
Author Biography

write for us about us advertise

Copyright 1997, 1998 A Big Lime. All rights reserved.

/body>