This article describes how to compute the current date in Perl and teaches you how to extract its individual values.
In is very easy to extract common values from a Date Object into an array such as the year, month, day, hour, minutes, and seconds. All this requires is declaring the array with the individual variables passed from the function call localtime(time).
Example:
 | Code Snippet 1 |
 |
|
#!/usr/bin/perl -w
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
use Time::Local;
print "Content-type: text/plain\n\n";
#declare array returned from localtime
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
#print the date using newly extracted values
printf "%4d-%02d-%02d %02d:%02d:%02d\n", $year+1900,$mon+1,$mday,$hour,$min,$sec;
#calculate system time again from the extracted values
my $time2 = timelocal($sec,$min,$hour,$mday,$mon,$year);
#calculate the difference (in seconds) of two system times
$difference = time - $time2;
exit 0;
|
|