February 23, 2019
Hot Topics:

Building WML Gadgets: World Time Clock

  • January 10, 2003
  • By Steve Schafer
  • Send Email »
  • More Articles »

Creating WML Cards

Using methods described in previous articles, it's relatively easy to output compliant WML for mobile devices. We will need another Perl module, CGI, for a couple of purposes:

  • Parsing arguments passed to the script
  • Supplying the mobile browser with an appropriate WML header

Note: After my calendar gadget article was posted, a reader wrote to chastise me for writing my own argument-parsing routine. As I stated in the article, the fact that the script was passing itself cleanly formatted parameters was justification for simple argument parsing. However, one point I missed (brought up by the reader) was that the code might go on to be incorporated in other scripts where the sterility of the parameters could not be guaranteed. To help promote solid coding, I've pledged to use the CGI method "param" from now on.

To output a WML card, we use the following code:

# Pass WML header
print header(-type=>'text/vnd.wap.wml');
# Print WML header and beginning tags
print <<ENDHEADER;
<?xml version="1.0"?>

This sets up the beginnings of a generic card. Subsequent PRINT statements can supply <do> and/or <p> tags accordingly. Let's close up the card:

# Print closing tags
print <<ENDFOOTER;

Now all that's left is more Perl logic to control the flow and the resulting output.

The Final Script

Here's a listing of our final script:

Listing: timezone.pl

use Time::ZoneInfo ':all';
use Date::Calc ':all';
use CGI qw(:standard);
my $findzone = param('findzone');
# Pass WML header
print header(-type=>'text/vnd.wap.wml');
# Print WML header and beginning tags
print <<ENDHEADER;
<?xml version="1.0"?>
# Do we have a time zone to look up?
if ( "$findzone" eq "" ) {
# No time zone specified; display input card
<do type="accept">
  <go href="timezone.pl?findzone=\$findzone"/>
Enter zone text to search<br/>
for or ALL for all zones:
<input name="findzone" maxlength="40" format="*A"/>
} else {
$findzone =~ tr/ /_/;
$findzone =~ tr/+/_/;
# Set up zones
my $zones = Time::ZoneInfo->new();
my $matches = 0;
print "<p>\n";
# Run through zones, looking for match
foreach my $zone ($zones->zones) {
  if (((index uc($zone), uc($findzone)) != -1) ||
      ( "uc($findzone)" eq "ALL" )) {
    # Display each match, or all if "ALL" was entered
    print $zone." :<br/>";
    $ENV{TZ} = $zone;
    ($year,$month,$day, $hour,$min,$sec,
      $doy,$dow,$dst) = System_Clock();
    print $year."-".$month."-".$day." ";
    print $hour.":".$min.":".$sec."<br/>\n";
# If no time zone matches, tell user
if ( $matches eq 0 ) {
    print "No time zone match for:<br/> \n";
    print $findzone."<br/><br/>";
# Print closing tags
print <<ENDFOOTER;
# End of script

The most notable addition is the IF statement to control which card is displayed. When the script is first executed (without a parameter), the input card is displayed to allow the user to input search text. The script is then called again with the name/value pair "findzone" and the zones are searched for a match.

We've also added logic to tell whether any results were returned. If no results were displayed, the code tells the user ("No time zone match for. . . ").

Note: Pay particular attention to the escaped dollar sign ($) in the <go> tag. Without the escape backslash (\), Perl would interpret "$findzone" as one of its local variables. Since the Perl variable "$findzone" is empty when the script is first called, the resulting <go> tag would incorrectly be sent to the browser as follows:

<go href="timezone.pl?findzone="/>

Room for Improvement

As with previous projects, several things could be added to improve our world time clock:

  • A better format for the time output. Right now values under 10 are output as a single digit. For example, nine o'clock AM is displayed as: "9:0."  Using a format mask or some simple logic we could pad the time accordingly (e.g., "09:00").
  • Break long listings into multiple pages/cards. For example, the "ALL" timezone listing ends up weighing in at just under 5K. That's five times the suggested 1K card data limit. Using some simple logic, the Perl script could display the data in a sequence of cards, each containing 7-12 records.
  • Add a control on the results card(s) to return to the search card.
  • Find/create a more comprehensive time zone database that includes named zones such as EST, CST, etc. (A comprehensive database can be compiled by downloading the source files found at ftp://elsie.nci.nih.gov/pub.)

What Do You Want from WML?

I'm interested in hearing what you need/want to do with WML. I'll use some of the more challenging or common ideas in upcoming articles. Send your ideas to the address below.

About the Author

Steve Schafer is the chief operating officer of Progeny Linux Systems, a Linux-based consulting company in Indianapolis, Indiana. He has written several technical books and articles and can be reached at sschafer@synergy-tech.com.

About this Series

This series of articles describes how to provide Web content to mobile devices through WML (Wireless Markup Language). This article covers creating small programs that extend the functionality of mobile devices, building on the previous two articles and examples.

Note: These articles cover WML and WMLScript version 1.1, which are supported by the majority of mobile devices in use today. The articles assume a working knowledge of HTML and general Web technologies, and further assume that you have read the previous article(s) in this series.

# # #

Page 3 of 3

Comment and Contribute


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



Enterprise Development Update

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

Thanks for your registration, follow us on our social networks to keep up-to-date