#!/usr/bin/perl

use strict;



sub time2float($){
	my $time = shift;
	if($time != 0){
		my @parts = split /:|\./, $time;
		if($#parts == 3){
			return $parts[0]*3600+$parts[1]*60+$parts[2]+$parts[3]/1000;
		}else{
			return -1;
		}
	}else{
		return 0;
	}
}

if(-f $ARGV[0]){
	open(FILE,$ARGV[0]) or die ("Could not open $ARGV[0]: $!");
	my $line = 0;
	while(<FILE>){
		chomp;
		if($line >= 4){
			my @values = split /,/;
			my $a = 0, $b = 0;
			for(my $i; $i <= $#values; $i++){
				if($i % 3 == 1){
					$a = time2float($values[$i] ? $values[$i] : 0);
				}elsif($i % 3 == 2){
					$b = time2float($values[$i] ? $values[$i] : 0);
					printf("%.3f ", $b-$a);
				}
			}
			print "\n";
		}
		$line++;
	}
}else{
	print "Usage: csv2matlab <intputfile>\n";
}
