Extend usable time range to about 200 years

Previously the maximum grid spacing was 3 months which became
impractical with more than about 10 years of data. Now there are also
1 year and 5 year grid spacings.
Also worked around timelocal() DWIMery which would return wrong values
for much of the 20th century.
This commit is contained in:
Peter J. Holzer 2016-06-04 13:17:19 +02:00
parent 59902c1322
commit 08e8660ad9
1 changed files with 41 additions and 4 deletions

View File

@ -31,7 +31,7 @@ use Time::Local;
use Data::Dumper; use Data::Dumper;
use HTTP::Date qw(parse_date); use HTTP::Date qw(parse_date);
use Time::Local qw(timegm_nocheck); 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}; 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); 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 (", print $ctlfh "set xtics rotate (",
join(", ", map sprintf(qq|"%s" %d|, $_->[1], $_->[0]), @tics), join(", ", map sprintf(qq|"%s" %.16e|, $_->[1], $_->[0]), @tics),
")\n"; ")\n";
# what to plot # what to plot
@ -499,7 +501,32 @@ sub get_ticks {
my $label; my $label;
my $nexttime; 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 # more than 3 years: 4 ticks/year
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($firsttime); my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($firsttime);
@ -601,11 +628,21 @@ sub add_months {
my ($time, $d_mon) = @_; my ($time, $d_mon) = @_;
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
= localtime($time); = localtime($time);
$year += 1900; # localtime/timelocal mismatch
$mon += $d_mon; $mon += $d_mon;
if ($mon >= 12) { if ($mon >= 12) {
$mon -= 12; $year++; $mon -= 12; $year++;
} }
$time = timelocal($sec,$min,$hour,$mday,$mon,$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; 1;