blib/lib/Term/Fabulous/Chart/Scale/Time.pm

filter: Statementstmt 100% 150 / 150 Branchbran 87.5% 70 / 80 Conditioncond 85.4% 47 / 55 MC/DCmcdc 75.0% 21 / 28 Subroutinesub 100% 28 / 28 Podpod 66.6% 4 / 6 totaltotal 92.2% 320 / 347 CC 61 SCAR 25.6
CC
61
Cov
94%
fit
26.1
_auto_label
16.0
_next
13.0
CRAP 12.9
« blib/lib/Term/Fabulous/Chart/Marker.pm ↑ Summary blib/lib/Term/Fabulous/Chart/Format.pm »
1 package Term::Fabulous::Chart::Scale::Time;
2  
3 1 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 our $VERSION = '0.05';
9  
10 1 ▶use Object::Pad 0.825;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 ▶use Term::Fabulous::Chart::Scale;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 class Term::Fabulous::Chart::Scale::Time :isa(Term::Fabulous::Chart::Scale) {
15 1 ▶ use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶ use List::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use POSIX qw(floor strftime);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 ▶ use Time::Local 1.30 qw(timelocal_posix timegm_posix);
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶ use Term::Fabulous::Chart::Format qw(time_formatter);
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 1 ▶ use constant { MINUTE => 60, HOUR => 3600, DAY => 86400 };
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # The tick intervals, finest first: [ unit, count, approximate seconds ].
24 my @INTERVALS = (
25 6 ( map { [ second => $_, $_ ] } 1, 2, 5, 10, 15, 30 ),
26 6 ( map { [ minute => $_, $_ * MINUTE ] } 1, 2, 5, 10, 15, 30 ),
27 5 ( map { [ hour => $_, $_ * HOUR ] } 1, 2, 3, 6, 12 ),
28 2 ( map { [ day => $_, $_ * DAY ] } 1, 2 ),
29 [ week => 1, 7 * DAY ],
30 4 ( map { [ month => $_, $_ * 30.44 * DAY ] } 1, 2, 3, 6 ),
31 1 ( map { [ year => $_, $_ * 365.25 * DAY ] } 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000 ),
32 );
33  
34 1 field $utc :param :reader = 0;
35 6 ▶ field $interval :param :reader; # [ unit, count ]
Subroutine interval: called CC=1 SCAR=0.0
Subroutine utc: called CC=1 SCAR=0.0
Pod undocumented
Pod undocumented
36  
37 1 ▶ method kind () { return 'time' }
Subroutine kind: called CC=1 SCAR=0.0
Pod documented
38  
39 54 ▶ method position ($value) {
Subroutine position: called CC=1 SCAR=0.0
Pod documented
40 54 my ( $low, $high ) = ( $self->min, $self->max );
41 54 return ( $value - $low ) / ( $high - $low );
42 1 }
43  
44 1 ▶ method value_at ($position) {
Subroutine value_at: called CC=1 SCAR=0.0
Pod documented
45 1 return $self->min + $position * ( $self->max - $self->min );
46 1 }
47  
48 12 ▶ method fit :common (%options) {
Subroutine fit: called CC=24 SCAR=32.6
Pod documented
49 12 ▶ my $cells = $options{cells} // croak "$class: fit needs cells";
Condition50%1/2
$options{"cells"} // croak("${class}: fit needs cells")
l!l
120
MC/DC0%0/1
$options{"cells"} // croak("${class}: fit needs cells")
$options{"cells"}
Truth table50%1/2
A
A= $options{"cells"}
Aresult
11
00
50 12 ▶ croak "$class: cells must be a positive integer, got $cells" unless $cells =~ /\A[1-9][0-9]*\z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $cells =~ /\A[1-9][0-9]*\z/u 1 11 12
51 11 my ( $fixed_low, $fixed_high ) = @options{qw(min max)};
52 11 ▶ croak "$class: min ($fixed_low) must be before max ($fixed_high)" if defined $fixed_low && defined $fixed_high && $fixed_low >= $fixed_high;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fixed_low and defined $fixed_high and $fixed_low >= $fixed_high 1 10 11
Condition83%5/6
defined $fixed_low and defined $fixed_high and $fixed_low >= $fixed_high
!ll&&!rl&&r
911
defined $fixed_low and defined $fixed_high
!ll&&!rl&&r
902
MC/DC66%2/3
defined $fixed_low and defined $fixed_high and $fixed_low >= $fixed_high
defined $fixed_low defined $fixed_high $fixed_low >= $fixed_high
Truth table75%3/4
A and B and C
A= defined $fixed_lowB= defined $fixed_highC= $fixed_low >= $fixed_high
ABCresult
0XX0
10X0
1100
1111
53 10 ▶ my $utc = $options{utc} ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$options{'utc'} ? : 10 0 10
54 10 ▶ my $measure = $options{measure} // sub ($label) { length $label };
Subroutine __ANON__: called CC=1 SCAR=0.6
Condition33%1/3
$options{"measure"} // sub ($label) { length $label; }
l!l&&r!l&&!r
0100
MC/DC0%0/2
$options{"measure"} // sub ($label) { length $label; }
$options{"measure"} sub ($label) { length $label; }
Truth table33%1/3
A // B
A= $options{"measure"}B= sub ($label) { length $label; }
ABresult
1X1
011
000
55 10 ▶ my $vertical = ( $options{orientation} // 'horizontal' ) eq 'vertical';
Condition100%2/2
$options{"orientation"} // "horizontal"
l!l
19
MC/DC100%1/1
$options{"orientation"} // "horizontal"
$options{"orientation"}
Truth table100%2/2
A
A= $options{"orientation"}
Aresult
11
00
56  
57 10 ▶ my ( $low, $high ) = $options{extent} ? $options{extent}->@* : ( 0, DAY );
Branch: 1 branch
BranchTrueFalseTotal
$options{'extent'} ? : 10 0 10
58 10 ▶ $low = $fixed_low if defined $fixed_low;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fixed_low 1 9 10
59 10 ▶ $high = $fixed_high if defined $fixed_high;
Branch: 1 branch
BranchTrueFalseTotal
if defined $fixed_high 1 9 10
60 10 ▶ if ( $high <= $low ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($high <= $low) 1 9 10
61 1 ▶ ( $low, $high )
Branch: 2 branches
BranchTrueFalseTotal
defined $fixed_low ? : 0 1 1
defined $fixed_high ? : 0 1 1
62 = defined $fixed_low ? ( $low, $low + HOUR )
63 : defined $fixed_high ? ( $high - HOUR, $high )
64 : ( $low - HOUR / 2, $high + HOUR / 2 );
65 }
66  
67 10 my $chosen;
68 10 foreach my $interval (@INTERVALS) {
69 161 my ( $unit, $count, $seconds ) = @$interval;
70 161 ▶ next if ( $high - $low ) / $seconds > 500; # far too many ticks
Branch: 1 branch
BranchTrueFalseTotal
if ($high - $low) / $seconds > 500 91 70 161
71 70 ▶ my @values = _ticks( $low, $high, $unit, $count, $utc ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my(@values) = _ticks($low, $high, $unit, $count, $utc) or next 70 0 70
72 70 ▶ my $format = defined $options{format} ? time_formatter( $options{format}, $utc ) : sub ($epoch) { _auto_label( $epoch, $unit, $utc ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
defined $options{'format'} ? : 8 62 70
73 70 my @labels = map { $format->($_) } @values;
74 70 my $widest = List::Util::max( map { $measure->($_) } @labels );
75 70 my $apart = ( $cells - 1 ) * $seconds / ( $high - $low );
76 70 ▶ my $room = $vertical ? 2 : List::Util::max( $widest + 3, 8 );
Branch: 1 branch
BranchTrueFalseTotal
$vertical ? : 7 63 70
77 70 $chosen = { unit => $unit, count => $count, values => \@values, labels => \@labels };
78 70 ▶ last if $apart >= $room && ( @values >= 2 || $apart > $cells );
Branch: 1 branch
BranchTrueFalseTotal
if $apart >= $room and @values >= 2 || $apart > $cells 10 60 70
Condition50%3/6
$apart >= $room and @values >= 2 || $apart > $cells
!ll&&!rl&&r
60010
@values >= 2 or $apart > $cells
l!l&&r!l&&!r
1000
MC/DC33%1/3
$apart >= $room and @values >= 2 || $apart > $cells
$apart >= $room @values >= 2 $apart > $cells
Truth table50%2/4
A and B or C
A= $apart >= $roomB= @values >= 2C= $apart > $cells
ABCresult
0XX0
1000
11X1
1011
79 }
80 10 ▶ croak "$class: no tick interval fits the range $low .. $high" unless $chosen;
Branch: 1 branch
BranchTrueFalseTotal
unless $chosen 0 10 10
81  
82 10 my @ticks = map { { value => $chosen->{values}[$_], label => $chosen->{labels}[$_] } } 0 .. $chosen->{values}->$#*;
83 10 return $class->new( min => $low, max => $high, cells => $cells, used => $cells, utc => $utc, interval => [ @$chosen{qw(unit count)} ], ticks => \@ticks );
84 1 }
85  
86 # ---------------------------------------------------------------------
87 # Calendar arithmetic
88 # ---------------------------------------------------------------------
89  
90 12157 ▶ sub _parts ( $epoch, $utc ) {
Subroutine _parts: called CC=2 SCAR=7.2
91 12157 ▶ my @parts = $utc ? gmtime $epoch : localtime $epoch;
Branch: 1 branch
BranchTrueFalseTotal
$utc ? : 12157 0 12157
92 12157 return @parts[ 0 .. 6 ]; # sec min hour mday mon year wday
93 }
94  
95 6790 ▶ sub _epoch ( $utc, $sec, $min, $hour, $mday, $mon, $year ) {
Subroutine _epoch: called CC=2 SCAR=7.6
96 6790 ▶ return ( $utc ? \&timegm_posix : \&timelocal_posix )->( $sec, $min, $hour, $mday, $mon, $year );
Branch: 1 branch
BranchTrueFalseTotal
$utc ? : 6790 0 6790
97 }
98  
99 # The calendar date $days after (y, m, d), counted at noon UTC so no
100 # clock change gets in the way.
101 660 ▶ sub _add_days ( $mday, $mon, $year, $days ) {
Subroutine _add_days: called CC=1 SCAR=0.0
102 660 my @date = gmtime( timegm_posix( 0, 0, 12, $mday, $mon, $year ) + $days * DAY );
103 660 return @date[ 3, 4, 5 ];
104 }
105  
106 # The boundary of $unit (a multiple of $count) at or before $epoch.
107 5437 ▶ sub _floor ( $epoch, $unit, $count, $utc ) {
Subroutine _floor: called CC=7 SCAR=19.5
108 5437 my ( $sec, $min, $hour, $mday, $mon, $year, $wday ) = _parts( $epoch, $utc );
109 5437 ▶ return _epoch( $utc, floor( $sec / $count ) * $count, $min, $hour, $mday, $mon, $year ) if $unit eq 'second';
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq "second" 1692 3745 5437
110 3745 ▶ return _epoch( $utc, 0, floor( $min / $count ) * $count, $hour, $mday, $mon, $year ) if $unit eq 'minute';
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq "minute" 2562 1183 3745
111 1183 ▶ return _epoch( $utc, 0, 0, floor( $hour / $count ) * $count, $mday, $mon, $year ) if $unit eq 'hour';
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq "hour" 1168 15 1183
112 15 ▶ return _epoch( $utc, 0, 0, 0, 1 + floor( ( $mday - 1 ) / $count ) * $count, $mon, $year ) if $unit eq 'day';
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq "day" 4 11 15
113 11 ▶ return _epoch( $utc, 0, 0, 0, _add_days( $mday, $mon, $year, -( ( $wday + 6 ) % 7 ) ) ) if $unit eq 'week';
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq "week" 2 9 11
114 9 ▶ return _epoch( $utc, 0, 0, 0, 1, floor( $mon / $count ) * $count, $year ) if $unit eq 'month';
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq "month" 6 3 9
115 3 return _epoch( $utc, 0, 0, 0, 1, 0, floor( ( $year + 1900 ) / $count ) * $count - 1900 );
116 }
117  
118 # The next boundary after the boundary $epoch.
119 6720 ▶ sub _next ( $epoch, $unit, $count, $utc ) {
Subroutine _next: called CC=13 SCAR=25.7
120 6720 my ( $sec, $min, $hour, $mday, $mon, $year ) = _parts( $epoch, $utc );
121 6720 ▶ if ( $unit eq 'second' || $unit eq 'minute' || $unit eq 'hour' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit eq "second" or $unit eq "minute" or $unit eq "hour") 5367 1353 6720
Condition100%6/6
$unit eq "second" or $unit eq "minute" or $unit eq "hour"
l!l&&r!l&&!r
422111461353
$unit eq "second" or $unit eq "minute"
l!l&&r!l&&!r
168025412499
MC/DC100%3/3
$unit eq "second" or $unit eq "minute" or $unit eq "hour"
$unit eq "second" $unit eq "minute" $unit eq "hour"
Truth table100%4/4
A or B or C
A= $unit eq "second"B= $unit eq "minute"C= $unit eq "hour"
ABCresult
1XX1
01X1
0011
0000
122 5367 ▶ my $step = $count * ( $unit eq 'second' ? 1 : $unit eq 'minute' ? MINUTE : HOUR );
Branch: 2 branches
BranchTrueFalseTotal
$unit eq 'second' ? : 1680 3687 5367
$unit eq 'minute' ? : 2541 1146 3687
123  
124 # Across a clock change the next boundary is a little nearer or
125 # farther; floor finds it.
126 5367 ▶ my $next = _floor( $epoch + $step + ( $unit eq 'hour' ? HOUR / 2 : 0 ), $unit, $count, $utc );
Branch: 1 branch
BranchTrueFalseTotal
$unit eq 'hour' ? : 1146 4221 5367
127 5367 ▶ return $next > $epoch ? $next : _floor( $epoch + 2 * $step, $unit, $count, $utc );
Branch: 1 branch
BranchTrueFalseTotal
$next > $epoch ? : 5367 0 5367
128 }
129 1353 ▶ if ( $unit eq 'day' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit eq "day") 600 753 1353
130 600 my ( $next_mday, $next_mon, $next_year ) = _add_days( $mday, $mon, $year, $count );
131 600 ▶ ( $next_mday, $next_mon, $next_year ) = ( 1, $next_mon, $next_year ) if $next_mon != $mon && $count > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $next_mon != $mon and $count > 1 13 587 600
Condition100%3/3
$next_mon != $mon and $count > 1
!ll&&!rl&&r
5741313
MC/DC100%2/2
$next_mon != $mon and $count > 1
$next_mon != $mon $count > 1
Truth table100%3/3
A and B
A= $next_mon != $monB= $count > 1
ABresult
0X0
100
111
132 600 return _epoch( $utc, 0, 0, 0, $next_mday, $next_mon, $next_year );
133 }
134 753 ▶ return _epoch( $utc, 0, 0, 0, _add_days( $mday, $mon, $year, 7 ) ) if $unit eq 'week';
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq "week" 58 695 753
135 695 ▶ if ( $unit eq 'month' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unit eq "month") 648 47 695
136 648 my $month = $mon + $count;
137 648 return _epoch( $utc, 0, 0, 0, 1, $month % 12, $year + floor( $month / 12 ) );
138 }
139 47 return _epoch( $utc, 0, 0, 0, 1, 0, $year + $count );
140 }
141  
142 70 ▶ sub _ticks ( $low, $high, $unit, $count, $utc ) {
Subroutine _ticks: called CC=3 SCAR=11.0
143 70 my @values;
144 70 my $tick = _floor( $low, $unit, $count, $utc );
145 70 ▶ $tick = _next( $tick, $unit, $count, $utc ) if $tick < $low;
Branch: 1 branch
BranchTrueFalseTotal
if $tick < $low 1 69 70
146 70 ▶ while ( $tick <= $high ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tick <= $high) 6719 70 6789
147 6719 push @values, $tick;
148 6719 $tick = _next( $tick, $unit, $count, $utc );
149 }
150 70 return @values;
151 }
152  
153 # Each tick shows the largest calendar unit it starts that the
154 # interval cares about: the year on January 1, the month on the first
155 # of a month (or the day, when the ticks are days), the date at
156 # midnight (between hours), else the time.
157 6035 ▶ sub _auto_label ( $epoch, $unit, $utc ) {
Subroutine _auto_label: called CC=16 SCAR=27.7
158 6035 ▶ my @parts = $utc ? gmtime $epoch : localtime $epoch;
Branch: 1 branch
BranchTrueFalseTotal
$utc ? : 6035 0 6035
159 6035 my ( $sec, $min, $hour, $mday, $mon ) = @parts;
160 6035 ▶ my $day_label = sub () { strftime( '%b ', @parts ) . $mday };
Subroutine __ANON__: called CC=1 SCAR=0.0
161 6035 ▶ return strftime( '%Y', @parts ) if $unit eq 'year' || ( !$sec && !$min && !$hour && $mday == 1 && !$mon && $unit ne 'second' );
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq "year" or not $sec || $min || $hour and $mday == 1 and not $mon and $unit ne "second" 163 5872 6035
Condition94%17/18
$unit eq "year" or not $sec || $min || $hour and $mday == 1 and not $mon and $unit ne "second"
l!l&&r!l&&!r
471165872
not $sec || $min || $hour and $mday == 1 and not $mon and $unit ne "second"
!ll&&!rl&&r
58720116
not $sec || $min || $hour and $mday == 1 and not $mon
!ll&&!rl&&r
5257615116
not $sec || $min || $hour and $mday == 1
!ll&&!rl&&r
4478779731
$sec || $min || $hour
l!l&&r!l&&!r
322112571510
$sec or $min
l!l&&r!l&&!r
129619252767
MC/DC85%6/7
$unit eq "year" or not $sec || $min || $hour and $mday == 1 and not $mon and $unit ne "second"
$unit eq "year" $sec $min $hour $mday == 1 not $mon $unit ne "second"
Truth table87%7/8
A or not(B or C || D) and E and not(F) and G
A= $unit eq "year"B= $secC= $minD= $hourE= $mday == 1F= not $monG= $unit ne "second"
ABCDEFGresult
1XXXXXX1
00001111
01XXXXX0
001XXXX0
0001XXX0
00000XX0
000010X0
00001100
162 5872 ▶ return strftime( '%b', @parts ) if $unit eq 'month';
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq "month" 536 5336 5872
163 5336 ▶ return $day_label->() if $unit eq 'day' || $unit eq 'week';
Branch: 1 branch
BranchTrueFalseTotal
if $unit eq "day" or $unit eq "week" 653 4683 5336
Condition100%3/3
$unit eq "day" or $unit eq "week"
l!l&&r!l&&!r
596574683
MC/DC100%2/2
$unit eq "day" or $unit eq "week"
$unit eq "day" $unit eq "week"
Truth table100%3/3
A or B
A= $unit eq "day"B= $unit eq "week"
ABresult
1X1
011
000
164 4683 ▶ return strftime( '%H:%M:%S', @parts ) if $sec || $unit eq 'second';
Branch: 1 branch
BranchTrueFalseTotal
if $sec or $unit eq "second" 1680 3003 4683
Condition100%3/3
$sec or $unit eq "second"
l!l&&r!l&&!r
12963843003
MC/DC100%2/2
$sec or $unit eq "second"
$sec $unit eq "second"
Truth table100%3/3
A or B
A= $secB= $unit eq "second"
ABresult
1X1
011
000
165 3003 ▶ return $day_label->() if !$min && !$hour;
Branch: 1 branch
BranchTrueFalseTotal
unless $min or $hour 193 2810 3003
Condition100%3/3
$min or $hour
l!l&&r!l&&!r
15561254193
MC/DC100%2/2
$min or $hour
$min $hour
Truth table100%3/3
A or B
A= $minB= $hour
ABresult
1X1
011
000
166 2810 return strftime( '%H:%M', @parts );
167 }
168 }
169  
170 1 1;
171  
172 __END__
« blib/lib/Term/Fabulous/Chart/Marker.pm ↑ Summary blib/lib/Term/Fabulous/Chart/Format.pm »