The Time Functions

#!/usr/bin/perl -w

use Time::Local;   ######  For timelocal function ONLY!

$then = time();
printf("Time is: %ld\n\n", $then);

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($then);
print "Seconds = $sec\n";
print "Minutes = $min\n";
print "Hours   = $hour\n";
print "Day of month = $mday\n";
print "Month   = $mon\n";
print "Year    = $year\n";
print "Day of week  = $wday\n";
print "Day of year  = $yday\n";
print "Daylight time?  $isdst\n\n";

print "Localtime in scalar context:  ",scalar(localtime($then)),"\n\n";

$now = timelocal($sec, $min, $hour, $mday, $mon, $year);
printf("New time is: %ld\n\n", $now); 

############  Tricks to Convert Numeric Day/Month to a String  ###########

$day    = (Sun,Mon,Tue,Wed,Thu,Fri,Sat) [$wday];
$month  = (Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec)[$mon];
print "Today is: $day, $month $mday\n";

######################  Wall Clock Benchmarking Trick  #####################

$then = time();
for($i = 0; $i < 1000000; $i++){}  #####  Nonsense but good for a demo!
$now  = time();
print "One million for loops took ",$now - $then," seconds.\n";
##########################  Program Output Below  ##########################

Time is: 896163189

Seconds = 9
Minutes = 13
Hours   = 23
Day of month = 25
Month   = 4
Year    = 98
Day of week  = 1
Day of year  = 144
Daylight time?  1

Localtime in scalar context:  Mon May 25 23:13:09 1998

New time is: 896163189  #####  Timelocal is the inverse of localtime!!

Today is: Mon, May 25

One million for loops took 13 seconds.