Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calculate ms date difference between 2 arrays

Status
Not open for further replies.

penguin2020

Programmer
Apr 10, 2011
1
GB
I have 2 files one contains time string values in the following format 09:00.000 and another file that has time string valuesin the same format

I am interested in calculating the millisecond time difference between each of the two files

e.g the time in the first row of file a minus the tim in the first row of file b and so on until the end of each file. Both files contain the same number of time entries.

I thought that I would

-read each file into a separate array
- loop through via foreach and subtract array_a[0] from array_b[0] and so on.

Problem is I don't know how to do it

Heres my code
{CODE}
#!/usr/bin/perl

$ltimefile="linetime.txt";
$stimefile="sendtime.txt";
open(TZ1, $stimefile);
@stimefinal=<TZ1>;
close(TZ1);
open(TZ2, $ltimefile);
@limefinal=<TZ2>;
close(TZ2);

foreach(@ltimefile, @stimefile){

$ltimefile - $stimefile
my @components = split /[:\.]/, shift;
return (($components[0] * 60 + $components[1]) * 60 + $components[
+2]) * 1000 + $components[3];
}

}

{CODE}


#timestamps in both files look like this

7:18:00.416
17:51:41.580
07:18:10.983
17:51:41.773
07:18:11.866
17:51:41.799
07:18:12.079
17:51:41.805
07:18:13.642
17:51:41.853
07:18:13.642
17:51:41.853
07:18:13.647
17:51:41.853
07:18:13.658
17:51:41.854
07:18:13.667
17:51:41.855
07:18:14.731
17:51:41.890
07:18:14.738
17:51:41.890
07:18:14.877
17:51:41.896
07:18:14.877

#
#

Please help!
 
You are not that far from your (very simple) goal, but you should decide first what to do with your calculated values: output to a file, to the screen, collect them in an array,...
Also you should be certain that your times are always within the same day, otherwise...
And you would need also some error checking in the code.
Anyway you could start from here (untested):
Code:
#!/usr/bin/perl
if(open(TZ1,'linetime.txt')){
  if(open(TZ2,'sendtime.txt')){
    my(@lt,@st,$dif);
    while($ltimefile=<TZ1>){
      $stimefile=<TZ2>;
      @lt=split/[:.]/,$ltimefile;
      @st=split/[:.]/,$stimefile;
      $dif=(($lt[0]*60+$lt[1])*60+$lt[2])*1000+$lt[3]-(($st[0]*60+$st[1])*60+$st[2])*1000-$st[3];
      print$dif,"\n";
    }
    close TZ2;
  }
  close TZ1;
}

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Code:
#!/usr/bin/perl

use strict;
use warnings;

my $ltimefile = 'linetime.txt';
my $stimefile = 'sendtime.txt';

open my $fh1, $ltimefile or die "Can't open $ltimefile: $!";
open my $fh2, $stimefile or die "Can't open $stimefile: $!";

while (!eof($fh1) && !eof($fh2)) {
	chomp(my $line1 = <$fh1>);
	chomp(my $line2 = <$fh2>);

	my $diff = ms_to_time(time_to_ms($line2) - time_to_ms($line1));
	
	print "$line2 - $line1 = $diff\n";
}

sub time_to_ms {
	my $time = shift;
	my @array = split /[:.]/, $time;
	return $array[3] + 1000 * ($array[2] + 60 * ($array[1] + 60 * $array[0]));
}

sub ms_to_time {
	my $time = shift;
	
	my $ms = $time % 1000; $time = ($time - $ms) / 1000;
	my $sec = $time % 60; $time = ($time - $sec) / 60;
	my $min = $time % 60; $time = ($time - $min) / 60;
	my $hour = $time;
	
	return sprintf "%d:%02d:%02d.%03d", $hour, $min, $sec, $ms;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top