Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Perl Script Problem

Posted on 2014-07-22
10
Medium Priority
?
350 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 27

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 27

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 27

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 27

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 27

Accepted Solution

by:
wilcoxon earned 2000 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

688 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