You want to convert a date, a time, or both with distinct values for day, month, year, etc. to Epoch seconds.
functions in the standard Time::Local module, depending on whether the date and time is in the current time zone or in UTC.
$TIME = timelocal($sec, $min, $hours, $mday, $mon, $year);
$TIME = timegm($sec, $min, $hours, $mday, $mon, $year);
The built-in function
converts an Epoch seconds value to distinct DMYHMS values; the
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
# $hours, $minutes, and $seconds represent a time today,
# in the current time zone
$time = timelocal($seconds, $minutes, $hours, (localtime)[3,4,5]);
If you're passing month and year values to
, it expects values with the same range as those which
returns. Namely, months start at 0, and years have 1900 subtracted from them.
function assumes the
DMYHMS values represent a time in the current time zone. Time::Local also exports a
subroutine that assumes the DMYHMS values represent a time in the GMT time zone. Unfortunately, there is no convenient way to convert from a time zone other than the current local time zone or GMT. The best you can do is convert to GMT and add or subtract the time zone offset in seconds.
This code illustrates both the use of
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 time
$time = timegm($seconds, $minutes, $hours, $day, $month-1, $year-1900);
As explained in the introduction, Epoch seconds cannot hold values before
. Don't convert such dates to Epoch seconds - use a Date:: module from CPAN, and do your calculations with that