Refactorization: get_ticks consists essentially of four parts:
An computing the initial value, generating the label, computing the next part, and a loop around the whole stuff. Extracted the loop from the if-else cascade.
This commit is contained in:
parent
3d0b7f7cbe
commit
6833d0b350
111
TimeSeries.pm
111
TimeSeries.pm
|
@ -31,8 +31,9 @@ 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);
|
||||||
|
|
||||||
our $VERSION = do { my @r=(q$Revision: 1.16 $=~/\d+/g);sprintf "%d."."%02d"x$#r,@r};
|
our $VERSION = do { my @r=(q$Revision: 1.17 $=~/\d+/g);sprintf "%d."."%02d"x$#r,@r};
|
||||||
|
|
||||||
=head2 new(%opts)
|
=head2 new(%opts)
|
||||||
|
|
||||||
|
@ -442,6 +443,9 @@ sub get_ticks {
|
||||||
|
|
||||||
my @ticks = ();
|
my @ticks = ();
|
||||||
|
|
||||||
|
my $label;
|
||||||
|
my $nexttime;
|
||||||
|
|
||||||
if ($lasttime - $firsttime > 3 * 365 * 24 * 3600) {
|
if ($lasttime - $firsttime > 3 * 365 * 24 * 3600) {
|
||||||
# more than 3 years: 4 ticks/year
|
# more than 3 years: 4 ticks/year
|
||||||
|
|
||||||
|
@ -450,16 +454,10 @@ sub get_ticks {
|
||||||
$mday = 1;
|
$mday = 1;
|
||||||
$mon = int($mon/3) * 3;
|
$mon = int($mon/3) * 3;
|
||||||
$firsttime = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
$firsttime = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
||||||
my $time;
|
|
||||||
for (;;) {
|
$label = '%Y-%m-%d';
|
||||||
$time = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
|
||||||
push @ticks, [$time, sprintf('%04d-%02d-%02d', $year+1900, $mon+1, $mday)];
|
$nexttime = sub { return add_months($_[0], 3) };
|
||||||
$mon += 3;
|
|
||||||
if ($mon >= 12) {
|
|
||||||
$mon -= 12; $year++;
|
|
||||||
}
|
|
||||||
if ($time > $lasttime) {last}
|
|
||||||
}
|
|
||||||
} elsif ($lasttime - $firsttime > 3 * 30 * 24 * 3600) {
|
} elsif ($lasttime - $firsttime > 3 * 30 * 24 * 3600) {
|
||||||
# 3 to 36 months: 1 tick/month
|
# 3 to 36 months: 1 tick/month
|
||||||
|
|
||||||
|
@ -467,15 +465,10 @@ sub get_ticks {
|
||||||
$sec = $min = $hour = 0;
|
$sec = $min = $hour = 0;
|
||||||
$mday = 1;
|
$mday = 1;
|
||||||
$firsttime = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
$firsttime = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
||||||
my $time;
|
|
||||||
for (;;) {
|
$label = '%Y-%m-%d';
|
||||||
$time = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
|
||||||
push @ticks, [$time, sprintf('%04d-%02d-%02d', $year+1900, $mon+1, $mday)];
|
$nexttime = sub { return add_months($_[0], 1) };
|
||||||
if (++$mon >= 12) {
|
|
||||||
$mon = 0; $year++;
|
|
||||||
}
|
|
||||||
if ($time > $lasttime) {last}
|
|
||||||
}
|
|
||||||
} elsif ($lasttime - $firsttime > 30 * 24 * 3600) {
|
} elsif ($lasttime - $firsttime > 30 * 24 * 3600) {
|
||||||
# 30 ... 90 days: 1 tick/week.
|
# 30 ... 90 days: 1 tick/week.
|
||||||
|
|
||||||
|
@ -484,56 +477,42 @@ sub get_ticks {
|
||||||
|
|
||||||
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($firsttime);
|
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($firsttime);
|
||||||
$sec = $min = $hour = 0;
|
$sec = $min = $hour = 0;
|
||||||
my $time = $firsttime = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
$firsttime = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
||||||
for (;;) {
|
|
||||||
($sec,$min,$hour,$mday,$mon,$year) = localtime($time);
|
|
||||||
push @ticks, [$time, sprintf('%04d-%02d-%02d', $year+1900, $mon+1, $mday)];
|
|
||||||
if ($time > $lasttime) {last}
|
|
||||||
|
|
||||||
$time += 7 * 24 * 3600;
|
$label = '%Y-%m-%d';
|
||||||
$time = dstcorr($time);
|
|
||||||
}
|
$nexttime = sub { return dstcorr($_[0] + 7 * 24 * 3600) };
|
||||||
} elsif ($lasttime - $firsttime > 8 * 24 * 3600) {
|
} elsif ($lasttime - $firsttime > 8 * 24 * 3600) {
|
||||||
# 8 .. 30 days: 1 tick per day.
|
# 8 .. 30 days: 1 tick per day.
|
||||||
|
|
||||||
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($firsttime);
|
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($firsttime);
|
||||||
$sec = $min = $hour = 0;
|
$sec = $min = $hour = 0;
|
||||||
my $time = $firsttime = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
$firsttime = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
||||||
for (;;) {
|
|
||||||
($sec,$min,$hour,$mday,$mon,$year) = localtime($time);
|
|
||||||
push @ticks, [$time, sprintf('%04d-%02d-%02d', $year+1900, $mon+1, $mday)];
|
|
||||||
if ($time > $lasttime) {last}
|
|
||||||
|
|
||||||
$time += 24 * 3600;
|
$label = '%Y-%m-%d';
|
||||||
$time = dstcorr($time);
|
|
||||||
}
|
$nexttime = sub { return dstcorr($_[0] + 24 * 3600) };
|
||||||
} elsif ($lasttime - $firsttime > 2 * 24 * 3600) {
|
} elsif ($lasttime - $firsttime > 2 * 24 * 3600) {
|
||||||
# 2 .. 8 days: 1 tick/4 hours
|
# 2 .. 8 days: 1 tick/4 hours
|
||||||
|
|
||||||
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($firsttime);
|
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($firsttime);
|
||||||
$sec = $min = $hour = 0;
|
$sec = $min = 0;
|
||||||
my $time = $firsttime = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
$hour = int($hour / 4) * 4;
|
||||||
for (;;) {
|
$firsttime = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
||||||
($sec,$min,$hour,$mday,$mon,$year) = localtime($time);
|
|
||||||
push @ticks, [$time, sprintf('%04d-%02d-%02d %02d:%02d', $year+1900, $mon+1, $mday, $hour, $min)];
|
|
||||||
if ($time > $lasttime) {last}
|
|
||||||
|
|
||||||
$time += 4 * 3600;
|
$label = '%Y-%m-%d %H:%M';
|
||||||
$time = dstcorr($time, 4 * 3600);
|
|
||||||
}
|
$nexttime = sub { return dstcorr($_[0] + 4 * 3600, 4 * 3600) };
|
||||||
} elsif ($lasttime - $firsttime > 6 * 3600) {
|
} elsif ($lasttime - $firsttime > 6 * 3600) {
|
||||||
# 6 hours to 2 days: 1 tick per hour.
|
# 6 hours to 2 days: 1 tick per hour.
|
||||||
|
|
||||||
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($firsttime);
|
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($firsttime);
|
||||||
$sec = $min = 0;
|
$sec = $min = 0;
|
||||||
my $time = $firsttime = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
$firsttime = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
||||||
for (;;) {
|
|
||||||
($sec,$min,$hour,$mday,$mon,$year) = localtime($time);
|
|
||||||
push @ticks, [$time, sprintf('%04d-%02d-%02d %02d:%02d', $year+1900, $mon+1, $mday, $hour, $min)];
|
|
||||||
if ($time > $lasttime) {last}
|
|
||||||
|
|
||||||
$time += 3600;
|
$label = '%Y-%m-%d %H:%M';
|
||||||
}
|
|
||||||
|
$nexttime = sub { return $_[0] + 3600 };
|
||||||
} else {
|
} else {
|
||||||
# less than 6 hours: 1 tick per minute.
|
# less than 6 hours: 1 tick per minute.
|
||||||
# (ok, that's too much - need to find some intermediate steps,
|
# (ok, that's too much - need to find some intermediate steps,
|
||||||
|
@ -542,15 +521,31 @@ sub get_ticks {
|
||||||
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($firsttime);
|
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($firsttime);
|
||||||
$sec = 0;
|
$sec = 0;
|
||||||
my $time = $firsttime = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
my $time = $firsttime = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
||||||
for (;;) {
|
|
||||||
($sec,$min,$hour,$mday,$mon,$year) = localtime($time);
|
|
||||||
push @ticks, [$time, sprintf('%04d-%02d-%02d %02d:%02d', $year+1900, $mon+1, $mday, $hour, $min)];
|
|
||||||
if ($time > $lasttime) {last}
|
|
||||||
|
|
||||||
$time += 60;
|
$label = '%Y-%m-%d %H:%M';
|
||||||
}
|
|
||||||
|
$nexttime = sub { return $_[0] + 60 };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
my $time = $firsttime;
|
||||||
|
for (;;) {
|
||||||
|
push @ticks, [$time, strftime($label, localtime($time))];
|
||||||
|
if ($time > $lasttime) {last}
|
||||||
|
$time = $nexttime->($time);
|
||||||
|
}
|
||||||
|
|
||||||
return @ticks;
|
return @ticks;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sub add_months {
|
||||||
|
my ($time, $d_mon) = @_;
|
||||||
|
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)
|
||||||
|
= localtime($time);
|
||||||
|
$mon += $d_mon;
|
||||||
|
if ($mon >= 12) {
|
||||||
|
$mon -= 12; $year++;
|
||||||
|
}
|
||||||
|
$time = timelocal($sec,$min,$hour,$mday,$mon,$year);
|
||||||
|
};
|
||||||
|
|
||||||
1;
|
1;
|
||||||
|
|
Loading…
Reference in New Issue