#!/usr/bin/perl -Tw

#++
# cookie-time.pl
#
# Based on time4.pl (pp. 47 - 49) from
#   _Official Guide to Programming with CGI.pm: The Standard for Building
#   Web Scripts_
#   by Lincoln Stein
#   (c) 1998 byLincoln Stein
#  Published by John Wiley & Sons, Inc.
#
# Source code time4.pl at
#  <URL:http://www.wiley.com/legacy/compbooks/stein/text/time4.txt>
#--

use CGI ':standard';
use POSIX 'strftime';

# constants
use constant LOCAL => 'Atlantic';
use constant GMT   => 'Standard';

# get initial values
  my $zone  = param('zone')  || cookie('zone')  || LOCAL; 
  my $style = param('style') || cookie('style') || '12';

# resend the cookie so it won't expire too soon
  my @cookie; 
  $cookie[0] = cookie(-name=>'zone',  -value=>$zone,  -expires=>'+3h');
  $cookie[1] = cookie(-name=>'style', -value=>$style, -expires=>'+3h');
  print header( -cookie=>[ $cookie[0], $cookie[1] ] ); # <<< must pass each cookie as a scalar


# print the HTTP header and the HTML document
  start_xhtml();
  $current_time = now($zone, $style);
  print "<p>The current time is <strong>", $current_time, " ", $zone, "</strong>.</p>\n";
  print_form($zone, $style, $current_time);
  finish_xhtml();


#-- subs begin --#

sub start_xhtml {
    # header already printed with cookie in main
    print '<?xml version="1.0" encoding="UTF-8"?>'."\n";
    print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">'."\n";
    print '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'."\n";
    print '<head>'."\n";
    print " <title>A Virtual Clock</title>\n";
    print "</head>\n";
    print "<body>\n";
    print " <h1>A Virtual Clock</h1>\n";
} # start_xhtml()

sub finish_xhtml {
    print " <hr />\n";
    print "  <p><a href='http://validator.w3.org/check/referer'>valid?</a></p>\n";
    print "  <p><a href='http://www.cs.dal.ca/~jamie/CS3172/examples/list.html'\n".
          "        >list of examples</a></p>\n";
    print "</body>\n";
    print "</html>\n";
} # finish_xhtml()

# get the time in the specified format
sub now {
    my ($zone, $style) = @_;
    my $format;

    $format = ($style eq '12') ?'%r' :'%T';
    if ($zone eq LOCAL) {
       return strftime($format,localtime);
   } else {
       return strftime($format,gmtime);
   }
} # now()

# print the clock settings form
sub print_form {
    my ($zone, $style) = @_;

    print ' <form', "\n";
    print '  action="/~jamie/cgi-bin/3172/cookie/cookie-time.cgi"', "\n";
    print '  method="post">', "\n";
    print '  <table border="1">', "\n";
    print '    <tr><th>Zone</th><th>Format</th></tr>', "\n";
    print '    <tr>', "\n";
    print '      <td>', "\n";
    print '        <input type="radio" name="zone" value="Atlantic" ',
          ($zone eq LOCAL) ?"checked='checked'"  :"",
          '/>', "\n";
    print '        Atlantic', "\n";
    print '     </td>', "\n";
    print '     <td>', "\n";
    print '        <input type="radio" name="style" value="12" ',
          ($style eq "12") ?"checked='checked'" :"",
          ' />', "\n";
    print '        12-hour', "\n";
    print '    </td>', "\n";
    print '    </tr>', "\n";
    print '    <tr>', "\n";
    print '      <td>', "\n";
    print '        <input type="radio" name="zone" value="Standard" ',
          ($zone eq GMT) ?"checked='checked'"  :"",
          '/>', "\n";
    print '        Greenwich', "\n";
    print '      </td>', "\n";
    print '      <td>', "\n";
    print '        <input type="radio" name="style" value="24" ',
          ($style eq "24") ?"checked='checked'" :"",
          ' />', "\n";
    print '        24-hour ', "\n";
    print '      </td>', "\n";
    print '    </tr>', "\n";
    print '   </table>', "\n";
    print '   <p><input type="submit" /></p>', "\n";
    print ' </form>', "\n";
} # print_form()

## End of Example ##
