Chopped Time
December 13, 1999
The downside to using localtime (or gmtime) in
the scalar context is that it simply spits out
a pre-formatted result. You may wish to return the individual
values for month, day, year and so on, which you
might, for example, format in an alternative manner of your
own construction. When you call localtime from
within a "list context" -- assigning its return
values to an array -- you receive the time chopped and
diced into bite sized pieces.
Calling localtime in a list context yields the
following values in this exact order:
List items returned by the localtime
(or gmtime) function.
| List index |
Description |
Values |
Notes |
| 0 |
seconds |
0-59 |
|
| 1 |
minutes |
0-59 |
|
| 2 |
hour |
0-23 |
|
| 3 |
day of the month |
1-31 |
|
| 4 |
month |
0-11 |
0=January, 11=December |
| 5 |
year |
minus 1900 |
The year value is not
the last two digits of the year, it is the
current year minus 1900; e.g., 105 would
mean the year 2005 (1900+105). Perl
is therefore Y2K compliant! |
| 6 |
day of the week |
0-6 |
0=Sunday, 6=Saturday |
| 7 |
day of the year |
0-364 |
|
| 8 |
daylight savings time flag |
0 or 1 |
1 (True) if date falls during
Daylight Savings Time, 0 (False) otherwise.
|
One way, then, to grab all the chopped bits of time is to
assign localtime to a list of scalar variables:
($seconds,$minutes,$hour,
$monthday,$month,$year,
$weekday,$yearday,$dst_flag)=localtime(time);
The trouble with this approach is twofold: for one, Perl
returns values such as the day of the week and the
month as numeric values, not names, which is not exactly
suitable for human display. Second, you may not be interested
in all of these bits, so you may not want to create variables
that you're never going to use in the script (such
as $dst_flag).
A clever way to pick out, for example, the month and translate
it immediately into its respective name, relies
on nested list references. Watch closely:
$day=(Sunday,Monday,Tuesday,
Wednesday,Thursday,Friday,Saturday)[(localtime(time))[
The above snippet revolves around an "anonymous
array", a list of weekday names which is not assigned
to a particular list variable. We specify a single element of
that array, that element being "(localtime(time))[6]]".
In turn, "(localtime(time))" itself returns the list
of time variables seen in the earlier table, from
which we specify the element at index 6, which is the day of
the week. We can use the same trick to pluck the name
of the month.
$month=(January,February,March,
April,May,June,July,
August,September,October,
November,December)[(localtime(time))[4]];
Although both examples above rely on the current time, you
can of course replace time with any epoch
time you wish.
An alternative to using Perl's default localtime
function (or gmtime) is to use the module
Time::localtime,
which is included in the standard Perl distribution.
In fact, this module behaves in nearly the exact same way
as localtime -- meaning that it converts epoch
time into localized time -- but allows for a slightly easier
way to pick out the bits and pieces.
use Time::localtime;
$tomorrow=time+86400;
$timeVar=localtime($tomorrow);
$day=(Sunday,Monday,Tuesday,
Wednesday,Thursday,Friday,Saturday)[$timeVar->wday];
Notice in the above script that we assign localtime to
a scalar variable -- you can then call methods
from this variable which return relevant time information,
such as the day of the week ($timeVar->wday). The
data is still returned as a numeric value, so we continue to
rely on the anonymous array trick to translate this
value into the day name. The convenience is largely in the
fact that you can call upon a single item of time data
by name rather than needing to refer to list indices. Using
the localtime object in this manner you can call the
following methods. Each of these values has the same range
and meaning as those returned by the localtime
function as seen in the previous table.
Methods of the localtime object.
| sec |
Seconds |
| min |
Minutes |
| hour |
Hour |
| mday |
Day of the month |
| mon |
Month |
| year |
Year |
| wday |
Day of the week |
| yday |
Day of the year |
| isdst |
Daylight Savings Time Flag |
Remember that the Time::localtime module adjusts time for your
particular time zone. You can alternately use
the Time::gmtime module as a replacement for the gmtime
function if you wish to stick with GMT time.
Human Friendly Time
The Perl You Need to Know
Reversing Time
|