|
Perl Case Study - Time CGI 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
# Start by printing out the content header
# Request the local time from the server
# Let's see what the server returned to the array; you
\$sec = $sec<br> ~;
# We can choose either to have the clock hours display # ****************************************************
# If you want a 12 hr system, continue with the following
# Extension
# If you need a TIME OFFSET (display time for a different
# If it is noon (at midnight $hour = 0) then change the
# If it is midnight, then change that 0 to 12 AM
# If it is after noon (12), then the extension is PM and # ****************************************************
# Create an array that stores the names of the months
@months =
("January","February","March","April","May","June","July","August","September","October","November","December");
# Now that we have all the variables we need, let's
# You can instead have created a variable to store
# What if you want to print out the day of the week?
# Note that I knew to write in EST as the time zone. You can
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.
|
||||||||||
©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.