Broad Network


Active Perl and Local Time

Forward: In this article, I show you how to read and display the server local time at the browser, using ActivePerl.

By: Chrysanthus Date Published: 11 Aug 2012

Introduction

Many web servers have Active Perl (or Perl) scripts. It is possible for a script in a server to read the date and time of the server computer. Everything being equal such date and time is called local time. In this article, I show you how to read and display the server local time at a browser. The browser here is not in the server; it is in some client computer somewhere in the Internet.

You need basic knowledge in HTML (or XHTML) and ActivePerl (Perl) in order to understand this article. If you do not have basic knowledge in HTML then read the series I wrote whose first article is titled, “Getting Started with XHTML”. To arrive at the series, type the title and my name Chrys in the Search Box of this page and click Search. If you do not have basic knowledge in ActivePerl (or Perl) then read another series I wrote whose first article is titled, “Getting Started with ActivePerl”. To arrive at the series, type the title and my name Chrys in the Search Box of this page and click Search.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

Everything said in this article is applicable to traditional Perl. However, with traditional Perl you have to precede your code with something like, #!/usr/bin/perl .

The Perl localtime Function
Perl has a predefined function called localtime. In simple terms the function returns a list as follows:

    ($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst) = localtime();

There are nine elements in the list. The values of the variables in the list are numbers. $sec is second as you would read from a digital clock (it may be one or two digits). $min is minutes as you would read from a digital clock (it may be one or two digits). $hour is hour (24-hour-clock) as you would read from a digital clock (it may be one or two digits).

$mday is the day number of the month and can be from 1 to 31. $mon is the month number and can be any number from 0 to 11, where 0 means January and 11 means December.

$year is the number of years since 1900. The year today is 2010. If you were to get the local time today, $year would be 110. The year returned in the list is the difference between today’s year and 1900. You have to add the value of $year to 1900 in order to obtain the four-digit year, today.

$wday is the day number of the week and can be any number from 0 to 6, where 0 is Sunday and 6 is Saturday. $yday is the day number of the year and it can be a number from 0 to 364 or 365 depending on whether you are dealing with a leap year or not. $isdst is true (1) if the specified time occurs during Daylight Saving Time, false (0) otherwise.

The return value of the function, localtime can be assigned to an array. The following code illustrates a simple use of the function. Save the code as a Perl file in a web server. Call the file from a browser.

use strict;
print "Content-Type: text/html\n\n";

my @time = localtime();

foreach (@time)
    {
        print $_ . "<br />";
    }

The nine values displayed should be numbers as mentioned above.

Example
Consider the following date-time example:

    Fri, 21 Nov 2010 9:55:13

You have the day of the week in letters, a comma, the day number of the month, the month in letters, the four-digit year, the hour, minutes and seconds. Date and time are usually displayed in a particular format as in this example.

To use the localtime function and finally display local time (date-time) in the above format, there are two main problems: the day of the week and month of the year are given as three-letter words; the second problem is that the year is given as the four-digit number. The other values for the format can be copied directly from the returned list of the localtime function.

Here is how we shall link the letter days of the week and the letter months of the year to their corresponding numbers from the returned list: For the days of the week there will be an array of seven elements; the values of this array are the three-letter words for the days of the week. Each of the indices of the array element is a number that can return the three-letter day of the week; an index of the array corresponds to its three-letter word value. For the months of the year there will be an array of 12 elements; the values of this array are the three-letter words for the months of the year. Each of the indices of the array element is a number that can return the corresponding three-letter month of the year.

The order of the elements of the example local time above is not the same order in the list returned by the localtime function. So the code to produce a local time string similar to the above example format has to do some reordering. It also has to select particular elements from the returned list of the function. These are implicitly done.

Note the use of a comma, spaces and colons of the above example format.

Implementation
The following code displays a string of the server local time according to the format given in the example above:

use strict;
print "Content-Type: text/html\n\n";

my @time = localtime();

my @week = ("Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat");

my @mYear = ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");

my $serverStr;

my $year = 1900 + $time[5];
$serverStr = $week[$time[6]] . ", " . $time[3] . " " . $mYear[$time[4]] . " " . $year . " " . $time[2] . ":" . $time[1] . ":" . $time[0];

print $serverStr;

Read the above code and try it; it should be self-explanatory.

We have come to the end of this article.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
Perl Course

Comments

Become the Writer's Fan
Send the Writer a Message