Link to home
Start Free TrialLog in
Avatar of SuperRoot
SuperRoot

asked on

Perl Script Problem

Hello, I'm wondering if someone can help. How can you convert "2014-07-22 05:34:06" to seconds using perl? I've been having trouble doing the math. Already did tons of array but there could be easier way to do it. :)
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

use Date::Calc;
my $seconds = Mktime(2014,7,22,5,34,6);

Open in new window

Avatar of SuperRoot
SuperRoot

ASKER

hmmm... It seems like I need Date::calc module?

Undefined subroutine &main::Mktime called at ./script.pl line 89.
Yes.  If Date::Calc isn't installed on your system, you'll need to install it.  "cpanm Date::Calc" or "cpan -i Date::Calc" will do it (assuming you have permissions to install to the primary perllib dir).  If not, you can install it into your own directory by:
download Date::Calc
perl Makefile.PL PREFIX=<dir>
make test
make install

If you do that, then you'll need to add "use lib '<dir>';" above the "use Date::Calc".
Hmm.  Although maybe not.  It looks like Date::Calc doesn't export any functions by default.  Try this:
use strict;
use warnings;
use Date::Calc qw(Mktime);
my $seconds = Mktime(2014,7,22,5,34,6);

Open in new window

Or using Time::Local which I believe is part of any std distrib:

require "timelocal.pl";
print timelocal(6,34,5,22,7,2014),"\n";

Open in new window

timelocal.pl was removed in Perl 5.20 (per the message in Perl 5.18 at least).,,  Or maybe it already was removed...

Legacy library timelocal.pl will be removed from the Perl core distribution in the next major release. Please install it from the CPAN distribution Perl4::CoreLibs.
Ooops. Seems our systems need a major revamp :)
I might just end up doing a lot of array for each character and multiply then add them up to create the seconds. This is a lot of logic which I hate :( The numbers in "print timelocal(6,34,5,22,7,2014),"\n";" changes everyday so I think its easier just to do the math?
ASKER CERTIFIED SOLUTION
Avatar of wilcoxon
wilcoxon
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that worked! Thank you!