3.2.3. Discussion
The built-in function localtime converts an Epoch
seconds value to distinct DMYHMS values; the
timelocal subroutine from the standard Time::Local
module converts distinct DMYHMS values to an Epoch seconds value.
Here's an example that shows how to find Epoch seconds for a time in
the current day. It gets the day, month, and year values from
localtime:
# $hours, $minutes, and $seconds represent a time today,
# in the current time zone
use Time::Local;
$time = timelocal($seconds, $minutes, $hours, (localtime)[3,4,5]);
If you're passing month and year values to
timelocal, it expects values with the same range
as those which localtime returns. Namely, months
start at 0, and years have 1900 subtracted from them.
This code illustrates both the use of timegm and
how to adjust the ranges of months and years:
# $day is day in month (1-31)
# $month is month in year (1-12)
# $year is four-digit year e.g., 1967
# $hours, $minutes and $seconds represent UTC (GMT) time
use Time::Local;
$time = timegm($seconds, $minutes, $hours, $day, $month-1, $year-1900);
As explained in the introduction, Epoch seconds cannot hold values
before Fri Dec
13 20:45:52
1901 or after Tue
Jan 19
03:14:07 2038. Don't convert
such dates to Epoch seconds—use a Date:: module from CPAN, and
do your calculations with that instead.