November 26, 2014
Hot Topics:

Building WML Gadgets: A Calendar

  • December 20, 2002
  • By Steve Schafer
  • Send Email »
  • More Articles »

Finding the Right Tool

As mentioned earlier, in an effort to not re-create the wheel, I started with the output from the Unix cal command. Using a filter, I was easily able to add line break tags to the output. However, using a similar filter to insert appropriate table tags would be an exercise in futility. The permutations for what and where to insert row and cell tags are too many to even count, let alone parse.

Instead, I turned to Perl and the Date::Calc module from Steffen Beyer, found at www.engelschall.com. ( http://www.engelschall.com/~sb/download/) This module contains all the date functionality needed for our simple application and will allow us to mimic the output of the cal program while including the changes to make it more legible.

Using Perl and the Date::Calc module, a simple calendar-printing application was easy to accomplish:

Listing of cal.pl

1.   #!/usr/bin/perl  -w

2.   # Define modules
3.   use Date::Calc ':all';

4.   # Get current day/month/year
5.   ($year,$month,$day) = Today([$gmt]);

6.   # Find weekday (Sun - Sat) of first of the month
7.   $firstdow = Day_of_Week($year,$month,"1");
8.   # Convert Sun (7) to 0
9.   if ("$firstdow" == "7") { $firstdow = 0; }

10.  # Month text
11.  $monthtext = Month_to_Text($month);

12.  # Print WML header and beginning tags
13.  print <<ENDHEADER;
14.  <?xml version="1.0"?>
15.  <!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN"
16.  "http://www.wapforum.org/DTD/wml_1.1.xml">

17.  <wml>
18.  <card>
19.  <p>
20.  $monthtext $year<br/>
21.  <table columns="7">
22.  <tr><td> S </td><td> M </td><td> T </td><td> W </td>
23.  <td> T </td><td> F </td><td> S </td></tr>

24.  ENDHEADER

25.  # Start first date row
26.  print "<tr>";

27.  # Print blank cells up to first day
28.  $x = 0;
29.  while ($x < $firstdow) {
30.  print "<td>&nbsp;</td>";
31.  $x++;
32.  }

33.  # Start with first day
34.  $day = 1;

35.  # Work through month from first day through last
36.  while ($day <= Days_in_Month($year,$month)) {
37.  print "<td>";
38.  if ($day < 10) { print "&nbsp;"; }
39.  print $day."|</td>";
40.  $x++;
41.  # At end of week, close row
42.  if ($x == 7) { 
43.  print "</tr>\n";
44.  # If we aren't done, start new row
45.  if ($day != Days_in_Month($year,$month)) {
46.  print "<tr>";
47.  }
48.  $x = 0
49.  }
50.  $day++;
51.  }

52.  # Fill out last row (if applicable)
53.  #   with blank cells
54.  if ($x ne 0) {
55.  while ($x <= 6) {
56.  print "<td>&nbsp;</td>";
57.  $x++;
58.  }
59.  print "</tr>";
60.  }

61.  # Close tags, card, and WML
62.  print <<ENDFOOTER;
63.  </table>
64.  </p>
65.  </card>
66.  </wml>

67.  ENDFOOTER

Note: As with earlier articles, teaching Perl is out of the scope of this article. For a tutorial on Perl, you can go to http://wdvl.internet.com/Authoring/Languages/Perl/PerlfortheWeb/toc.html.

Lines 1-3 define where to find the Perl interpreter and include the Date::Calc module in the compilation.

Line 5 sets the variables $day, $month, and $year to the current date. Note that this is the date on the server, not the date on the device.

Lines 6-9 set the variable $firstdow to the numeric equivalent of the weekday of the first day of the month, where Sun = 0 and Sat = 6. (Note: The Day_of_Week routine returns a value of 7 for Sunday, requiring line 9 to adjust it to 0 to suit our needs.)

Line 11 sets $monthtext to the full-text representation of the month (for example, "December").

Lines 12-24 print the beginning WML header and tags up through the weekday marker table row ("S M T W T F S"). Note that I decided to use single-letter markers for the weekdays-I believe it helps better align them with the columns.

Line 26 starts the first date row and lines 27-32 print blank cells ("<td>&nbsp; </td>") for the unused weekdays up to the first of the month. For example, if the first of the month is a Wednesday, this routine would print three blank cells—one each for Sunday, Monday, and Tuesday.

Line 34 sets the $day variable back to the first day.

Lines 35-51 work through the month, day by day, from the first day to the last day. Every seventh day (marked when $x = 7), the current row is closed (lines 41-42). If we aren't done with the month ($day < last day of month), a new row is started (lines 44-47).

Lines 52-60 close out the last row, printing blank cells for each day left in the week. For example, if the last day is a Thursday, this routine would print two blank cells (Friday and Saturday). The row is then closed.

Lines 61-67 close the open tags, ending the WML output.





Page 3 of 5



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Sitemap | Contact Us

Rocket Fuel