diff --git a/Makefile.PL b/Makefile.PL index f4d89e4..e185668 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -9,5 +9,5 @@ WriteMakefile( 'Time::Local' => 0, 'Data::Dumper' => 0, }, - 'EXE_FILES' => [ ], + 'EXE_FILES' => [ 'tsplot' ], ); diff --git a/TimeSeries.pm b/TimeSeries.pm index 66a66d0..5e5243f 100644 --- a/TimeSeries.pm +++ b/TimeSeries.pm @@ -3,8 +3,10 @@ package TimeSeries; use File::Temp qw(tempfile); use Time::Local; 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 { my ($class, %opts) = @_; @@ -26,6 +28,27 @@ sub add { #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 { my ($self, @legend) = @_; @@ -271,8 +294,9 @@ sub plot { $pipe .= "pnmtopng |"; } if ($self->{output_format} eq "gif") { + # the ppm tools are noisy. Shut them up. $pipe .= "ppmquant 256 2> /dev/null |" . - "ppmtogif |"; + "ppmtogif 2> /dev/null |"; } if ($self->{output_format} eq "jpeg") { $pipe .= "cjpeg -sample 1x1,1x1,1x1 |"; diff --git a/t/0basic.t b/t/0basic.t index 6d37db2..80790f1 100644 --- a/t/0basic.t +++ b/t/0basic.t @@ -34,7 +34,7 @@ $ts->add(1.002E9, 3); $test = 3; 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") { print "ok $test\n"; } else { @@ -44,7 +44,7 @@ if (length($g) > 0 && substr($g, 0, 4) eq "\211PNG") { $test = 4; $ts->output_format("gif"); 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") { print "ok $test\n"; } else { @@ -54,7 +54,7 @@ if (length($g) > 0 && substr($g, 0, 6) eq "GIF87a") { $test = 5; $ts->output_format("jpeg"); 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") { print "ok $test\n"; } else { @@ -64,7 +64,7 @@ if (length($g) > 0 && substr($g, 0, 10) eq "\377\330\377\340\000\020JFIF") { $test = 6; $ts->output_format("ps"); 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 "%!") { print "ok $test\n"; } else { diff --git a/t/1parse.t b/t/1parse.t new file mode 100644 index 0000000..a130c90 --- /dev/null +++ b/t/1parse.t @@ -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"; +} + diff --git a/tsplot b/tsplot new file mode 100755 index 0000000..4ea0151 --- /dev/null +++ b/tsplot @@ -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