dcsimg
June 21, 2018
Hot Topics:

Building WML Gadgets: World Time Clock

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

Searching for Time Zones

Next, we need the ability to search for a particular time zone. Adding a variable, "findzone", and a simple substring search accomplishes this goal:

Listing: tztest.pl

#!/usr/bin/perl

use Time::ZoneInfo ':all';
use Date::Calc ':all';

my $findzone = (shift @ARGV);

my $zones = Time::ZoneInfo->new();

foreach my $zone ($zones->zones) {
  if ((index uc($zone), uc($findzone)) != -1) {

  $ENV{TZ} = $zone;
  ($year,$month,$day,$hour,$min,$sec, $doy,$dow,$dst) =
    System_Clock();
  print $zone.":";

  print $year."-".$month."-".$day." ";
  print $hour.":".$min.":".$sec."\n";
  }
}

Note the use of the IF statement. We uppercase both the zone and the findzone variables to help ensure a match (if "asia" is entered, it will still match all "Asia" entries). For example, running the script with this entry:

./tztest.pl pacific

displays the following:

Pacific/Easter: 2002-12-29  23:40:20
Pacific/Galapagos: 2002-12-29  22:40:20
Pacific/Yap: 2002-12-30  14:40:20
Pacific/Truk: 2002-12-30  14:40:20
Pacific/Ponape: 2002-12-30  15:40:20
Pacific/Kosrae: 2002-12-30  15:40:20
Pacific/Tarawa: 2002-12-30  16:40:20
Pacific/Enderbury: 2002-12-30  17:40:20
Pacific/Kiritimati: 2002-12-30  18:40:20
Pacific/Majuro: 2002-12-30  16:40:20
Pacific/Kwajalein: 2002-12-30  16:40:20
Pacific/Auckland: 2002-12-30  17:40:20
Pacific/Chatham: 2002-12-30  18:25:20
Pacific/Tahiti: 2002-12-29  18:40:20
Pacific/Marquesas: 2002-12-29  19:10:20
Pacific/Gambier: 2002-12-29  19:40:20
Pacific/Johnston: 2002-12-29  18:40:20
Pacific/Midway: 2002-12-29  17:40:20
Pacific/Wake: 2002-12-30  16:40:20
Pacific/Honolulu: 2002-12-29  18:40:20

Suppose that the user doesn't know what time zone he/she is looking for. For example, Pacific/Honolulu covers Hawaii, but isn't necessarily intuitive to someone looking for "Hawaii." To help the user, we will add "ALL" as a valid search that returns all zones. To do so, we doctor the IF statement accordingly:

if (((index uc($zone), uc($findzone)) != -1) ||
    ("uc($findzone)" eq "ALL" )) {

Now, if the user enters "ALL" for the search, he/she will get all known zones.





Page 2 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.

By submitting your information, you agree that developer.com may send you developer offers via email, phone and text message, as well as email offers about other products and services that developer believes may be of interest to you. developer will process your information in accordance with the Quinstreet Privacy Policy.

Sitemap

×
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date