Solved

Perl Script Problem

Posted on 2014-07-22
10
322 Views
Last Modified: 2014-07-23
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. :)
0
Comment
Question by:SuperRoot
  • 5
  • 3
  • 2
10 Comments
 
LVL 26

Expert Comment

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

Open in new window

0
 
LVL 1

Author Comment

by:SuperRoot
ID: 40213415
hmmm... It seems like I need Date::calc module?

Undefined subroutine &main::Mktime called at ./script.pl line 89.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 40213434
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".
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Expert Comment

by:wilcoxon
ID: 40213437
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

0
 
LVL 8

Expert Comment

by:Surrano
ID: 40213635
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

0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 40214767
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.
0
 
LVL 8

Expert Comment

by:Surrano
ID: 40214797
Ooops. Seems our systems need a major revamp :)
0
 
LVL 1

Author Comment

by:SuperRoot
ID: 40214952
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?
0
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 40215135
Not really.  Assuming the date is always in the format you provide in your question, you can just do:
use strict;
use warnings;
use Date::Calc qw(Mktime);
my $date = "2014-07-22 05:34:06"; # get this via command line or file or however you want
my $seconds = Mktime(split /[- :]/, $date); # that's a space between - and :

Open in new window

0
 
LVL 1

Author Closing Comment

by:SuperRoot
ID: 40215214
that worked! Thank you!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question