Program to demonstrate the "localtime" function


The program below will demonstrate how "localtime" can be used. Copy the program below (cut and paste into a text editor). Save the program to the hard disk and make it executible. Execute the program and it will print out the time, day and year.

#! /usr/bin/perl -w
#
# Written by Sam Watson Jan 12, 2008
#
# Set up the following variables:
#       $stars  Used in the header and footer just to make things look pretty.
#       $ap     Determines AM (default) or PM.
#       $st     Set time zone to CST (default) or CDT if in daylight savings time.
#       @month  Months of the year.
#       @day    Days of the week.
$stars = "\n*****\n";
$ap = "AM";
$st = "CST";
@month = qw(Jan. Feb. Mar. Apr. May Jun. Jul. Aug. Sep. Oct. Nov. Dec.);
@day = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);

# Get the localtime
($sec, $min, $hour, $mday,$mon, $year_off, $wday, $yday, $isdst) = localtime;

# Set the actual year
$year = $year_off + 1900;

# Set up correct $ap and $st
if ($hour > 12) {
        $hour = $hour - 12;
        $ap = "PM";}
if ($isdst) {
        $st = "CDT";}

# Output to the screen
print "$stars\n";
print "The time is\t$hour:$min:$sec $ap\n";
print "Today is\t@day[$wday]\t@month[$mon], $mday $year $st\n";
print "Today is day $yday of $year";
print "\n$stars";


Sample output:


*****

The time is     2:33:20 PM
Today is        Friday  Jan., 11 2008 CST
Today is day 10 of 2008

*****