| 1 | package DateTime::Set; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 23 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 23 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 23 | ▶ | use Params::Validate qw( validate SCALAR BOOLEAN OBJECT CODEREF ARRAYREF ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 23 | ▶ | use DateTime 0.12; # this is for version checking only | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 23 | ▶ | use DateTime::Duration; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 23 | ▶ | use DateTime::Span; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 23 | ▶ | use Set::Infinite 0.59; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 23 | ▶ | use Set::Infinite::_recurrence; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 23 | ▶ | use vars qw( $VERSION ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 23 | ▶ | use constant INFINITY => 100 ** 100 ** 100 ; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 23 | ▶ | use constant NEG_INFINITY => -1 * (100 ** 100 ** 100); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 23 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 23 | $VERSION = '0.3900'; | |||||||||||||||||||||||||||
| 19 | } | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 7952 | ▶ | sub _fix_datetime { | ||||||||||||||||||||||||||
|
Subroutine
_fix_datetime: called CC=5 SCAR=16.3
| |||||||||||||||||||||||||||||
| 23 | # internal function - | ||||||||||||||||||||||||||||
| 24 | # (not a class method) | ||||||||||||||||||||||||||||
| 25 | # | ||||||||||||||||||||||||||||
| 26 | # checks that the parameter is an object, and | ||||||||||||||||||||||||||||
| 27 | # also protects the object against mutation | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 7952 | ▶ | return $_[0] | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | unless defined $_[0]; # error | ||||||||||||||||||||||||||||
| 31 | 7940 | ▶ | return $_[0]->clone | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | if ref( $_[0] ); # "immutable" datetime | ||||||||||||||||||||||||||||
| 33 | 89 | ▶ | return DateTime::Infinite::Future->new | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | if $_[0] == INFINITY; # Inf | ||||||||||||||||||||||||||||
| 35 | 10 | ▶ | return DateTime::Infinite::Past->new | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | if $_[0] == NEG_INFINITY; # -Inf | ||||||||||||||||||||||||||||
| 37 | 0 | return $_[0]; # error | |||||||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 74 | ▶ | sub _fix_return_datetime { | ||||||||||||||||||||||||||
|
Subroutine
_fix_return_datetime: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 41 | 74 | my ( $dt, $dt_arg ) = @_; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # internal function - | ||||||||||||||||||||||||||||
| 44 | # (not a class method) | ||||||||||||||||||||||||||||
| 45 | # | ||||||||||||||||||||||||||||
| 46 | # checks that the returned datetime has the same | ||||||||||||||||||||||||||||
| 47 | # time zone as the parameter | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | # TODO: set locale | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 74 | ▶ | return unless $dt; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 61 | ▶ | return unless $dt_arg; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 61 | ▶ | if ( $dt_arg->can('time_zone_long_name') && | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$dt_arg->can("time_zone_long_name") and not $dt_arg->time_zone_long_name eq "floating"
MC/DC50%1/2
$dt_arg->can("time_zone_long_name") and not $dt_arg->time_zone_long_name eq "floating"
$dt_arg->can("time_zone_long_name")
not $dt_arg->time_zone_long_name eq "floating"
Truth table66%2/3
A and not(B)
A= $dt_arg->can("time_zone_long_name")B= not $dt_arg->time_zone_long_name eq "floating"
| |||||||||||||||||||||||||||||
| 54 | !( $dt_arg->time_zone_long_name eq 'floating' ) ) | ||||||||||||||||||||||||||||
| 55 | { | ||||||||||||||||||||||||||||
| 56 | 2 | $dt->set_time_zone( $dt_arg->time_zone ); | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 61 | return $dt; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 0 | ▶ | sub iterate { | ||||||||||||||||||||||||||
|
Subroutine
iterate: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 62 | # deprecated method - use map() or grep() instead | ||||||||||||||||||||||||||||
| 63 | 0 | my ( $self, $callback ) = @_; | |||||||||||||||||||||||||||
| 64 | 0 | my $class = ref( $self ); | |||||||||||||||||||||||||||
| 65 | 0 | my $return = $class->empty_set; | |||||||||||||||||||||||||||
| 66 | $return->{set} = $self->{set}->iterate( | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 68 | 0 | my $min = $_[0]->min; | |||||||||||||||||||||||||||
| 69 | 0 | ▶ | $callback->( $min->clone ) if ref($min); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | 0 | ); | |||||||||||||||||||||||||||
| 72 | 0 | $return; | |||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 1 | ▶ | sub map { | ||||||||||||||||||||||||||
|
Subroutine
map: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | 1 | my ( $self, $callback ) = @_; | |||||||||||||||||||||||||||
| 77 | 1 | my $class = ref( $self ); | |||||||||||||||||||||||||||
| 78 | 1 | ▶ | die "The callback parameter to map() must be a subroutine reference" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | unless ref( $callback ) eq 'CODE'; | ||||||||||||||||||||||||||||
| 80 | 1 | my $return = $class->empty_set; | |||||||||||||||||||||||||||
| 81 | $return->{set} = $self->{set}->iterate( | ||||||||||||||||||||||||||||
| 82 | 2 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 83 | 2 | local $_ = $_[0]->min; | |||||||||||||||||||||||||||
| 84 | 2 | ▶ | next unless ref( $_ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 85 | 2 | $_ = $_->clone; | |||||||||||||||||||||||||||
| 86 | 2 | my @list = $callback->(); | |||||||||||||||||||||||||||
| 87 | 2 | my $set = Set::Infinite::_recurrence->new(); | |||||||||||||||||||||||||||
| 88 | 2 | $set = $set->union( $_ ) for @list; | |||||||||||||||||||||||||||
| 89 | 2 | return $set; | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | 1 | ); | |||||||||||||||||||||||||||
| 92 | 1 | $return; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1 | ▶ | sub grep { | ||||||||||||||||||||||||||
|
Subroutine
grep: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 1 | my ( $self, $callback ) = @_; | |||||||||||||||||||||||||||
| 97 | 1 | my $class = ref( $self ); | |||||||||||||||||||||||||||
| 98 | 1 | ▶ | die "The callback parameter to grep() must be a subroutine reference" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 99 | unless ref( $callback ) eq 'CODE'; | ||||||||||||||||||||||||||||
| 100 | 1 | my $return = $class->empty_set; | |||||||||||||||||||||||||||
| 101 | $return->{set} = $self->{set}->iterate( | ||||||||||||||||||||||||||||
| 102 | 2 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 103 | 2 | local $_ = $_[0]->min; | |||||||||||||||||||||||||||
| 104 | 2 | ▶ | next unless ref( $_ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 2 | $_ = $_->clone; | |||||||||||||||||||||||||||
| 106 | 2 | my $result = $callback->(); | |||||||||||||||||||||||||||
| 107 | 2 | ▶ | return $_ if $result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 108 | 1 | return; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 1 | ); | |||||||||||||||||||||||||||
| 111 | 1 | $return; | |||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 8 | ▶ | sub add { return shift->add_duration( DateTime::Duration->new(@_) ) } | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 1 | ▶ | sub subtract { return shift->subtract_duration( DateTime::Duration->new(@_) ) } | ||||||||||||||||||||||||||
|
Subroutine
subtract: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 3 | ▶ | sub subtract_duration { return $_[0]->add_duration( $_[1]->inverse ) } | ||||||||||||||||||||||||||
|
Subroutine
subtract_duration: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 18 | ▶ | sub add_duration { | ||||||||||||||||||||||||||
|
Subroutine
add_duration: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 121 | 18 | my ( $self, $dur ) = @_; | |||||||||||||||||||||||||||
| 122 | 18 | $dur = $dur->clone; # $dur must be "immutable" | |||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | $self->{set} = $self->{set}->iterate( | ||||||||||||||||||||||||||||
| 125 | 74 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 126 | 74 | my $min = $_[0]->min; | |||||||||||||||||||||||||||
| 127 | 74 | ▶ | $min->clone->add_duration( $dur ) if ref($min); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | }, | ||||||||||||||||||||||||||||
| 129 | 18 | ▶ | backtrack_callback => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 130 | 18 | my ( $min, $max ) = ( $_[0]->min, $_[0]->max ); | |||||||||||||||||||||||||||
| 131 | 18 | ▶ | if ( ref($min) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | { | ||||||||||||||||||||||||||||
| 133 | 14 | $min = $min->clone; | |||||||||||||||||||||||||||
| 134 | 14 | $min->subtract_duration( $dur ); | |||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | 18 | ▶ | if ( ref($max) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | { | ||||||||||||||||||||||||||||
| 138 | 8 | $max = $max->clone; | |||||||||||||||||||||||||||
| 139 | 8 | $max->subtract_duration( $dur ); | |||||||||||||||||||||||||||
| 140 | } | ||||||||||||||||||||||||||||
| 141 | 18 | return Set::Infinite::_recurrence->new( $min, $max ); | |||||||||||||||||||||||||||
| 142 | }, | ||||||||||||||||||||||||||||
| 143 | 18 | ); | |||||||||||||||||||||||||||
| 144 | 18 | $self; | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 4 | ▶ | sub set_time_zone { | ||||||||||||||||||||||||||
|
Subroutine
set_time_zone: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | 4 | my ( $self, $tz ) = @_; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | $self->{set} = $self->{set}->iterate( | ||||||||||||||||||||||||||||
| 151 | 43 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 152 | 43 | my $min = $_[0]->min; | |||||||||||||||||||||||||||
| 153 | 43 | ▶ | $min->clone->set_time_zone( $tz ) if ref($min); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | }, | ||||||||||||||||||||||||||||
| 155 | 8 | ▶ | backtrack_callback => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 156 | 8 | my ( $min, $max ) = ( $_[0]->min, $_[0]->max ); | |||||||||||||||||||||||||||
| 157 | 8 | ▶ | if ( ref($min) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | { | ||||||||||||||||||||||||||||
| 159 | 6 | $min = $min->clone; | |||||||||||||||||||||||||||
| 160 | 6 | $min->set_time_zone( $tz ); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | 8 | ▶ | if ( ref($max) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | { | ||||||||||||||||||||||||||||
| 164 | 2 | $max = $max->clone; | |||||||||||||||||||||||||||
| 165 | 2 | $max->set_time_zone( $tz ); | |||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||
| 167 | 8 | return Set::Infinite::_recurrence->new( $min, $max ); | |||||||||||||||||||||||||||
| 168 | }, | ||||||||||||||||||||||||||||
| 169 | 4 | ); | |||||||||||||||||||||||||||
| 170 | 4 | $self; | |||||||||||||||||||||||||||
| 171 | } | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 1 | ▶ | sub set { | ||||||||||||||||||||||||||
|
Subroutine
set: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 174 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 175 | 1 | my %args = validate( @_, | |||||||||||||||||||||||||||
| 176 | { locale => { type => SCALAR | OBJECT, | ||||||||||||||||||||||||||||
| 177 | default => undef }, | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | ); | ||||||||||||||||||||||||||||
| 180 | $self->{set} = $self->{set}->iterate( | ||||||||||||||||||||||||||||
| 181 | 2 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.2
| |||||||||||||||||||||||||||||
| 182 | 2 | my $min = $_[0]->min; | |||||||||||||||||||||||||||
| 183 | 2 | ▶ | $min->clone->set( %args ) if ref($min); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 184 | }, | ||||||||||||||||||||||||||||
| 185 | 1 | ); | |||||||||||||||||||||||||||
| 186 | 1 | $self; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 37 | ▶ | sub from_recurrence { | ||||||||||||||||||||||||||
|
Subroutine
from_recurrence: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | 37 | my $class = shift; | |||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 37 | my %args = @_; | |||||||||||||||||||||||||||
| 193 | 37 | my %param; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | # Parameter renaming, such that we can use either | ||||||||||||||||||||||||||||
| 196 | # recurrence => xxx or next => xxx, previous => xxx | ||||||||||||||||||||||||||||
| 197 | 37 | ▶ | $param{next} = delete $args{recurrence} || delete $args{next}; | ||||||||||||||||||||||||||
|
Condition100%3/3
delete $args{'recurrence'} || delete $args{'next'}
MC/DC100%2/2
delete $args{'recurrence'} || delete $args{'next'}
delete $args{'recurrence'}
delete $args{'next'}
Truth table100%3/3
A || B
A= delete $args{'recurrence'}B= delete $args{'next'}
| |||||||||||||||||||||||||||||
| 198 | 37 | $param{previous} = delete $args{previous}; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 37 | $param{span} = delete $args{span}; | |||||||||||||||||||||||||||
| 201 | # they might be specifying a span using start / end | ||||||||||||||||||||||||||||
| 202 | 37 | ▶ | $param{span} = DateTime::Span->new( %args ) if keys %args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | 37 | my $self = {}; | |||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | die "Not enough arguments in from_recurrence()" | ||||||||||||||||||||||||||||
| 207 | 37 | ▶ | unless $param{next} || $param{previous}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$param{"next"} or $param{"previous"}
MC/DC0%0/2
$param{"next"} or $param{"previous"}
$param{"next"}
$param{"previous"}
Truth table66%2/3
A or B
A= $param{"next"}B= $param{"previous"}
| |||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 37 | ▶ | if ( ! $param{previous} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | { | ||||||||||||||||||||||||||||
| 211 | 30 | my $data = {}; | |||||||||||||||||||||||||||
| 212 | $param{previous} = | ||||||||||||||||||||||||||||
| 213 | 1635 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 214 | 1635 | _callback_previous ( _fix_datetime( $_[0] ), $param{next}, $data ); | |||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||
| 216 | 30 | } | |||||||||||||||||||||||||||
| 217 | else | ||||||||||||||||||||||||||||
| 218 | { | ||||||||||||||||||||||||||||
| 219 | 7 | my $previous = $param{previous}; | |||||||||||||||||||||||||||
| 220 | $param{previous} = | ||||||||||||||||||||||||||||
| 221 | 28 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 222 | 28 | $previous->( _fix_datetime( $_[0] ) ); | |||||||||||||||||||||||||||
| 223 | } | ||||||||||||||||||||||||||||
| 224 | 7 | } | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 37 | ▶ | if ( ! $param{next} ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | { | ||||||||||||||||||||||||||||
| 228 | 2 | my $data = {}; | |||||||||||||||||||||||||||
| 229 | $param{next} = | ||||||||||||||||||||||||||||
| 230 | 4 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 231 | 4 | _callback_next ( _fix_datetime( $_[0] ), $param{previous}, $data ); | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | 2 | } | |||||||||||||||||||||||||||
| 234 | else | ||||||||||||||||||||||||||||
| 235 | { | ||||||||||||||||||||||||||||
| 236 | 35 | my $next = $param{next}; | |||||||||||||||||||||||||||
| 237 | $param{next} = | ||||||||||||||||||||||||||||
| 238 | 6055 | ▶ | sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 239 | 6055 | $next->( _fix_datetime( $_[0] ) ); | |||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | 35 | } | |||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 37 | my ( $min, $max ); | |||||||||||||||||||||||||||
| 244 | 37 | $max = $param{previous}->( DateTime::Infinite::Future->new ); | |||||||||||||||||||||||||||
| 245 | 37 | $min = $param{next}->( DateTime::Infinite::Past->new ); | |||||||||||||||||||||||||||
| 246 | 37 | ▶ | $max = INFINITY if $max->is_infinite; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | 37 | ▶ | $min = NEG_INFINITY if $min->is_infinite; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | 37 | my $base_set = Set::Infinite::_recurrence->new( $min, $max ); | |||||||||||||||||||||||||||
| 250 | $base_set = $base_set->intersection( $param{span}->{set} ) | ||||||||||||||||||||||||||||
| 251 | 37 | ▶ | if $param{span}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | # warn "base set is $base_set\n"; | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 37 | my $data = {}; | |||||||||||||||||||||||||||
| 256 | $self->{set} = | ||||||||||||||||||||||||||||
| 257 | $base_set->_recurrence( | ||||||||||||||||||||||||||||
| 258 | $param{next}, | ||||||||||||||||||||||||||||
| 259 | $param{previous}, | ||||||||||||||||||||||||||||
| 260 | 37 | $data, | |||||||||||||||||||||||||||
| 261 | ); | ||||||||||||||||||||||||||||
| 262 | 37 | bless $self, $class; | |||||||||||||||||||||||||||
| 263 | |||||||||||||||||||||||||||||
| 264 | 37 | return $self; | |||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||
| 267 | 83 | ▶ | sub from_datetimes { | ||||||||||||||||||||||||||
|
Subroutine
from_datetimes: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 268 | 83 | my $class = shift; | |||||||||||||||||||||||||||
| 269 | 83 | my %args = validate( @_, | |||||||||||||||||||||||||||
| 270 | { dates => | ||||||||||||||||||||||||||||
| 271 | { type => ARRAYREF, | ||||||||||||||||||||||||||||
| 272 | }, | ||||||||||||||||||||||||||||
| 273 | } | ||||||||||||||||||||||||||||
| 274 | ); | ||||||||||||||||||||||||||||
| 275 | 82 | my $self = {}; | |||||||||||||||||||||||||||
| 276 | 82 | $self->{set} = Set::Infinite::_recurrence->new; | |||||||||||||||||||||||||||
| 277 | # possible optimization: sort datetimes and use "push" | ||||||||||||||||||||||||||||
| 278 | 82 | for( @{ $args{dates} } ) | |||||||||||||||||||||||||||
| 279 | { | ||||||||||||||||||||||||||||
| 280 | # DateTime::Infinite objects are not welcome here, | ||||||||||||||||||||||||||||
| 281 | # but this is not enforced (it does't hurt) | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 113 | ▶ | carp "The 'dates' argument to from_datetimes() must only contain ". | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | "datetime objects" | ||||||||||||||||||||||||||||
| 285 | unless UNIVERSAL::can( $_, 'utc_rd_values' ); | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 113 | $self->{set} = $self->{set}->union( $_->clone ); | |||||||||||||||||||||||||||
| 288 | } | ||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||
| 290 | 82 | bless $self, $class; | |||||||||||||||||||||||||||
| 291 | 82 | return $self; | |||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||
| 294 | 79 | ▶ | sub empty_set { | ||||||||||||||||||||||||||
|
Subroutine
empty_set: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 295 | 79 | my $class = shift; | |||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||
| 297 | 79 | return bless { set => Set::Infinite::_recurrence->new }, $class; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | 2 | ▶ | sub is_empty_set { | ||||||||||||||||||||||||||
|
Subroutine
is_empty_set: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 301 | 2 | my $set = $_[0]; | |||||||||||||||||||||||||||
| 302 | 2 | $set->{set}->is_null; | |||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | 54 | ▶ | sub clone { | ||||||||||||||||||||||||||
|
Subroutine
clone: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 306 | 54 | my $self = bless { %{ $_[0] } }, ref $_[0]; | |||||||||||||||||||||||||||
| 307 | 54 | $self->{set} = $_[0]->{set}->copy; | |||||||||||||||||||||||||||
| 308 | 54 | return $self; | |||||||||||||||||||||||||||
| 309 | } | ||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||
| 311 | # default callback that returns the | ||||||||||||||||||||||||||||
| 312 | # "previous" value in a callback recurrence. | ||||||||||||||||||||||||||||
| 313 | # | ||||||||||||||||||||||||||||
| 314 | # This is used to simulate a 'previous' callback, | ||||||||||||||||||||||||||||
| 315 | # when then 'previous' argument in 'from_recurrence' is missing. | ||||||||||||||||||||||||||||
| 316 | # | ||||||||||||||||||||||||||||
| 317 | 1635 | ▶ | sub _callback_previous { | ||||||||||||||||||||||||||
|
Subroutine
_callback_previous: called CC=5 SCAR=16.2
| |||||||||||||||||||||||||||||
| 318 | 1635 | my ($value, $callback_next, $callback_info) = @_; | |||||||||||||||||||||||||||
| 319 | 1635 | my $previous = $value->clone; | |||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||
| 321 | 1635 | ▶ | return $value if $value->is_infinite; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 1433 | my $freq = $callback_info->{freq}; | |||||||||||||||||||||||||||
| 324 | 1433 | ▶ | unless (defined $freq) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | { | ||||||||||||||||||||||||||||
| 326 | # This is called just once, to setup the recurrence frequency | ||||||||||||||||||||||||||||
| 327 | 28 | my $previous = $callback_next->( $value ); | |||||||||||||||||||||||||||
| 328 | 28 | my $next = $callback_next->( $previous ); | |||||||||||||||||||||||||||
| 329 | 28 | $freq = 2 * ( $previous - $next ); | |||||||||||||||||||||||||||
| 330 | # save it for future use with this same recurrence | ||||||||||||||||||||||||||||
| 331 | 28 | $callback_info->{freq} = $freq; | |||||||||||||||||||||||||||
| 332 | } | ||||||||||||||||||||||||||||
| 333 | |||||||||||||||||||||||||||||
| 334 | 1433 | $previous->add_duration( $freq ); | |||||||||||||||||||||||||||
| 335 | 1433 | $previous = $callback_next->( $previous ); | |||||||||||||||||||||||||||
| 336 | 1433 | ▶ | if ($previous >= $value) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | { | ||||||||||||||||||||||||||||
| 338 | # This error happens if the event frequency oscillates widely | ||||||||||||||||||||||||||||
| 339 | # (more than 100% of difference from one interval to next) | ||||||||||||||||||||||||||||
| 340 | 0 | my @freq = $freq->deltas; | |||||||||||||||||||||||||||
| 341 | 0 | print STDERR "_callback_previous: Delta components are: @freq\n"; | |||||||||||||||||||||||||||
| 342 | 0 | warn "_callback_previous: iterator can't find a previous value, got ". | |||||||||||||||||||||||||||
| 343 | $previous->ymd." after ".$value->ymd; | ||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | 1433 | my $previous1; | |||||||||||||||||||||||||||
| 346 | 1433 | while (1) | |||||||||||||||||||||||||||
| 347 | { | ||||||||||||||||||||||||||||
| 348 | 1445 | $previous1 = $previous->clone; | |||||||||||||||||||||||||||
| 349 | 1445 | $previous = $callback_next->( $previous ); | |||||||||||||||||||||||||||
| 350 | 1445 | ▶ | return $previous1 if $previous >= $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | # default callback that returns the | ||||||||||||||||||||||||||||
| 355 | # "next" value in a callback recurrence. | ||||||||||||||||||||||||||||
| 356 | # | ||||||||||||||||||||||||||||
| 357 | # This is used to simulate a 'next' callback, | ||||||||||||||||||||||||||||
| 358 | # when then 'next' argument in 'from_recurrence' is missing. | ||||||||||||||||||||||||||||
| 359 | # | ||||||||||||||||||||||||||||
| 360 | 4 | ▶ | sub _callback_next { | ||||||||||||||||||||||||||
|
Subroutine
_callback_next: called CC=5 SCAR=16.4
| |||||||||||||||||||||||||||||
| 361 | 4 | my ($value, $callback_previous, $callback_info) = @_; | |||||||||||||||||||||||||||
| 362 | 4 | my $next = $value->clone; | |||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 4 | ▶ | return $value if $value->is_infinite; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 2 | my $freq = $callback_info->{freq}; | |||||||||||||||||||||||||||
| 367 | 2 | ▶ | unless (defined $freq) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 368 | { | ||||||||||||||||||||||||||||
| 369 | # This is called just once, to setup the recurrence frequency | ||||||||||||||||||||||||||||
| 370 | 1 | my $next = $callback_previous->( $value ); | |||||||||||||||||||||||||||
| 371 | 1 | my $previous = $callback_previous->( $next ); | |||||||||||||||||||||||||||
| 372 | 1 | $freq = 2 * ( $next - $previous ); | |||||||||||||||||||||||||||
| 373 | # save it for future use with this same recurrence | ||||||||||||||||||||||||||||
| 374 | 1 | $callback_info->{freq} = $freq; | |||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | 2 | $next->add_duration( $freq ); | |||||||||||||||||||||||||||
| 378 | 2 | $next = $callback_previous->( $next ); | |||||||||||||||||||||||||||
| 379 | 2 | ▶ | if ($next <= $value) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | { | ||||||||||||||||||||||||||||
| 381 | # This error happens if the event frequency oscillates widely | ||||||||||||||||||||||||||||
| 382 | # (more than 100% of difference from one interval to next) | ||||||||||||||||||||||||||||
| 383 | 0 | my @freq = $freq->deltas; | |||||||||||||||||||||||||||
| 384 | 0 | print STDERR "_callback_next: Delta components are: @freq\n"; | |||||||||||||||||||||||||||
| 385 | 0 | warn "_callback_next: iterator can't find a previous value, got ". | |||||||||||||||||||||||||||
| 386 | $next->ymd." before ".$value->ymd; | ||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||
| 388 | 2 | my $next1; | |||||||||||||||||||||||||||
| 389 | 2 | while (1) | |||||||||||||||||||||||||||
| 390 | { | ||||||||||||||||||||||||||||
| 391 | 2 | $next1 = $next->clone; | |||||||||||||||||||||||||||
| 392 | 2 | $next = $callback_previous->( $next ); | |||||||||||||||||||||||||||
| 393 | 2 | ▶ | return $next1 if $next >= $value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 40 | ▶ | sub iterator { | ||||||||||||||||||||||||||
|
Subroutine
iterator: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 398 | 40 | my $self = shift; | |||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 40 | my %args = @_; | |||||||||||||||||||||||||||
| 401 | 40 | my $span; | |||||||||||||||||||||||||||
| 402 | 40 | $span = delete $args{span}; | |||||||||||||||||||||||||||
| 403 | 40 | ▶ | $span = DateTime::Span->new( %args ) if %args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 40 | ▶ | return $self->intersection( $span ) if $span; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 406 | 25 | return $self->clone; | |||||||||||||||||||||||||||
| 407 | } | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | # next() gets the next element from an iterator() | ||||||||||||||||||||||||||||
| 411 | # next( $dt ) returns the next element after a datetime. | ||||||||||||||||||||||||||||
| 412 | 1367 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 413 | 1367 | my $self = shift; | |||||||||||||||||||||||||||
| 414 | 1367 | ▶ | return undef unless ref( $self->{set} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 1355 | ▶ | if ( @_ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 417 | { | ||||||||||||||||||||||||||||
| 418 | 39 | ▶ | if ( $self->{set}->_is_recurrence ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | { | ||||||||||||||||||||||||||||
| 420 | return _fix_return_datetime( | ||||||||||||||||||||||||||||
| 421 | 20 | $self->{set}->{param}[0]->( $_[0] ), $_[0] ); | |||||||||||||||||||||||||||
| 422 | } | ||||||||||||||||||||||||||||
| 423 | else | ||||||||||||||||||||||||||||
| 424 | { | ||||||||||||||||||||||||||||
| 425 | 19 | my $span = DateTime::Span->from_datetimes( after => $_[0] ); | |||||||||||||||||||||||||||
| 426 | 19 | return _fix_return_datetime( | |||||||||||||||||||||||||||
| 427 | $self->intersection( $span )->next, $_[0] ); | ||||||||||||||||||||||||||||
| 428 | } | ||||||||||||||||||||||||||||
| 429 | } | ||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||
| 431 | 1316 | my ($head, $tail) = $self->{set}->first; | |||||||||||||||||||||||||||
| 432 | 1316 | $self->{set} = $tail; | |||||||||||||||||||||||||||
| 433 | 1316 | ▶ | return $head->min if defined $head; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 434 | 3 | return $head; | |||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | # previous() gets the last element from an iterator() | ||||||||||||||||||||||||||||
| 438 | # previous( $dt ) returns the previous element before a datetime. | ||||||||||||||||||||||||||||
| 439 | 126 | ▶ | sub previous { | ||||||||||||||||||||||||||
|
Subroutine
previous: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||
| 440 | 126 | my $self = shift; | |||||||||||||||||||||||||||
| 441 | 126 | ▶ | return undef unless ref( $self->{set} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | 111 | ▶ | if ( @_ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 444 | { | ||||||||||||||||||||||||||||
| 445 | 35 | ▶ | if ( $self->{set}->_is_recurrence ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 446 | { | ||||||||||||||||||||||||||||
| 447 | return _fix_return_datetime( | ||||||||||||||||||||||||||||
| 448 | 13 | $self->{set}->{param}[1]->( $_[0] ), $_[0] ); | |||||||||||||||||||||||||||
| 449 | } | ||||||||||||||||||||||||||||
| 450 | else | ||||||||||||||||||||||||||||
| 451 | { | ||||||||||||||||||||||||||||
| 452 | 22 | my $span = DateTime::Span->from_datetimes( before => $_[0] ); | |||||||||||||||||||||||||||
| 453 | 22 | return _fix_return_datetime( | |||||||||||||||||||||||||||
| 454 | $self->intersection( $span )->previous, $_[0] ); | ||||||||||||||||||||||||||||
| 455 | } | ||||||||||||||||||||||||||||
| 456 | } | ||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | 76 | my ($head, $tail) = $self->{set}->last; | |||||||||||||||||||||||||||
| 459 | 76 | $self->{set} = $tail; | |||||||||||||||||||||||||||
| 460 | 76 | ▶ | return $head->max if defined $head; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 461 | 10 | return $head; | |||||||||||||||||||||||||||
| 462 | } | ||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | # "current" means less-or-equal to a datetime | ||||||||||||||||||||||||||||
| 465 | 29 | ▶ | sub current { | ||||||||||||||||||||||||||
|
Subroutine
current: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 466 | 29 | my $self = shift; | |||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | 29 | ▶ | return undef unless ref( $self->{set} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||
| 470 | 29 | ▶ | if ( $self->{set}->_is_recurrence ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 471 | { | ||||||||||||||||||||||||||||
| 472 | 9 | my $tmp = $self->next( $_[0] ); | |||||||||||||||||||||||||||
| 473 | 9 | return $self->previous( $tmp ); | |||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | 20 | ▶ | return $_[0] if $self->contains( $_[0] ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 477 | 20 | $self->previous( $_[0] ); | |||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 0 | ▶ | sub closest { | ||||||||||||||||||||||||||
|
Subroutine
closest: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 481 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 482 | # return $_[0] if $self->contains( $_[0] ); | ||||||||||||||||||||||||||||
| 483 | 0 | my $dt1 = $self->current( $_[0] ); | |||||||||||||||||||||||||||
| 484 | 0 | my $dt2 = $self->next( $_[0] ); | |||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | 0 | ▶ | return $dt2 unless defined $dt1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 487 | 0 | ▶ | return $dt1 unless defined $dt2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | 0 | my $delta = $_[0] - $dt1; | |||||||||||||||||||||||||||
| 490 | 0 | ▶ | return $dt1 if ( $dt2 - $delta ) >= $_[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | 0 | return $dt2; | |||||||||||||||||||||||||||
| 493 | } | ||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | 4 | ▶ | sub as_list { | ||||||||||||||||||||||||||
|
Subroutine
as_list: called CC=17 SCAR=34.8
Pod
documented
| |||||||||||||||||||||||||||||
| 496 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 497 | 4 | ▶ | return undef unless ref( $self->{set} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 498 | |||||||||||||||||||||||||||||
| 499 | 4 | my %args = @_; | |||||||||||||||||||||||||||
| 500 | 4 | my $span; | |||||||||||||||||||||||||||
| 501 | 4 | $span = delete $args{span}; | |||||||||||||||||||||||||||
| 502 | 4 | ▶ | $span = DateTime::Span->new( %args ) if %args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 503 | |||||||||||||||||||||||||||||
| 504 | 4 | my $set = $self->clone; | |||||||||||||||||||||||||||
| 505 | 4 | ▶ | $set = $set->intersection( $span ) if $span; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 506 | |||||||||||||||||||||||||||||
| 507 | 4 | ▶ | return if $set->{set}->is_null; # nothing = empty | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | # Note: removing this line means we may end up in an infinite loop! | ||||||||||||||||||||||||||||
| 510 | ## return undef if $set->{set}->is_too_complex; # undef = no start/end | ||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | return undef | ||||||||||||||||||||||||||||
| 513 | 3 | ▶ | if $set->max->is_infinite || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$set->max->is_infinite or $set->min->is_infinite
MC/DC50%1/2
$set->max->is_infinite or $set->min->is_infinite
$set->max->is_infinite
$set->min->is_infinite
Truth table66%2/3
A or B
A= $set->max->is_infiniteB= $set->min->is_infinite
| |||||||||||||||||||||||||||||
| 514 | $set->min->is_infinite; | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | 2 | my @result; | |||||||||||||||||||||||||||
| 517 | 2 | my $next = $self->min; | |||||||||||||||||||||||||||
| 518 | 2 | ▶ | if ( $span ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 519 | 0 | my $next1 = $span->min; | |||||||||||||||||||||||||||
| 520 | 0 | ▶ | $next = $next1 if $next1 && $next1 > $next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$next1 and $next1 > $next
MC/DC0%0/2
$next1 and $next1 > $next
$next1
$next1 > $next
Truth table0%0/3
A and B
A= $next1B= $next1 > $next
| |||||||||||||||||||||||||||||
| 521 | 0 | $next = $self->current( $next ); | |||||||||||||||||||||||||||
| 522 | } | ||||||||||||||||||||||||||||
| 523 | 2 | my $last = $self->max; | |||||||||||||||||||||||||||
| 524 | 2 | ▶ | if ( $span ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 525 | 0 | my $last1 = $span->max; | |||||||||||||||||||||||||||
| 526 | 0 | ▶ | $last = $last1 if $last1 && $last1 < $last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$last1 and $last1 < $last
MC/DC0%0/2
$last1 and $last1 < $last
$last1
$last1 < $last
Truth table0%0/3
A and B
A= $last1B= $last1 < $last
| |||||||||||||||||||||||||||||
| 527 | } | ||||||||||||||||||||||||||||
| 528 | 2 | ▶ | do { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$next and $next <= $last
MC/DC50%1/2
$next and $next <= $last
$next
$next <= $last
Truth table66%2/3
A and B
A= $nextB= $next <= $last
| |||||||||||||||||||||||||||||
| 529 | 3 | ▶ | push @result, $next if !$span || $span->contains($next); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $span or $span->contains($next)
MC/DC0%0/2
not $span or $span->contains($next)
not $span
$span->contains($next)
Truth table33%1/3
not(A) or B
A= not $spanB= $span->contains($next)
| |||||||||||||||||||||||||||||
| 530 | 3 | $next = $self->next( $next ); | |||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||
| 532 | while $next && $next <= $last; | ||||||||||||||||||||||||||||
| 533 | 2 | return @result; | |||||||||||||||||||||||||||
| 534 | } | ||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||
| 536 | 65 | ▶ | sub intersection { | ||||||||||||||||||||||||||
|
Subroutine
intersection: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 537 | 65 | my ($set1, $set2) = ( shift, shift ); | |||||||||||||||||||||||||||
| 538 | 65 | my $class = ref($set1); | |||||||||||||||||||||||||||
| 539 | 65 | my $tmp = $class->empty_set(); | |||||||||||||||||||||||||||
| 540 | 65 | ▶ | $set2 = $set2->as_set | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 541 | if $set2->can( 'as_set' ); | ||||||||||||||||||||||||||||
| 542 | 65 | ▶ | $set2 = $class->from_datetimes( dates => [ $set2, @_ ] ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 543 | unless $set2->can( 'union' ); | ||||||||||||||||||||||||||||
| 544 | 65 | $tmp->{set} = $set1->{set}->intersection( $set2->{set} ); | |||||||||||||||||||||||||||
| 545 | 65 | return $tmp; | |||||||||||||||||||||||||||
| 546 | } | ||||||||||||||||||||||||||||
| 547 | |||||||||||||||||||||||||||||
| 548 | 10 | ▶ | sub intersects { | ||||||||||||||||||||||||||
|
Subroutine
intersects: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 549 | 10 | my ($set1, $set2) = ( shift, shift ); | |||||||||||||||||||||||||||
| 550 | 10 | my $class = ref($set1); | |||||||||||||||||||||||||||
| 551 | 10 | ▶ | $set2 = $set2->as_set | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 552 | if $set2->can( 'as_set' ); | ||||||||||||||||||||||||||||
| 553 | 10 | ▶ | unless ( $set2->can( 'union' ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 554 | { | ||||||||||||||||||||||||||||
| 555 | 9 | ▶ | if ( $set1->{set}->_is_recurrence ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 556 | { | ||||||||||||||||||||||||||||
| 557 | 3 | for ( $set2, @_ ) | |||||||||||||||||||||||||||
| 558 | { | ||||||||||||||||||||||||||||
| 559 | 4 | ▶ | return 1 if $set1->current( $_ ) == $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 560 | } | ||||||||||||||||||||||||||||
| 561 | 1 | return 0; | |||||||||||||||||||||||||||
| 562 | } | ||||||||||||||||||||||||||||
| 563 | 6 | $set2 = $class->from_datetimes( dates => [ $set2, @_ ] ) | |||||||||||||||||||||||||||
| 564 | } | ||||||||||||||||||||||||||||
| 565 | 7 | return $set1->{set}->intersects( $set2->{set} ); | |||||||||||||||||||||||||||
| 566 | } | ||||||||||||||||||||||||||||
| 567 | |||||||||||||||||||||||||||||
| 568 | 30 | ▶ | sub contains { | ||||||||||||||||||||||||||
|
Subroutine
contains: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 569 | 30 | my ($set1, $set2) = ( shift, shift ); | |||||||||||||||||||||||||||
| 570 | 30 | my $class = ref($set1); | |||||||||||||||||||||||||||
| 571 | 30 | ▶ | $set2 = $set2->as_set | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 572 | if $set2->can( 'as_set' ); | ||||||||||||||||||||||||||||
| 573 | 30 | ▶ | unless ( $set2->can( 'union' ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 574 | { | ||||||||||||||||||||||||||||
| 575 | 29 | ▶ | if ( $set1->{set}->_is_recurrence ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 576 | { | ||||||||||||||||||||||||||||
| 577 | 3 | for ( $set2, @_ ) | |||||||||||||||||||||||||||
| 578 | { | ||||||||||||||||||||||||||||
| 579 | 3 | ▶ | return 0 unless $set1->current( $_ ) == $_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 580 | } | ||||||||||||||||||||||||||||
| 581 | 1 | return 1; | |||||||||||||||||||||||||||
| 582 | } | ||||||||||||||||||||||||||||
| 583 | 26 | $set2 = $class->from_datetimes( dates => [ $set2, @_ ] ) | |||||||||||||||||||||||||||
| 584 | } | ||||||||||||||||||||||||||||
| 585 | 27 | return $set1->{set}->contains( $set2->{set} ); | |||||||||||||||||||||||||||
| 586 | } | ||||||||||||||||||||||||||||
| 587 | |||||||||||||||||||||||||||||
| 588 | 4 | ▶ | sub union { | ||||||||||||||||||||||||||
|
Subroutine
union: called CC=5 SCAR=17.3
Pod
documented
| |||||||||||||||||||||||||||||
| 589 | 4 | my ($set1, $set2) = ( shift, shift ); | |||||||||||||||||||||||||||
| 590 | 4 | my $class = ref($set1); | |||||||||||||||||||||||||||
| 591 | 4 | my $tmp = $class->empty_set(); | |||||||||||||||||||||||||||
| 592 | 4 | ▶ | $set2 = $set2->as_set | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 593 | if $set2->can( 'as_set' ); | ||||||||||||||||||||||||||||
| 594 | 4 | ▶ | $set2 = $class->from_datetimes( dates => [ $set2, @_ ] ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 595 | unless $set2->can( 'union' ); | ||||||||||||||||||||||||||||
| 596 | 4 | $tmp->{set} = $set1->{set}->union( $set2->{set} ); | |||||||||||||||||||||||||||
| 597 | 4 | ▶ | bless $tmp, 'DateTime::SpanSet' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$set2->isa("DateTime::Span") or $set2->isa("DateTime::SpanSet")
MC/DC0%0/2
$set2->isa("DateTime::Span") or $set2->isa("DateTime::SpanSet")
$set2->isa("DateTime::Span")
$set2->isa("DateTime::SpanSet")
Truth table33%1/3
A or B
A= $set2->isa("DateTime::Span")B= $set2->isa("DateTime::SpanSet")
| |||||||||||||||||||||||||||||
| 598 | if $set2->isa('DateTime::Span') or $set2->isa('DateTime::SpanSet'); | ||||||||||||||||||||||||||||
| 599 | 4 | return $tmp; | |||||||||||||||||||||||||||
| 600 | } | ||||||||||||||||||||||||||||
| 601 | |||||||||||||||||||||||||||||
| 602 | 2 | ▶ | sub complement { | ||||||||||||||||||||||||||
|
Subroutine
complement: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||
| 603 | 2 | my ($set1, $set2) = ( shift, shift ); | |||||||||||||||||||||||||||
| 604 | 2 | my $class = ref($set1); | |||||||||||||||||||||||||||
| 605 | 2 | my $tmp = $class->empty_set(); | |||||||||||||||||||||||||||
| 606 | 2 | ▶ | if (defined $set2) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 607 | { | ||||||||||||||||||||||||||||
| 608 | 2 | ▶ | $set2 = $set2->as_set | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 609 | if $set2->can( 'as_set' ); | ||||||||||||||||||||||||||||
| 610 | 2 | ▶ | $set2 = $class->from_datetimes( dates => [ $set2, @_ ] ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 611 | unless $set2->can( 'union' ); | ||||||||||||||||||||||||||||
| 612 | # TODO: "compose complement"; | ||||||||||||||||||||||||||||
| 613 | 2 | $tmp->{set} = $set1->{set}->complement( $set2->{set} ); | |||||||||||||||||||||||||||
| 614 | } | ||||||||||||||||||||||||||||
| 615 | else | ||||||||||||||||||||||||||||
| 616 | { | ||||||||||||||||||||||||||||
| 617 | 0 | $tmp->{set} = $set1->{set}->complement; | |||||||||||||||||||||||||||
| 618 | 0 | bless $tmp, 'DateTime::SpanSet'; | |||||||||||||||||||||||||||
| 619 | } | ||||||||||||||||||||||||||||
| 620 | 2 | return $tmp; | |||||||||||||||||||||||||||
| 621 | } | ||||||||||||||||||||||||||||
| 622 | |||||||||||||||||||||||||||||
| 623 | 54 | ▶ | sub start { | ||||||||||||||||||||||||||
|
Subroutine
start: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 624 | 54 | return _fix_datetime( $_[0]->{set}->min ); | |||||||||||||||||||||||||||
| 625 | } | ||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | 23 | *min = \&start; | |||||||||||||||||||||||||||
| 628 | |||||||||||||||||||||||||||||
| 629 | 34 | ▶ | sub end { | ||||||||||||||||||||||||||
|
Subroutine
end: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 630 | 34 | return _fix_datetime( $_[0]->{set}->max ); | |||||||||||||||||||||||||||
| 631 | } | ||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | 23 | *max = \&end; | |||||||||||||||||||||||||||
| 634 | |||||||||||||||||||||||||||||
| 635 | # returns a DateTime::Span | ||||||||||||||||||||||||||||
| 636 | 0 | ▶ | sub span { | ||||||||||||||||||||||||||
|
Subroutine
span: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 637 | 0 | my $set = $_[0]->{set}->span; | |||||||||||||||||||||||||||
| 638 | 0 | my $self = bless { set => $set }, 'DateTime::Span'; | |||||||||||||||||||||||||||
| 639 | 0 | return $self; | |||||||||||||||||||||||||||
| 640 | } | ||||||||||||||||||||||||||||
| 641 | |||||||||||||||||||||||||||||
| 642 | 9 | ▶ | sub count { | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 643 | 9 | my ($self) = shift; | |||||||||||||||||||||||||||
| 644 | 9 | ▶ | return undef unless ref( $self->{set} ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||
| 646 | 9 | my %args = @_; | |||||||||||||||||||||||||||
| 647 | 9 | my $span; | |||||||||||||||||||||||||||
| 648 | 9 | $span = delete $args{span}; | |||||||||||||||||||||||||||
| 649 | 9 | ▶ | $span = DateTime::Span->new( %args ) if %args; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||
| 651 | 9 | my $set = $self->clone; | |||||||||||||||||||||||||||
| 652 | 9 | ▶ | $set = $set->intersection( $span ) if $span; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 653 | |||||||||||||||||||||||||||||
| 654 | return $set->{set}->count | ||||||||||||||||||||||||||||
| 655 | 9 | ▶ | unless $set->{set}->is_too_complex; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | return undef | ||||||||||||||||||||||||||||
| 658 | 4 | ▶ | if $set->max->is_infinite || | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$set->max->is_infinite or $set->min->is_infinite
MC/DC100%2/2
$set->max->is_infinite or $set->min->is_infinite
$set->max->is_infinite
$set->min->is_infinite
Truth table100%3/3
A or B
A= $set->max->is_infiniteB= $set->min->is_infinite
| |||||||||||||||||||||||||||||
| 659 | $set->min->is_infinite; | ||||||||||||||||||||||||||||
| 660 | |||||||||||||||||||||||||||||
| 661 | 1 | my $count = 0; | |||||||||||||||||||||||||||
| 662 | 1 | my $iter = $set->iterator; | |||||||||||||||||||||||||||
| 663 | 1 | ▶ | $count++ while $iter->next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 664 | 1 | return $count; | |||||||||||||||||||||||||||
| 665 | } | ||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||
| 667 | 23 | 1; | |||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||
| 669 | __END__ | ||||||||||||||||||||||||||||