I wanted to build an awesome place for people to discuss module specific issues, but I don't have any more time for this, and there are much better places to discuss Perl-related issues. I'd recommend asking your question on Stack Overflow or on Perl Monks.
If you are looking for a Perl tutorial or Perl-related news, I hope these links will serve you well.
Posted on 2009-10-30 13:33:55-07 by bruhnf
Help converting time array to epoch time
I am trying to convert a time array to epoch time. If I execute the following code with the time hard coded in the fields, I get the correct epoch time returned. Obviously, I had to subtract 1 for the month and 109 for the year (ie: 2009)
$epoch_time_var = timelocal(59,04,10,30,9,109); print "$epoch_time_var \n";
This prints 1256911934 (the correct epoch time that I hard coded in) but... If I try to use the elements of a time array to do the same thing, I get the error cannot handle date at line xxx.
my @time_array = localtime(time); $epoch_time_var = timelocal($time_array[0],$time_array[1],$time_array[2],$time_array[3],$time_array +[4]-1,$time_array[5]-1900); print "$epoch_time_var \n";
Any idea why this doesn't work and how to make it work? Thanks in advance for any help on this. Bruhn
Direct Responses: 11671 | Write a response
Posted on 2009-10-30 14:32:40-07 by wyant in response to 11669
Re: Help converting time array to epoch time
If you would print the values you are sending to timelocal(), you would find that $time_array[5] was 109 (for another couple months), so $time_array[5]-1900 is -1791, which Time::Local can not deal with. Instead, try

$epoch_time_var = timelocal($time_array[0],$time_array[1],$time_array[2],$time_array[3],$time_array[4],$time_array[5]);

It turns out that

$epoch_time_var = timelocal(@time_array)

works just as well, though the truly paranoid would say

$epoch_time_var = timelocal(@time_array[0..5]);

You could also try

use Time::y2038;

instead of Time::Local if you have Time::y2038 installed, and if you are worried about dates outside the range 1970 to 2038.
Direct Responses: 11672 | Write a response
Posted on 2009-10-30 14:50:02-07 by bruhnf in response to 11671
Re: Help converting time array to epoch time
Hey wyant, Thanks so much for the help. I've been messing with this thing since I made the post. I used $epoch_time_var = timelocal(@time_array[0..5]) Guess I'll join the ranks of the truly paranoid. :-) Never thought to try that and I like it the best. Nice and compact. Maybe in a later iteration, I'll go with Time::2038. Haven't worked with that one yet. Thanks again for the help. Bruhn
Direct Responses: Write a response