Solved

Perl Script Problem

Posted on 2014-07-22
10
341 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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

Technology Partners: 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

This article will show, step by step, how to integrate R code into a R Sweave document
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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 …

636 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