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
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
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab11 Example 00 # Description: An example of the time and localtime functions #========================================================================== # show that the time function returns an integer $curr_time = time; print "Current time in seconds is $curr_time\n"; # print the output in scalar context. Notice that the contantenation within # the print function tells the perl interpreter that localtime output is in # a scalar context. print 'One: '. localtime(time) . "\n"; print 'Two: '. localtime() . "\n"; # another example of localtime in scalar context $time_string = localtime(); print "Current time is: $time_string\n"; # we can get the time information from another date by manipulating the number # of seconds as the argument to localtime. Here we subtract one day (in # seconds) from the current time and use that value with the localtime func. $yesterday = $curr_time - 86400; print "Yesterday was " . localtime($yesterday) ."\n"; exit; # DONE |
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. |
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab11 Example 01 # Description: An example of the localtime function in list context #========================================================================== # Get the current time in seconds and call the Display_time function to # display the values. $now = time; print "Displaying raw current time\n"; &Display_time($now); # subtract 120 days from the current time and get the localtime values and # then call the Display_time function to display the values. $new_time = time - (60*60*24*120); print "\nDisplaying raw time 120 days ago\n"; &Display_time($new_time); exit; # DONE sub Display_time { # rename the function's arguments my $time_arg = $_[0]; # Get the current time ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time_arg); # print out the raw values: 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
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab11 Example 02 # Description: An example of correcting the return values from the # localtime function #========================================================================== # get the time values ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time); # correct the date values to match how humans generally count things. $mon++; $year += 1900; $wday++; $yday++; # Show the new values 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"; # Is it daylight savings time? 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
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab11 Example 03 # Description: An example of using localtime values #========================================================================== # setup some arrays of text values @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov); @weekday = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday); # get the current time values @now = localtime(time); # set the string values based on the array values $current_month = $months[$now[4]]; $current_weekday = $weekday[$now[6]]; # print the values to the screen print "The month is $current_month\n"; print "The day of the week is $current_weekday\n"; exit; # DONE |
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] |
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;
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
|
|||
#!/usr/bin/perl -w # Name: Mark Tucker # Assignment: Lab11 Example 04 # Description: An example of the timelocal. Given the date from user input # calculate the date three days prior and display on the screen. #========================================================================== # include the Time::Local module use Time::Local; # get the date information from the user 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>); # convert the current date to seconds and print to the screen $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"; # subtract 3 days from the input date and print to the screen $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; # DONE |
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