| 1 | package DateTimeX::Duration::SkipDays; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # # no critic qw( Modules::RequireExplicitInclusion ) | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | # ABSTRACT: Given a starting date, a number of days and a list of days to be skipped, returns the date X number of days away. | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 4 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 4 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | 4 | ▶ | use DateTime; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use DateTime::Event::Holiday::US; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 4 | ▶ | use DateTime::Format::Flexible; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use Try::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use List::MoreUtils 'any'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | # The circular::require test is having problems with one or more of the above modules, so we'll skip it. | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | ## efm skip circular | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 4 | our $VERSION = '0.002'; # VERSION | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 15 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 15 | my ( $class, $arg ) = @_; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 15 | ▶ | croak 'Must pass nothing or a reference to a hash to new' | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
ref $arg and ref $arg ne "HASH"
MC/DC100%2/2
ref $arg and ref $arg ne "HASH"
ref $arg
ref $arg ne "HASH"
Truth table100%3/3
A and B
A= ref $argB= ref $arg ne "HASH"
| |||||||||||||||||||||||||||||
| 30 | if ref $arg && ref $arg ne 'HASH'; | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 12 | my $self = bless {}, $class; | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 12 | $self->{ 'bad_format' } = {}; | |||||||||||||||||||||||||||
| 35 | 12 | $self->{ 'days_to_skip' } = DateTime::Set->empty_set; | |||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | 12 | for my $key ( keys %$arg ) { ## no critic qw( References::ProhibitDoubleSigils ) | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 11 | ▶ | next if $key eq 'add'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 10 | ▶ | if ( my $method = $self->can( $key ) ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | ## no critic qw( ValuesAndExpressions::ProhibitAccessOfPrivateData ) | ||||||||||||||||||||||||||||
| 44 | 9 | $self->$method( $arg->{ $key } ); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 12 | return $self; | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | } ## end sub new | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 6 | ▶ | sub start_date { | ||||||||||||||||||||||||||
|
Subroutine
start_date: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 6 | my ( $self, $start_date ) = @_; | |||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||
| 58 | 6 | ▶ | croak 'Must pass a DateTime object to start' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | if ref $start_date ne 'DateTime'; | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 5 | $self->{ 'start_date' } = $start_date->clone->truncate( 'to' => 'day' ); | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 5 | return 1; | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 13 | ▶ | sub days_to_skip { | ||||||||||||||||||||||||||
|
Subroutine
days_to_skip: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 13 | my ( $self, @days_to_skip ) = @_; | |||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 13 | $self->{ 'days_to_skip' } = $self->{ 'days_to_skip' }->union( $_ ) for @days_to_skip; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 13 | return 1; | |||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 9 | ▶ | sub parse_dates { | ||||||||||||||||||||||||||
|
Subroutine
parse_dates: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 9 | my ( $self, $skip_dates ) = @_; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 9 | ▶ | croak 'Expected scalar' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | if ref $skip_dates; | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 8 | my @known_holidays = DateTime::Event::Holiday::US::known(); | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | 8 | for my $line ( split /\n/, $skip_dates ) { | |||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | 27 | ▶ | next if $line =~ /^\s*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 20 | $line =~ s/^\s*(\S.*?)\s*$/$1/; | |||||||||||||||||||||||||||
| 92 | 20 | $line =~ s/\s+/ /g; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 20 | my $dt; | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 20 | ▶ | if ( $line =~ /^RRULE:/i ) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 3 | $dt = DateTime::Format::ICal->parse_recurrence( 'recurrence' => $line ); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | ## no tidy | ||||||||||||||||||||||||||||
| 101 | 612 | ▶ | } elsif ( any { /$line/ } @known_holidays ) { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 102 | ## use tidy | ||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 5 | $dt = DateTime::Event::Holiday::US::holiday( $line ); | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | } else { | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | ## no critic qw( TestingAndDebugging::ProhibitNoWarnings ErrorHandling::RequireCheckingReturnValueOfEval ) | ||||||||||||||||||||||||||||
| 109 | 4 | ▶ | eval { no warnings 'uninitialized'; $dt = DateTime::Format::Flexible->parse_datetime( $line ) }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 110 | ## use critic | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 12 | ▶ | if ( $@ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 7 | ( my $err = $@ ) =~ s/^(Invalid date format: $line).*$/$1/ms; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | 7 | $self->{ 'bad_format' }{ $line } = $err; | |||||||||||||||||||||||||||
| 117 | 7 | next; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # This fails with | ||||||||||||||||||||||||||||
| 122 | # Can't call method "can" on an undefined value at /usr/local/share/perl/5.10.1/DateTime/Set.pm line 593. | ||||||||||||||||||||||||||||
| 123 | # and I don't have time to figure out what's wrong. So, the above is going to have to do. | ||||||||||||||||||||||||||||
| 124 | # | ||||||||||||||||||||||||||||
| 125 | #try { | ||||||||||||||||||||||||||||
| 126 | # | ||||||||||||||||||||||||||||
| 127 | # $dt = DateTime::Format::Flexible->parse_datetime( $line ); | ||||||||||||||||||||||||||||
| 128 | # | ||||||||||||||||||||||||||||
| 129 | #} catch { | ||||||||||||||||||||||||||||
| 130 | # | ||||||||||||||||||||||||||||
| 131 | # ( my $err = $_ ) =~ s/^(Invalid date format: $line).*$/$1/ms; | ||||||||||||||||||||||||||||
| 132 | # | ||||||||||||||||||||||||||||
| 133 | # $self->{ 'bad_format' }{ $line } = $err; | ||||||||||||||||||||||||||||
| 134 | # | ||||||||||||||||||||||||||||
| 135 | #} | ||||||||||||||||||||||||||||
| 136 | } ## end else [ if ( $line =~ /^RRULE:/i)] | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 13 | $self->days_to_skip( $dt ); | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | } ## end for my $line ( split...) | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 8 | return 1; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | } ## end sub parse_dates | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 12 | ▶ | sub bad_format { ## no critic qw( Subroutines::RequireArgUnpacking ) | ||||||||||||||||||||||||||
|
Subroutine
bad_format: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 148 | 12 | ▶ | return wantarray ? keys %{ $_[0]->{ 'bad_format' } } : $_[0]->{ 'bad_format' }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 6 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=9 SCAR=22.1
Pod
documented
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 6 | my ( $self, $x ) = @_; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 4 | ▶ | { no warnings 'numeric'; $x += 0 } ## no critic qw( TestingAndDebugging::ProhibitNoWarnings ) | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 6 | ▶ | croak 'Must provide integer larger than or equal to 0' | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 159 | if $x < 0; | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # XXX: Need better error handling here | ||||||||||||||||||||||||||||
| 162 | croak 'No start date provided' | ||||||||||||||||||||||||||||
| 163 | 6 | ▶ | unless exists $self->{ 'start_date' }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | # XXX: Need better error handling here | ||||||||||||||||||||||||||||
| 166 | croak 'No days_to_skip provided' | ||||||||||||||||||||||||||||
| 167 | 6 | ▶ | unless exists $self->{ 'days_to_skip' }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 6 | my $duration = DateTime::Duration->new( 'days' => $x ); | |||||||||||||||||||||||||||
| 170 | 6 | my $span = DateTime::Span->from_datetime_and_duration( 'start' => $self->{ 'start_date' }, 'duration' => $duration ); | |||||||||||||||||||||||||||
| 171 | 6 | my $skipped = $span->intersection( $self->{ 'days_to_skip' } ); | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | 6 | my $count = my $new_count = 0; | |||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||
| 175 | 6 | my $iter = $skipped->iterator; | |||||||||||||||||||||||||||
| 176 | 6 | ▶ | $count++ while $iter->next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 6 | ▶ | while ( $count != $new_count ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 8 | $duration = DateTime::Duration->new( 'days' => $x + $count ); | |||||||||||||||||||||||||||
| 181 | 8 | $span = DateTime::Span->from_datetime_and_duration( 'start' => $self->{ 'start_date' }, 'duration' => $duration ); | |||||||||||||||||||||||||||
| 182 | 8 | $skipped = $span->intersection( $self->{ 'days_to_skip' } ); | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 8 | $iter = $skipped->iterator; | |||||||||||||||||||||||||||
| 185 | 8 | ▶ | my $new_count; $new_count++ while $iter->next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | 8 | ▶ | last if $new_count == $count; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 188 | 4 | $count = $new_count; | |||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | ## no critic qw( ValuesAndExpressions::ProhibitCommaSeparatedStatements ) | ||||||||||||||||||||||||||||
| 193 | 6 | ▶ | return wantarray ? ( $span, $skipped ) : { 'span' => $span, 'skipped' => $skipped }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | } ## end sub add | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 4 | 1; | |||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | __END__ | ||||||||||||||||||||||||||||