WDef
  
 
  
 
 
Group: Members 
Posts: 798 
Joined: Sep. 2005 | 
  | 
Posted: Sep. 23 2007,16:11 | 
   | 
 
 
  
With 24-hr output and taking multiple city arguments:
  EDIT: added  -f option to access extended listing of city times, useage -h
  EDIT2&3:  now outputs 5-char time in justified columns for tidy display.  Set width with option -w 
  EDIT4:  Web source is www.whatisthetime.com.  -f switch has no effect for this.
 
 | Code Sample  |  #!/usr/bin/perl
  # Use -h for useage # 24-hour, multi arg version # -f option for long city listing # -w option to set output width
  use strict; use warnings; use Getopt::Std;
  my $defaultwidth = 14; # default output width in chars, overidden by -w switch
  #========================================
  sub twelveto24{ # adapted from http://keithdevens.com/weblog/archive/2006/Feb/02/12-to-24     my ($hr, $per) = @_;     return '00' if($hr == 12 and $per eq 'AM');     return $hr+12 if($hr != 12 and $per eq 'PM');     if (length($hr) eq 1){ $hr = '0' . $hr };     return $hr; }
  #=========================================
  my ($city, $URL, $displaywidth); our ($opt_f, $opt_h, $opt_w); my $download = "/tmp/citytimes.$$.html";
 
  # Parse  cli switches getopts('fhw:');
  if ($opt_h) { die "Useage: $0 [-f|-h] [-w WIDTH ] somecity1 somecity2 somecity3 ..\n" }; if (!$ARGV[0]) {die "No city specified"}; if ($opt_w){   if ($opt_w !~ /\d+/ ) { die "Invalid argument to -w option"  } else { $displaywidth = $opt_w } } else { $displaywidth = $defaultwidth }
  #~ if ($opt_f) { $URL = "http://www.timeanddate.com/worldclock/full.html"  #~ } else { $URL = "http://www.timeanddate.com/worldclock/" }
  if ($opt_f) { $URL = "http://www.whattimeisit.com/cgitime.exe?Mode=FullList"  } else { $URL = "http://www.whattimeisit.com/cgitime.exe?Mode=FullList" }
  # print "timeanddate.com:\n"; # legal print "www.whattimeisit.com:\n"; 
  system("wget -O $download $URL &>/dev/null");
  open(CONT, $download) or die "Could not open $download";
  while (<CONT>){  foreach $city(@ARGV) {  if ($city !~ /\w+/) { die "City '$city' not alphanumeric" }  if (/$city.+?(\d{1,2})\:(\d+\d+) ((AM|PM))/i) {   my ($hr, $min, $ampm) = ( $1, $2, $3);  my $newhr = twelveto24($hr, $ampm);  my $newtime = $newhr . ":" . $min;  my $F = 'A' . $displaywidth;  print pack("$F A5", $city, $newtime), "\n";  }  } }
  unlink $download;
  |    
 |