6

I am attempting to write a Perl script that parses a log where on each line the second value is the date. The script takes in three arguments: the input log file, the start time, and the end time. The start and end time are used to parse out a certain value on each line that that falls between those two times. But to properly run this I am converting the start and end time to epoch time. The problem I am having is that to convert the loops 'i' value back to normal time to compare against the log file. After running localtime($i) I print the value and only see a reference printed not the actual value.

Here is the script I have so far (it is a work in progress):

#!/usr/bin/perl
use strict;
use warnings;
use Time::Local;
use Time::localtime;
use File::stat;

my $sec = 0;
my $min = 0;
my $hour = 0;
my $mday = 0;
my $mon = 0;
my $year = 0;
my $wday = 0;
my $yday = 0;
my $isdst = 0;

##########################
# Get the engine log date
##########################
my $date = `grep -m 1 'Metric' "$ARGV[0]" | awk '{print \$2}'`;
($year,$mon,$mday) = split('-', $date);
$mon--;

#########################################
# Calculate the start and end epoch time
#########################################
($hour,$min,$sec) = split(':', $ARGV[1]);
my $startTime = timelocal($sec,$min,$hour,$mday,$mon,$year);
($hour,$min,$sec) = split(':', $ARGV[2]);
my $endTime = timelocal($sec,$min,$hour,$mday,$mon,$year);


my $theTime = 0;
for (my $i = $startTime; $i <= $endTime + 29; $i++) {
        #print "$startTime   $i \n";

        $theTime = localtime($i);

        #my $DBInstance0 = `grep "$hour:$min:$sec" "$ARGV[0]"`;# | grep 'DBInstance-0' | awk '{print \$9}'`;
        #print "$DBInstance0\n";
        print "$theTime\n";
}
print "$startTime   $endTime \n";

The output looks like:

Time::tm=ARRAY(0x8cbbd40)
Time::tm=ARRAY(0x8cbc1a0)
Time::tm=ARRAY(0x8cbbe80)
Time::tm=ARRAY(0x8cbc190)
Time::tm=ARRAY(0x8bbb170)
Time::tm=ARRAY(0x8cbc180)
Time::tm=ARRAY(0x8cbbf30)
Time::tm=ARRAY(0x8cbc170)
Time::tm=ARRAY(0x8cbc210)
Time::tm=ARRAY(0x8cbc160)
1275760356   1275760773

I only have access to the core Perl modules and am unable to install any others.

Andy Lester
  • 91,102
  • 13
  • 100
  • 152
Matt Pascoe
  • 8,651
  • 17
  • 42
  • 48
  • Is this homework? And "unable to install any others" simply means that you haven't read [But I can't use CPAN!](http://www.shadowcat.co.uk/blog/matt-s-trout/but-i-cant-use-cpan/) – Ether Jun 25 '10 at 20:16
  • This is the start of a larger script for work, and my work will only allow me to use the core perl modules. We just started using Perl and the core modules are approved and they are testing others, but there is a three month waiting time from requesting to use a module and being allowed to use it. – Matt Pascoe Jun 25 '10 at 20:22
  • 2
    @Matt: in that case I recommend submitting your resume to http://careers.stackoverflow.com. – Ether Jun 25 '10 at 20:23
  • 1
    @Matt: Why would a company choose to use the "Pathological Eclectic Rubbish Lister" and deny access to the drawer with the sharpest tools in the universe : the CPAN? My advice : use the CPAN, get the job done, ask forgiveness later. You can always implement (read: copy) the functions in your own code later. That's why the artistic licence was invented. – Peter Tillemans Jun 25 '10 at 20:33
  • 2
    @Ether,@Peter - some people work for companies larger than a start-up, where the win of using untested code is WAY larger than a value at risk from using it. When you lose billions from buggy code, your perspective changes to be a bit more red-tapey. So being rude to someone on that basis is not only not helping you win Perl converts, it is also downright unfair. – DVK Jun 25 '10 at 20:59
  • 2
    @DVK: don't presume that the rest of us work for small companies. There is "untested", and there is utter paranoia and removing discretion from developers who are highly educated and highly paid for a reason. – Ether Jun 25 '10 at 21:10
  • 5
    @Ether - I'm not assuming that. I'm assuming that the "just use it and ask forgiveness later" **might** be an acceptable attitude at a small company - for a good or bad reason - but is generally, for a good reason, NOT and acceptable one in a large company, despite the opinion of individual developers on the topic and my liking of the cowboy ways personally – DVK Jun 25 '10 at 21:18
  • 3
    Also, the kneejerk reaction of "find another job" often seen on SO Perl tag is way off base IMHO. Ability to do whatever-you-feel-needed-and-damn-the-consequences is not always the overriding reason people want to work for a specific company, and rarely the major one. – DVK Jun 25 '10 at 21:23
  • @DVK: perhaps not, but if I couldn't be trusted to do my job, it would certainly speed up my looking for another one. – Ether Jun 25 '10 at 21:33
  • 6
    Let me explain why I can't use untested code. I work for one of the largest exchanges in the world. If I use a module that is untested and it causes someone to lose millions or billions of dollars, there is no forgiveness. We practice such "paranoia" because so much can be lost and possibly put a number of countries into a depression. DVK is correct that not everyone can "just use it and ask forgiveness later". And I love my job even though I can only use the core Perl modules. It does make my job more difficult until the modules are tested, but that is fine with me. – Matt Pascoe Jun 25 '10 at 22:30
  • 2
    @Ether - if you can trust yourself to know that a new module will not break 1000s of diverse scripts and applications using 1000s of modules, most of which few/nobody in the company even knows about, you're either a supergenius or way too self-confident for both your own and the company's good. And if you don't believe that this CAN cause problems, you haven't been around enough legacy/fragile code. I have, and hav observed perfectly nice and safe CPAN modules causing unanticipated issues - thankfully, caught during extensive testing. – DVK Jun 26 '10 at 01:01
  • 2
    @Ether - as a specific example, Archive::Tar (IIRC) caused a web server to run out of memory due to very tricky memory leak. Short of 3-month-long beta test run, NO WAY anyone would have discovered that... and having literally trillions of dollars of assets not manageable because 100s of major financial clients can't use their system due to web servers crashing is NOT what I'd consider a good trade-off for shortened CPAN module start-to-market adoption. – DVK Jun 26 '10 at 01:04
  • 1
    @Ether - and heck, it may sound vain but knowing my work contributes tangibly to said trillions of dollars of assets being managed is part of why I keep liking my work despite having to deal with a lot more red tape than I would have otherwise preferred. – DVK Jun 26 '10 at 01:06

2 Answers2

3

You can use ctime, depending on your definition of "Normal time":

Example code:

use Time::Local; 
use Time::localtime; 
my $time=timelocal(1,2,3,24,6,2010);
print "$time\n"; 
$theTime = ctime($time); 
print "$theTime\n";

Result:

1279954921
Sat Jul 24 03:02:01 2010

Also, you don't need to use Time::Localtime (which is why you get Time::tm instead of a standard array/string from Perl's internal localtime):

use Time::Local; 
my $time=timelocal(1,2,3,24,6,2010); 
print "$time\n"; 
$theTime = localtime($time); 
print "$theTime\n";

1279954921
Sat Jul 24 03:02:01 2010
DVK
  • 126,886
  • 32
  • 213
  • 327
2

Don't forget to subtract 1900 from the year!

Remember that in scalar context, localtime and gmtime returns a ctime-formatted string, so you could use it as in the following. If that's unsuitable, you might want to use strftime from the POSIX module.

#! /usr/bin/perl

use warnings;
use strict;

use Time::Local;

my $start = "01:02:03";
my $end   = "01:02:05";
my $date  = "2010-02-10";

my($year,$mon,$mday) = split /-/, $date;
$mon--;
$year -= 1900;

my($startTime,$endTime) =
  map { my($hour,$min,$sec) = split /:/;
        timelocal $sec,$min,$hour,$mday,$mon,$year }
  $start, $end;

for (my $i = $startTime; $i <= $endTime + 29; $i++) {
  print scalar localtime($i), "\n";
}

print "$startTime   $endTime \n";

Tail of the output:

Wed Feb 10 01:02:26 2010
Wed Feb 10 01:02:27 2010
Wed Feb 10 01:02:28 2010
Wed Feb 10 01:02:29 2010
Wed Feb 10 01:02:30 2010
Wed Feb 10 01:02:31 2010
Wed Feb 10 01:02:32 2010
Wed Feb 10 01:02:33 2010
Wed Feb 10 01:02:34 2010
1265785323   1265785325
Gray
  • 115,027
  • 24
  • 293
  • 354
Greg Bacon
  • 134,834
  • 32
  • 188
  • 245