Perl: the time vector
The tutor continues about Perl’s handling of time.
In yesterday’s post I began about Perl’s time utilities.
Besides localtime, there is simply time. It returns the number of seconds since January 1, 1970 on the Windows system I use, as well as on most others (tutorialspoint.com).
These few lines of code
#!/usr/bin/perl
$tim0=time;
print “The time is $tim0\n”;
will hopefully give output like
The time is 1421093858
There is even more to Perl’s handling of time than we’ve seen so far. Perl offers a veritable vector of time information to those desiring it. Consider the following little program:
#!/usr/bin/perl
@timarray=localtime;
$length=@timarray; #gives length of the array
for($i=0; $i<$length; $i++){
print “Element $i of Perl’s time array is $timarray[$i]\n”;
}
Running the lines above, you may be greeted with output like this:
Element 0 of Perl’s time array is 2 //seconds
Element 1 of Perl’s time array is 44 //minutes
Element 2 of Perl’s time array is 12 //hours
Element 3 of Perl’s time array is 12 //the day this month
Element 4 of Perl’s time array is 0 //the month: Jan=0
Element 5 of Perl’s time array is 115 //the year: 1900=0
Element 6 of Perl’s time array is 1 //day of the week: Sun=0
Element 7 of Perl’s time array is 11 //the day this year:Jan 1=0
Element 8 of Perl’s time array is 0 //daylight savings: no=0, yes=1
For now, this wraps up my coverage of Perl time functions. HIH:)
Source: McGrath, Mike. Perl in easy steps. Southam: Computer Step, 2004.
Jack of Oracle Tutoring by Jack and Diane, Campbell River, BC.
Leave a Reply
You must be logged in to post a comment.