CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
compare timegm

 



alferic
Novice

Apr 14, 2014, 1:40 AM

Post #1 of 7 (2687 views)
compare timegm Can't Post

hello Guru's

I need your help again. I wanted to get the earliest time from the "Inspection Start" column and the get the latest time from the "Inspection Stop" column. The issue is my script does not seem to correctly pick up the earliest time. Or is it possible to compare timegm format using the script below

My data:
Inspection Start Inspection Stop
E4855 WI_LEFT01 3/12/2014_5:00:09_AM 3/12/2014_5:02:08_AM 1m_59s 9334 8195 8135 59 1 60 99.27
H0096 WI_LEFT01 3/12/2014_5:22:58_AM 3/12/2014_5:24:55_AM 1m_57s 9334 8197 8138 58 1 59 99.28
L0998 WI_LEFT01 3/12/2014_5:29:13_AM 3/12/2014_5:31:09_AM 1m_56s 9334 8163 8088 73 2 75 99.08
P0113 WI_LEFT01 3/12/2014_5:15:37_AM 3/12/2014_5:17:39_AM 2m_2s 9334 8008 7927 80 1 81 98.99
P0149 WI_LEFT01 3/12/2014_5:12:36_AM 3/12/2014_5:14:31_AM 1m_55s 9334 8195 8125 68 2 70 99.15
T2765 WI_LEFT01 3/12/2014_5:25:59_AM 3/12/2014_5:28:00_AM 2m_1s 9334 7810 7732 77 1 78 99.00
T5518 WI_LEFT01 3/12/2014_5:04:37_AM 3/12/2014_5:06:37_AM 2m_0s 9334 8182 8107 73 2 75 99.08

my code:
#!/usr/bin/perl

use Time::timegm;

my $line = "";
my $ins_start_time ="";
my $ins_end_time ="";
my $tst_start_time ="";
my $tst_start_time ="";

open FH, "<file.txt" or die "could not open file, $!";

while($line=<FH>)
{
chomp($line);
$line =~ s/\cM//g;

my @dummy = split /\s/, $line;
#print "$line\n";

if ($dummy[0] =~ /^[A-Z]\d{0,4}$/i)
{
($dump, $dump, $st_dump, $et_dump, @dump) = @dummy;

($mm, $dd, $yy, $hh, $min, $sec, $dump) = split /[\/\_\:]+/, $st_dump;
$ins_start_time = timegm($sec, $min, $hh, $dd, $mm-1, $yy);
print "$ins_start_time\t";

if ($tst_start_time <= $ins_start_time)
{
$tst_start_time = $ins_start_time;
}

($mm, $dd, $yy, $hh, $min, $sec, $dump) = split /[\/\_\:]+/, $et_dump;
$ins_end_time = timegm($sec, $min, $hh, $dd, $mm-1, $yy);
print "$ins_end_time\n";

if ($ins_end_time >= $tst_end_time)
{
$tst_end_time = $ins_end_time;
}
}
}

close FH;

print "Start Time =$tst_start_time\n";
print "Start Time =$tst_end_time\n";

OUTPUT:
1394600409 1394600528
1394601778 1394601895
1394602153 1394602269
1394601337 1394601459
1394601156 1394601271
1394601959 1394602080
1394600677 1394600797
Start Time =1394602153 ---->incorrect
Start Time =1394602269

REQUIRED OUTPUT:
1394600409 1394600528
1394601778 1394601895
1394602153 1394602269
1394601337 1394601459
1394601156 1394601271
1394601959 1394602080
1394600677 1394600797
Start Time =1394600409
Start Time =1394602269


BillKSmith
Veteran

Apr 14, 2014, 5:30 AM

Post #2 of 7 (2680 views)
Re: [alferic] compare timegm [In reply to] Can't Post

You seem to want the earliest $tst_start_time, not the latest.

use

Code
 
my $ins_start_time = 0;
my $ins_end_time = 0;
my $tst_start_time = 0x7fffffff;
my $tst_end_time = 0;
.
.
.
if ( $tst_start_time >= $ins_start_time ) {
$tst_start_time = $ins_start_time;
}


Always use strict and warnings. They would have found at least one of the errors corrected here.
Good Luck,
Bill


alferic
Novice

Apr 14, 2014, 4:59 PM

Post #3 of 7 (2672 views)
Re: [BillKSmith] compare timegm [In reply to] Can't Post

Hello Bill,

This is great! your solution works! Thanks a lot.

By the way, I notice that you put some values on one of the variables, what that this mean?

my $tst_start_time = 0x7fffffff;


Laurent_R
Veteran / Moderator

Apr 15, 2014, 1:15 AM

Post #4 of 7 (2632 views)
Re: [alferic] compare timegm [In reply to] Can't Post

0x7fffffff is, in hexadecimal notation, the largest signed integer number that can be represented on 32 bits. It is equal to 2**31 - 1, or 2_147_483_647 in decimal notation.


alferic
Novice

Apr 15, 2014, 2:25 AM

Post #5 of 7 (2629 views)
Re: [Laurent_R] compare timegm [In reply to] Can't Post

Thanks Laurent!

I appreciate it.


BillKSmith
Veteran

Apr 15, 2014, 6:09 AM

Post #6 of 7 (2578 views)
Re: [alferic] compare timegm [In reply to] Can't Post

You may be interested. There is a name for this constant.


Code
use POSIX: 
.
.
.
my $tst_start_time = POSIX::LONG_MAX;


I thought that you would more likely to recognize the hex value than either the POSIX name or the decimal value.
Good Luck,
Bill


alferic
Novice

Apr 16, 2014, 2:40 AM

Post #7 of 7 (2434 views)
Re: [BillKSmith] compare timegm [In reply to] Can't Post

Thanks Bill!

I learn a lot from this thread. Wink

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives