3.2. Converting DMYHMS to Epoch SecondsProblemYou want to convert a date, a time, or both with distinct values for day, month, year, etc. to Epoch seconds. Solution
Use the use Time::Local; $TIME = timelocal($sec, $min, $hours, $mday, $mon, $year); $TIME = timegm($sec, $min, $hours, $mday, $mon, $year); Discussion
The built-in function # $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
The
This code illustrates both the use of # $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 use Time::Local; $time = timegm($seconds, $minutes, $hours, $day, $month-1, $year-1900);
As explained in the introduction, Epoch seconds cannot hold values before See AlsoThe documentation for the standard Time::Local module (also in Chapter 7 of Programming Perl ); convert in the other direction using Recipe 3.3 Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|