Notes: Perl Lab 11
Date and Time Functions
- time
- localtime
- modules
- timelocal
-
time
time
simply returns the current system time as number of
seconds that have elapsed since the time that the system regards as
the "epoch".For most Unix based systems this is 12:00 AM Jan 1, 1970
UTC.
$seconds = time;
-
localtime
By default, the localtime
function returns the current time. In
scalar context this will be the time formatted as dictated by the
system's locale settings. This syntax generally will take on the
form:
$now = localtime();
Which is the same as the following:
$now = localtime(time);
On our systems $now
will have a value that looks
something like this:
Sat Apr 24 13:01:53 2004
Example lab11_0.pl
|
$curr_time = time;
print "Current time in seconds is $curr_time\n";
print 'One: '. localtime(time) . "\n";
print 'Two: '. localtime() . "\n";
$time_string = localtime();
print "Current time is: $time_string\n";
$yesterday = $curr_time - 86400;
print "Yesterday was " . localtime($yesterday) ."\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@platypus PERL] ./lab11_0.pl
Current time in seconds is 1083001729
One: Mon Apr 26 17:48:49 2004
Two: Mon Apr 26 17:48:49 2004
Current time is: Mon Apr 26 17:48:49 2004
Yesterday was Sun Apr 25 17:48:49 2004
[mark@platypus PERL]
|
localtime
may be more commonly used in list context. In
this case, localtime
returns an array containing various
values of the time. Below is a table of the returned values in the
order that the function will return them and what their meaning:
Index |
Units |
Description |
0 | seconds | Values range 0-59 |
1 | minutes | Values range 0-59 |
2 | hours | Values range 0-23 |
3 | day of month | Values range 1-31 depending on
the month |
4 | month | Values range 0-11 where 0 is January
and 11 is December. |
5 | year | Returnrs the number of years since
1900. For example, 2004 would have a value of 104. |
6 | weekday | Returns the number of the day of the
week 0-6 for Sunday-Saturday |
7 | julian day | Returns 0-364 for non-leap years and
0-365 for leap years |
8 | daylight savings time | Returns true or "1" if
the time occurs during daylight savings time. Otherwise it
returns 0 or false. |
Example lab11_1.pl
|
$now = time;
print "Displaying raw current time\n";
&Display_time($now);
$new_time = time - (60*60*24*120);
print "\nDisplaying raw time 120 days ago\n";
&Display_time($new_time);
exit;
sub Display_time {
my $time_arg = $_[0];
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
localtime($time_arg);
print "Seconds are: $sec\n".
"Minutes are: $min\n".
"Hours are: $hour\n".
"Day of month: $mday\n".
"Month is: $mon\n".
"Year is: $year\n".
"Week day is: $wday\n".
"Julian day is: $yday\n".
"EST/EDT is: $isdst\n";
if($isdst) {
print "It is Daylight Savings Time\n";
}else{
print "It is not Daylight Savings Time\n";
}
}
|
When executed, the script above produces the following output:
|
[mark@platypus PERL] ./lab11_1.pl
Displaying raw current time
Seconds are: 48
Minutes are: 0
Hours are: 18
Day of month: 26
Month is: 3
Year is: 104
Week day is: 1
Julian day is: 116
EST/EDT is: 0
It is not Daylight Savings Time
Displaying raw time 180 days ago
Seconds are: 48
Minutes are: 0
Hours are: 18
Day of month: 28
Month is: 11
Year is: 103
Week day is: 0
Julian day is: 361
EST/EDT is: 0
It is not Daylight Savings Time
[mark@platypus PERL]
|
Example lab11_2.pl
|
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon++;
$year += 1900;
$wday++;
$yday++;
print "Seconds are: $sec\n".
"Minutes are: $min\n".
"Hours are: $hour\n".
"Day of month: $mday\n".
"Month is: $mon\n".
"Year is: $year\n".
"Week day is: $wday\n".
"Julian day is: $yday\n".
"EST/EDT is: $isdst\n";
if($isdst) {
print "It is Daylight Savings Time\n";
}else{
print "It is not Daylight Savings Time\n";
}
|
When executed, the script above produces the following output:
|
[mark@platypus PERL] ./lab11_2.pl
Seconds are: 42
Minutes are: 5
Hours are: 18
Day of month: 26
Month is: 4
Year is: 2004
Week day is: 2
Julian day is: 117
EST/EDT is: 0
It is not Daylight Savings Time
[mark@platypus PERL]
|
You may not want to alter all the values but rather, use them as
indexes for other arrays.
Example lab11_3.pl
|
@months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov);
@weekday = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
@now = localtime(time);
$current_month = $months[$now[4]];
$current_weekday = $weekday[$now[6]];
print "The month is $current_month\n";
print "The day of the week is $current_weekday\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@platypus PERL] ./lab11_3.pl
The month is Apr
The day of the week is Monday
[mark@platypus PERL]
|
-
modules
Before moving on to the functions availble in the
Time::Local
module,
the inclusion of modules should be discussed. The function
use
is
used to make the specified Perl module available to the program.
Generally, this will make new functions available to the program.
use Time;
This will make functions within the Time module available for use in
the current script. Specific versions or routes of the module may be
specified by appending the appropriate name to the module name
separated by a double colon "::"
use Time::Local;
-
timelocal
The Time::Local
module makes some additional
functions available for manipulating time values. In
particular, the timelocal
function will be examined.
timelocal
, like its name, is the inverse of the
localtime
function. timelocal
expects
a list of date values provided to it as arguments and returns an
integer time value for the number of seconds since the epoch.
$seconds = timelocal($sec,$min,$hour,$mday,$mon,$year);
This is particularly useful when there is the need to manipulate
date values other than the current date and time.
Those values can be converted to the number of seconds since the
epoch through the use of timelocal
. Once the
values are in seconds units the date data can then be
manipulated mathematically much more easily.
Example lab11_4.pl
|
use Time::Local;
print "Enter a year value: ";
chomp($user_year = <STDIN>);
print "Enter a month value (0-11): ";
chomp($user_month = <STDIN>);
print "Enter a day value (0-31): ";
chomp($user_day = <STDIN>);
$user_date = timelocal(0,0,0,$user_day,$user_month,$user_year);
$user_formatted = localtime($user_date);
print "The input date is $user_formatted\n";
$past_seconds = $user_date - (60*60*24*3);
$past_formatted = localtime($past_seconds);
print "The date 3 days prior to the input date is $past_formatted\n";
exit;
|
When executed, the script above produces the following output:
|
[mark@platypus PERL] ./lab11_4.pl
Enter a year value: 2004
Enter a month value (0-11): 2
Enter a day value (0-31): 15
The input date is Mon Mar 15 00:00:00 2004
The date 3 days prior to the input date is Fri Mar 12 00:00:00 2004
[mark@platypus PERL]
|
last updated: 18 Mar 2012 12:58