added add_timestring method.

added tsplot script.
cleaned up test output.
This commit is contained in:
hjp 2003-08-08 13:01:59 +00:00
parent 03cb462f75
commit 1145af3d59
5 changed files with 87 additions and 7 deletions

View File

@ -9,5 +9,5 @@ WriteMakefile(
'Time::Local' => 0, 'Time::Local' => 0,
'Data::Dumper' => 0, 'Data::Dumper' => 0,
}, },
'EXE_FILES' => [ ], 'EXE_FILES' => [ 'tsplot' ],
); );

View File

@ -3,8 +3,10 @@ package TimeSeries;
use File::Temp qw(tempfile); use File::Temp qw(tempfile);
use Time::Local; use Time::Local;
use Data::Dumper; use Data::Dumper;
use HTTP::Date qw(parse_date);
use Time::Local qw(timegm_nocheck);
$VERSION = do { my @r=(q$Revision: 1.4 $=~/\d+/g);sprintf "%d."."%02d"x$#r,@r}; $VERSION = do { my @r=(q$Revision: 1.5 $=~/\d+/g);sprintf "%d."."%02d"x$#r,@r};
sub new { sub new {
my ($class, %opts) = @_; my ($class, %opts) = @_;
@ -26,6 +28,27 @@ sub add {
#print Dumper($self); #print Dumper($self);
} }
sub add_timestring {
my ($self, $timestring, @data) = @_;
my ($year, $mon, $day, $hour, $min, $sec, $zone)
= parse_date($timestring);
# print STDERR "date = ($year, $mon, $day, $hour, $min, $sec, $zone)\n";
my $timestamp;
if (defined ($zone)) {
# adjust for timezone
my ($zs, $zh, $zm) = $zone =~ /([+-])(\d\d)(\d\d)/;
$min -= ($zs eq '-' ? -1 : +1) * ($zh * 60 + $zm);
$timestamp = timegm_nocheck($sec, $min, $hour, $day, $mon-1, $year);
} else {
$timestamp = timelocal($sec, $min, $hour, $day, $mon-1, $year);
}
# print STDERR "\$timestamp = $timestamp\n";
$self->add($timestamp, @data);
}
sub legend { sub legend {
my ($self, @legend) = @_; my ($self, @legend) = @_;
@ -271,8 +294,9 @@ sub plot {
$pipe .= "pnmtopng |"; $pipe .= "pnmtopng |";
} }
if ($self->{output_format} eq "gif") { if ($self->{output_format} eq "gif") {
# the ppm tools are noisy. Shut them up.
$pipe .= "ppmquant 256 2> /dev/null |" . $pipe .= "ppmquant 256 2> /dev/null |" .
"ppmtogif |"; "ppmtogif 2> /dev/null |";
} }
if ($self->{output_format} eq "jpeg") { if ($self->{output_format} eq "jpeg") {
$pipe .= "cjpeg -sample 1x1,1x1,1x1 |"; $pipe .= "cjpeg -sample 1x1,1x1,1x1 |";

View File

@ -34,7 +34,7 @@ $ts->add(1.002E9, 3);
$test = 3; $test = 3;
my $g = $ts->plot(); my $g = $ts->plot();
print STDERR "length \$g = ", length($g), "\n"; # print STDERR "length \$g = ", length($g), "\n";
if (length($g) > 0 && substr($g, 0, 4) eq "\211PNG") { if (length($g) > 0 && substr($g, 0, 4) eq "\211PNG") {
print "ok $test\n"; print "ok $test\n";
} else { } else {
@ -44,7 +44,7 @@ if (length($g) > 0 && substr($g, 0, 4) eq "\211PNG") {
$test = 4; $test = 4;
$ts->output_format("gif"); $ts->output_format("gif");
my $g = $ts->plot(); my $g = $ts->plot();
print STDERR "length \$g = ", length($g), "\n"; # print STDERR "length \$g = ", length($g), "\n";
if (length($g) > 0 && substr($g, 0, 6) eq "GIF87a") { if (length($g) > 0 && substr($g, 0, 6) eq "GIF87a") {
print "ok $test\n"; print "ok $test\n";
} else { } else {
@ -54,7 +54,7 @@ if (length($g) > 0 && substr($g, 0, 6) eq "GIF87a") {
$test = 5; $test = 5;
$ts->output_format("jpeg"); $ts->output_format("jpeg");
my $g = $ts->plot(); my $g = $ts->plot();
print STDERR "length \$g = ", length($g), "\n"; # print STDERR "length \$g = ", length($g), "\n";
if (length($g) > 0 && substr($g, 0, 10) eq "\377\330\377\340\000\020JFIF") { if (length($g) > 0 && substr($g, 0, 10) eq "\377\330\377\340\000\020JFIF") {
print "ok $test\n"; print "ok $test\n";
} else { } else {
@ -64,7 +64,7 @@ if (length($g) > 0 && substr($g, 0, 10) eq "\377\330\377\340\000\020JFIF") {
$test = 6; $test = 6;
$ts->output_format("ps"); $ts->output_format("ps");
my $g = $ts->plot(); my $g = $ts->plot();
print STDERR "length \$g = ", length($g), "\n"; # print STDERR "length \$g = ", length($g), "\n";
if (length($g) > 0 && substr($g, 0, 2) eq "%!") { if (length($g) > 0 && substr($g, 0, 2) eq "%!") {
print "ok $test\n"; print "ok $test\n";
} else { } else {

38
t/1parse.t Normal file
View File

@ -0,0 +1,38 @@
######################### We start with some black magic to print on failure.
BEGIN { $| = 1; print "1..4\n"; }
END {print "not ok 1\n" unless $loaded;}
use TimeSeries;
$loaded = 1;
print "ok 1\n";
######################### End of black magic.
my $test;
$test = 2;
my $ts = TimeSeries->new();
if (defined($ts)) {
print "ok $test\n";
} else {
print "not ok $test\n";
}
$ts->legend("value");
$test = 3;
$ts->add_timestring('2003-07-06T17:52:33', 5);
if ($ts->{data}[0][0] == 1057506753) {
print "ok $test\n";
} else {
print "not ok $test\n";
}
$test = 4;
$ts->add_timestring('06/Jul/2003:17:52:34 +0200', 5);
if ($ts->{data}[1][0] == 1057506754) {
print "ok $test\n";
} else {
print "not ok $test\n";
}

18
tsplot Executable file
View File

@ -0,0 +1,18 @@
#!/usr/bin/perl -w
use strict;
use TimeSeries;
my $ts = TimeSeries->new();
while (<>) {
my @legend = split;
shift @legend; # first must be for timestamp
$ts->legend(@legend);
last;
}
while (<>) {
my ($timestamp, @values) = split();
$ts->add_timestring($timestamp, @values);
}
my $g = $ts->plot();
print $g