Code:
C:\>
C:\>type data.txt
12.06.09/14:20:13 4 sms/s
12.06.09/14:20:16 4 sms/s
12.06.09/14:20:17 3 sms/s
12.06.09/14:20:18 3 sms/s
12.06.09/14:20:25 5 sms/s
12.06.09/14:20:26 6 sms/s
12.06.09/14:20:29 2 sms/s
12.06.09/14:20:32 8 sms/s
12.06.09/14:20:33 4 sms/s
12.06.09/14:20:34 2 sms/s
12.06.09/14:20:35 3 sms/s
C:\>
C:\>type densify.pl
#!perl -w
use Date::Calc;
my $startdate;
my $enddate;
# process file, populate hash, set start and end dates
# IMPORTANT - it is assumed that the dates in the file are sorted
open(DATES,"data.txt") or die "Can't open data.txt: $!";
while (<DATES>) {
chomp($line=$_);
($key = $line) =~ s/^([^ ]+) (.*)$/$1/;
($value = $line) =~ s/^([^ ]+) (.*)$/$2/;
$startdate = $key if $. == 1;
$enddate = $key;
$exists{$key} = $value;
}
close(DATES) or die "Can't close data.txt: $!";
# split startdate and create @startts = (No_of_days_since_1/1/1, <additional_seconds>)
($sdy,$smn,$syr,$shr,$smi,$ssc) = split /[\.\/:]/, $startdate;
$syr += 2000;
@startts = ( Date::Calc::Date_to_Days($syr,$smn,$sdy), ($shr*60+$smi)*60+$ssc );
# split enddate and create @endts = (No_of_days_since_1/1/1, <additional_seconds>)
($edy,$emn,$eyr,$ehr,$emi,$esc) = split /[\.\/:]/, $enddate;
$eyr += 2000;
@endts = ( Date::Calc::Date_to_Days($eyr,$emn,$edy), ($ehr*60+$emi)*60+$esc );
# diff[0] = diff_of_days_from_common_epoch; diff[1] = diff_of_seconds
@diff = ($endts[0]-$startts[0], $endts[1]-$startts[1]);
# now loop through each second increment and print key value from hash if it exists, "0" otherwise
while ($diff[0] >= 0 and $diff[1] >= 0) {
$key = sprintf("%02d.%02d.%02d/%02d:%02d:%02d",$sdy,$smn,substr($syr,2),$shr,$smi,$ssc);
printf("%s %s\n",$key, defined $exists{$key} ? $exists{$key} : "0");
($syr,$smn,$sdy,$shr,$smi,$ssc) = Date::Calc::Add_Delta_YMDHMS($syr,$smn,$sdy,$shr,$smi,$ssc,0,0,0,0,0,1);
@startts = ( Date::Calc::Date_to_Days($syr,$smn,$sdy), ($shr*60+$smi)*60+$ssc );
@diff = ($endts[0]-$startts[0], $endts[1]-$startts[1]);
}
C:\>
C:\>
C:\>perl densify.pl
12.06.09/14:20:13 4 sms/s
12.06.09/14:20:14 0
12.06.09/14:20:15 0
12.06.09/14:20:16 4 sms/s
12.06.09/14:20:17 3 sms/s
12.06.09/14:20:18 3 sms/s
12.06.09/14:20:19 0
12.06.09/14:20:20 0
12.06.09/14:20:21 0
12.06.09/14:20:22 0
12.06.09/14:20:23 0
12.06.09/14:20:24 0
12.06.09/14:20:25 5 sms/s
12.06.09/14:20:26 6 sms/s
12.06.09/14:20:27 0
12.06.09/14:20:28 0
12.06.09/14:20:29 2 sms/s
12.06.09/14:20:30 0
12.06.09/14:20:31 0
12.06.09/14:20:32 8 sms/s
12.06.09/14:20:33 4 sms/s
12.06.09/14:20:34 2 sms/s
12.06.09/14:20:35 3 sms/s
C:\>
C:\>