Solved

Perl Script Problem

Posted on 2014-07-22
10
300 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Join Greg Farro and Ethan Banks from Packet Pushers (http://packetpushers.net/podcast/podcasts/pq-show-93-smart-network-monitoring-paessler-sponsored/) and Greg Ross from Paessler (https://www.paessler.com/prtg) for a discussion about smart network …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 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

17 Experts available now in Live!

Get 1:1 Help Now