Print this Article (NS4)
Netscape Navigator
Internet Explorer
Opera
Neoplanet

Forums
HTML
General
Site Dev
Programming
Flash
Grafix (Art)

Laboratory
Smart HTML
Color Lab
Generators

Contents
Simple CGI
  1. Hello World
  2. Print function
  3. Quoting, EOF
  4. Metacharacters
  5. Special Characters
Perl Basics 1
  1. Variables
  2. Arrays
  3. Hashes
  4. Split function
  5. Subroutines
  6. Defaults
Form CGI
  1. Loops
  2. Conditions
  3. Boolean Statements
  4. Pattern Matching
Time CGI
  1. Local Time
  2. GM Time
  3. time function
Perl Basics 2
  1. Reading Files
  2. Writing Files
  3. Including Files
  4. chop function
  5. chomp function
Guestbook CGI

Redirect CGI

Poll CGI

  1. Giving Commands
  2. Voting
  3. Results Display
  4. Adding Your Vote
Password CGI
  1. Authentification
  2. Multiple Users
  3. Encryption
Mailing List CGI
  1. Sendmail
  2. Multiple Recipients
Unlimited Subdomains CGI

News Grabber CGI

  1. LWP::Simple
Message Board CGI (Part 1)

Back to the Top


Perl Case Study - Time CGI
By Lisa Hui

Time is pretty straightforward when it comes to Perl. You have two possible functions that will query (ask) the server for the local time. What's a little bit more difficult is parsing and making the data readable and suitable for display (or use). Below is a heavily commented script that will guide you through the process of writing one and

#!/usr/local/bin/perl
######################
# File: time.cgi
# Queries the server for its "local" time;
# Since the data is returned as a set of
# numbers, we can modify and associate them
# to a more familiar time display

# Start by printing out the content header
# We won't need any special type of HTTP header for this
print "Content-type: text/html\n\n";

# Request the local time from the server
# And set that equal to an array
# @array = localtime(time);
# we gave each array element an actual
# variable so that they are easier to identify
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);

# Let's see what the server returned to the array; you
# don't have to do this in the actual script, but it is
# nice to understand what you're working with in this
# example :)
print qq~

\$sec = $sec<br>
\$min = $min<br>
\$hour = $hour<br>
\$mday = $mday<br>
\$mon = $mon<br>
\$year = $year<br>
\$wday = $wday<br>
\$yday = $yday<br>
\$isdst = $isdst<br>

~;

# We can choose either to have the clock hours display
# 24hrs or 12 hrs with AM and PM to distinguish between
# day and night; it's basically preference

# ****************************************************

# If you want a 12 hr system, continue with the following
# code, else just skip to the end of the starred area

# Extension
$ampm = "a.m.";

# If you need a TIME OFFSET (display time for a different
# time zone than that of the server); IMPORTANT: the
# default time zone is NOT GMT (Greenwhich Mean Time)
# The following line would add 3 hours (set ahead)
# $hour += 3;
# The following line would subtract 3 hours (set back)
# $hour -= 3;

# If it is noon (at midnight $hour = 0) then change the
# extension to PM
if ($hour eq 12) { $ampm = "p.m."; }

# If it is midnight, then change that 0 to 12 AM
# The default extension is AM already
if ($hour eq 0) { $hour = "12"; }

# If it is after noon (12), then the extension is PM and
# we go back to 1PM, 2PM, 3PM so at 13:00, it is 1 PM
# (which means subtract 12 from 13 to get 1)
if ($hour > 12) {
   $ampm = "p.m.";
   $hour = ($hour - 12);
}

# ****************************************************

# Create an array that stores the names of the months
# based on the index number (which also happens to
# correspond with $mon! - but remember that $mon is 1
# less than the actual number of the month)

@months = ("January","February","March","April","May","June","July","August","September","October","November","December");
# Notice above that January is in array slot index of 0, but is month 1

# Now that we have all the variables we need, let's
# just create a format in which to print the date
print "Time Page Accessed\: $months[$mon] $mday, 19$year at $hour\:$min $ampm EST";

# You can instead have created a variable to store
# this date string like so:
# $fulldate = "$months[$mon] $mday, 19$year at $hour\:$min $ampm EST";

# What if you want to print out the day of the week?
# We would use $wday (days of the week) since it has
# a range of 0-6 (for the 7 days of the week)

# Note that I knew to write in EST as the time zone. You can
# omit this or change as you desire (just make sure that
# you substitute the correct time zone!) :)


See this script in action: time.cgi

In this example, we used the function localtime() to get the current time. There is another function, called gmtime(), which will get not the time at the locale in which the server is located but the time in GMT (Greenwich Mean Time) instead.

How do you get localtime or gmtime to return a list of items? You noticed that I wrote "localtime(time)" where time is a parameter of this function. time is also a function in itself, which returns the number of non-leap seconds since January 1, 1970.


Perl Basics: Reading, Writing and Taking a Bite

©1999 Team 26297 "Ad Infinitum Web." All rights reserved. Any reproduction of this document for commercial or redistribution purposes without the permission of the author is forbidden.