Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert Human Readable time to Epoch and then Back

Status
Not open for further replies.

avashisht

Technical User
Feb 16, 2004
9
US
Hi people,
I am trying to parse 2 parameters to a subroutine in a Perl/cgi app; to convert the normal time to epoch time, add no of seconds supplied and then convert that back to normal date format.
for some reason, by shell the subroutine works but not when put in the Perl/Cgi app where it is required.
Using the module::
Time::Local;

here is the code:

----------------------------------------------------------
sub add_time {
my ($timestamp, $time_to_add) = @_;
my ($date, $time, $mon, $dd, $yy, $hh, $mm, $ss);
my ($epoch, $added_time);

&wbsdTest("In add_time: TimeStamp: $timestamp, Time To Add: $time_to_add");
($date, $time) = (split(' ', $timestamp));
($mon, $dd, $yy) = (split('/', $date));
($hh, $mm) = (split(':', $time));
&wbsdTest("In add_time: before Time Local:");
$epoch = timelocal(00, $mm, $hh, $dd, $mon - 1, $yy - 1900);
&wbsdTest("In add_time: After timelocal: $epoch");

$epoch += ($time_to_add * 60);

($ss, $mm, $hh, $dd, $mon, $yy) = (localtime($epoch))[0 .. 5];
$added_time = sprintf("%02d/%02d/%04d %02d:%02d",
$mon + 1, $dd, $yy + 1900, $hh, $mm);
&wbsdTest("In add_time: Returning $add_time");
return($added_time);
}
----------------------------------------------------------

It is at the following section where the apps stops working
$epoch = timelocal(00, $mm, $hh, $dd, $mon - 1, $yy - 1900);
....

any Comments?
any one knows a much simpler way to get this done?

will appreciate any comments !! :)


 
Are you sure that your passing in $timestamp properly at the beginning of the code? Everything seems to hinge on that variable being proper and my guess would be that however you generate that information in the wrapping code is working at the shell and not working from cgi. Maybe your using some backtick operator to get it that fails under cgi?



 
Yes that variable is passes properly and i have a debug log that gets generated whenevr the apps reaches this subroutine. as i am still debugging i have a debug log for practically every line..
 
So in this call

Code:
 $epoch = timelocal(00, $mm, $hh, $dd, $mon - 1, $yy - 1900);
are $hh,$mm, $dd, $mon, $yy properly populating?

Sorry, I know it seems basic, I don't have your debug output here so I'm asking :)
 
aaaah,
got it solved,....
($mon, $dd, $yy) = (split('/', $date));
should be like this:
($yy, $mon, $dd) = (split('/', $date));

That's it...

date format was: yyyy(-/)mm(-/)dd and not the other way round
 
Congrats, it was in the $timestamp variable, good catch!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top