diff --git a/TimeSeries.pm b/TimeSeries.pm index d2a8070..1206403 100644 --- a/TimeSeries.pm +++ b/TimeSeries.pm @@ -31,7 +31,7 @@ use Time::Local; use Data::Dumper; use HTTP::Date qw(parse_date); use Time::Local qw(timegm_nocheck); -use POSIX qw(strftime); +use POSIX qw(floor strftime); our $VERSION = do { my @r=(q$Revision: 1.24 $=~/\d+/g);sprintf "%d."."%02d"x$#r,@r}; @@ -404,8 +404,10 @@ sub plot { my @tics = get_ticks($firsttime, $lasttime); + # force tick values to look like fp numbers to avoid warnings about + # exceeding the int range from gnuplot print $ctlfh "set xtics rotate (", - join(", ", map sprintf(qq|"%s" %d|, $_->[1], $_->[0]), @tics), + join(", ", map sprintf(qq|"%s" %.16e|, $_->[1], $_->[0]), @tics), ")\n"; # what to plot @@ -499,7 +501,32 @@ sub get_ticks { my $label; my $nexttime; - if ($lasttime - $firsttime > 3 * 365 * 24 * 3600) { + if ($lasttime - $firsttime > 50 * 365 * 24 * 3600) { + # more than 50 years: 1 tick/5 years + + my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($firsttime); + $sec = $min = $hour = 0; + $mday = 1; + $mon = 0; + $year = floor($year / 5) * 5; + $firsttime = timelocal($sec,$min,$hour,$mday,$mon,$year); + + $label = '%Y-%m-%d'; + + $nexttime = sub { return add_years($_[0], 5) }; + } elsif ($lasttime - $firsttime > 10 * 365 * 24 * 3600) { + # more than 10 years: 1 tick/year + + my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($firsttime); + $sec = $min = $hour = 0; + $mday = 1; + $mon = 0; + $firsttime = timelocal($sec,$min,$hour,$mday,$mon,$year); + + $label = '%Y-%m-%d'; + + $nexttime = sub { return add_years($_[0], 1) }; + } elsif ($lasttime - $firsttime > 3 * 365 * 24 * 3600) { # more than 3 years: 4 ticks/year my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($firsttime); @@ -601,11 +628,21 @@ sub add_months { my ($time, $d_mon) = @_; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($time); + $year += 1900; # localtime/timelocal mismatch $mon += $d_mon; if ($mon >= 12) { $mon -= 12; $year++; } $time = timelocal($sec,$min,$hour,$mday,$mon,$year); -}; +} + +sub add_years { + my ($time, $d_year) = @_; + my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) + = localtime($time); + $year += 1900; # localtime/timelocal mismatch + $year += $d_year; + $time = timelocal($sec,$min,$hour,$mday,$mon,$year); +} 1;