Solved

Perl Script Problem

Posted on 2014-07-22
10
311 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Fine Tune your automatic Updates for Ubuntu / Debian
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

813 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now