How to compare timegm in Perl? -
i need on code below. wanted earliest time "inspection start" column , latest time "inspection stop" column . issue script not seem correctly pick earliest time. or possible compare timegm format using 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 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 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 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 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 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 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 9334 8182 8107 73 2 75 99.08
my code:
#!/usr/bin/perl use time::timegm; $line = ""; $ins_start_time =""; $ins_end_time =""; $tst_start_time =""; $tst_start_time =""; open fh, "<file.txt" or die "could not open file, $!"; while($line=<fh>) { chomp($line); $line =~ s/\cm//g; @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: start time =1394602153 ##---->incorrect start time =1394602269
expected output: start time =1394600409 ##---->correct start time =1394602269
this might little easier if use time::piece.
#!/usr/bin/perl use strict; use warnings; use 5.010; use time::piece; $fmt = '%m/%d/%y_%h:%m:%s_%p'; # assumes of data in past $min_time = localtime; $max_time = localtime(0); while (<data>) { ($start_str, $stop_str) = (split)[2,3]; $start_time = time::piece->strptime($start_str, $fmt); $stop_time = time::piece->strptime($stop_str, $fmt); if ($start_time->epoch < $min_time->epoch) { $min_time = $start_time; } if ($stop_time->epoch > $max_time->epoch) { $max_time = $stop_time; } } 'start - ', $min_time->strftime($fmt); 'stop - ', $max_time->strftime($fmt); __data__ e4855 wi_left01 3/12/2014_5:00:09_am 3/12/2014_5:02:08_am 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 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 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 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 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 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 9334 8182 8107 73 2 75 99.08
Comments
Post a Comment