| 1 | package Time::Duration::Concise; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 9 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 9 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 9 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 9 | ▶ | use Time::Seconds; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 9 | ▶ | use POSIX qw(ceil); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 9 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 9 | ▶ | use Tie::Hash::LRU; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 9 | our $VERSION = '2.61'; ## VERSION | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 9 | our %popular; | |||||||||||||||||||||||||||
| 15 | 9 | our $lru = tie %popular, 'Tie::Hash::LRU', 100; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | =head1 NAME | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | Time::Duration::Concise | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | Time::Duration::Concise is an improved approach to convert concise time duration to string representation. | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 9 | our %LENGTH_TO_PERIOD = ( | |||||||||||||||||||||||||||
| 28 | 86400 => 'day', | ||||||||||||||||||||||||||||
| 29 | 3600 => 'hour', | ||||||||||||||||||||||||||||
| 30 | 60 => 'minute', | ||||||||||||||||||||||||||||
| 31 | 1 => 'second', | ||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | 9 | our %PERIOD_SIZES = ( | |||||||||||||||||||||||||||
| 35 | 'd' => 86400, | ||||||||||||||||||||||||||||
| 36 | 'h' => 3600, | ||||||||||||||||||||||||||||
| 37 | 'm' => 60, | ||||||||||||||||||||||||||||
| 38 | 's' => 1, | ||||||||||||||||||||||||||||
| 39 | ); | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 9 | our @KNOWN_UNITS = qw[d h m s]; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 9 | our $KNOWN_UNITS_ = 'dhms'; | |||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | use Time::Duration::Concise; | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | my $duration = Time::Duration::Concise->new( | ||||||||||||||||||||||||||||
| 50 | interval => '1h20m' | ||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # Intervals can have decimal values | ||||||||||||||||||||||||||||
| 54 | # 1.5h etc | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | $duration = Time::Duration::Concise->new( | ||||||||||||||||||||||||||||
| 57 | interval => '1.5h' | ||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | =head1 FIELDS | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | =head2 interval (REQUIRED) | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | Concise Format | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | The format is an integer followed immediatley by its duration | ||||||||||||||||||||||||||||
| 67 | identifier. White-space will be ignored. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | The following table explains the format. | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | identifier duration | ||||||||||||||||||||||||||||
| 72 | ---------- -------- | ||||||||||||||||||||||||||||
| 73 | d day | ||||||||||||||||||||||||||||
| 74 | h hour | ||||||||||||||||||||||||||||
| 75 | m minute | ||||||||||||||||||||||||||||
| 76 | s second | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | # Intervals can have decimal values | ||||||||||||||||||||||||||||
| 79 | Example : 1.5h | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | =cut | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head1 METHODS | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =head2 interval | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | Returns the given time interval. | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =cut | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub interval { | ||||||||||||||||||||||||||||
| 92 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
interval: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 1 | return $self->{'_interval'}; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | =head2 seconds | ||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | The number of seconds represented by this time interval. | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | =cut | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub seconds { | ||||||||||||||||||||||||||||
| 103 | 133 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
seconds: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | 133 | ▶ | return $self->{'_seconds'} if $self->{'_seconds'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 35 | my $interval = $self->{'_interval'}; | |||||||||||||||||||||||||||
| 108 | 35 | my $known_units = $KNOWN_UNITS_; | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 35 | my $seconds = 0; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | # These should be integers, but we might need to have 0.5m | ||||||||||||||||||||||||||||
| 113 | 35 | ▶ | while ($interval =~ s/([+-]?\d*\.?\d+)([$known_units])//) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 61 | my $amount = $1; | |||||||||||||||||||||||||||
| 115 | 61 | my $units = $2; | |||||||||||||||||||||||||||
| 116 | 61 | $seconds += $amount * $PERIOD_SIZES{$units}; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 35 | ▶ | if ($interval ne '') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | # We had something which didn't match the above, which renders this unparseable | ||||||||||||||||||||||||||||
| 122 | 1 | Carp::croak("Bad format supplied [" . $interval . "]: unknown key."); | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | 34 | $self->{'_seconds'} = int $seconds; | |||||||||||||||||||||||||||
| 125 | 34 | return $self->{'_seconds'}; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head2 minutes | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | The number of minutes represented by this time interval. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | sub minutes { | ||||||||||||||||||||||||||||
| 135 | 22 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
minutes: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 136 | 22 | return $self->duration->{'minutes'}; | |||||||||||||||||||||||||||
| 137 | } | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | =head2 hours | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | The number of hours represented by this time interval. | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | =cut | ||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||
| 145 | sub hours { | ||||||||||||||||||||||||||||
| 146 | 20 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
hours: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 20 | return $self->duration->{'hours'}; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 days | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | The number of days represented by this time interval. | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | sub days { | ||||||||||||||||||||||||||||
| 157 | 20 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
days: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 158 | 20 | return $self->duration->{'days'}; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | =head2 weeks | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | The number of week represented by this time interval. | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =cut | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | sub weeks { | ||||||||||||||||||||||||||||
| 168 | 1 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
weeks: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 169 | 1 | return $self->duration->{'weeks'}; | |||||||||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | =head2 months | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | The number of months represented by this time interval. | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =cut | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | sub months { | ||||||||||||||||||||||||||||
| 179 | 2 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
months: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 180 | 2 | return $self->duration->{'months'}; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head2 as_string | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Concise time druation to string representation. | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =cut | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | sub as_string { | ||||||||||||||||||||||||||||
| 190 | 5 | ▶ | my ($self, $precision) = @_; | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 191 | 5 | my $time_frames = $self->_duration_array($precision); | |||||||||||||||||||||||||||
| 192 | 5 | return join(' ', @$time_frames); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | =head2 as_concise_string | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | Concise time druation to conscise string representation. | ||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||
| 199 | =cut | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | sub as_concise_string { | ||||||||||||||||||||||||||||
| 202 | 27 | ▶ | my ($self, $precision) = @_; | ||||||||||||||||||||||||||
|
Subroutine
as_concise_string: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 203 | 27 | ▶ | $precision ||= 10; | ||||||||||||||||||||||||||
|
Condition100%2/2
$precision ||= 10
MC/DC100%1/1
$precision ||= 10
$precision
Truth table100%2/2
A
A= $precision
| |||||||||||||||||||||||||||||
| 204 | 27 | my $time_frames = $self->_duration_array($precision); | |||||||||||||||||||||||||||
| 205 | my @concise_time_frames = map { | ||||||||||||||||||||||||||||
| 206 | 27 | s/\s+//rg =~ /([-|\+]?\d+[A-Za-z]{1})/ig; | |||||||||||||||||||||||||||
| 207 | 47 | $1; | |||||||||||||||||||||||||||
| 208 | } @$time_frames; | ||||||||||||||||||||||||||||
| 209 | 27 | $self->{"_duration_array_$precision"} = undef; | |||||||||||||||||||||||||||
| 210 | 27 | return join('', @concise_time_frames); | |||||||||||||||||||||||||||
| 211 | } | ||||||||||||||||||||||||||||
| 212 | |||||||||||||||||||||||||||||
| 213 | =head2 normalized_code | ||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||
| 215 | The largest division of Duration | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | =cut | ||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | sub normalized_code { | ||||||||||||||||||||||||||||
| 220 | 19 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
normalized_code: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 221 | 19 | my @keys = sort @KNOWN_UNITS; | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 19 | my $entry_code = '0s'; | |||||||||||||||||||||||||||
| 224 | 19 | ▶ | while ($entry_code eq '0s' and my $period = shift @keys) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$entry_code eq "0s" and my $period = shift @keys
MC/DC50%1/2
$entry_code eq "0s" and my $period = shift @keys
$entry_code eq "0s"
my $period = shift @keys
Truth table66%2/3
A and B
A= $entry_code eq "0s"B= my $period = shift @keys
| |||||||||||||||||||||||||||||
| 225 | 53 | my $period_length = $PERIOD_SIZES{$period}; | |||||||||||||||||||||||||||
| 226 | 53 | ▶ | if (not $self->seconds % $period_length) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | 19 | my $period_size = $self->seconds / $period_length; | |||||||||||||||||||||||||||
| 228 | 19 | $entry_code = $period_size . $period; | |||||||||||||||||||||||||||
| 229 | } | ||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | 19 | return $entry_code; | |||||||||||||||||||||||||||
| 232 | } | ||||||||||||||||||||||||||||
| 233 | |||||||||||||||||||||||||||||
| 234 | =head2 duration_array | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | Concise time druation to array | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | [ { value => 1, unit => 'day' }, { value => 2, unit => 'hours' } ] | ||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||
| 240 | =cut | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | sub duration_array { | ||||||||||||||||||||||||||||
| 243 | 29 | ▶ | my ($self, $precision) = @_; | ||||||||||||||||||||||||||
|
Subroutine
duration_array: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 244 | 29 | my $durations = $self->_duration_array($precision); | |||||||||||||||||||||||||||
| 245 | 29 | my @duration_distribution; | |||||||||||||||||||||||||||
| 246 | 29 | foreach my $d (@$durations) { | |||||||||||||||||||||||||||
| 247 | 62 | my @d_value_unit = split(' ', $d); | |||||||||||||||||||||||||||
| 248 | 62 | push( | |||||||||||||||||||||||||||
| 249 | @duration_distribution, | ||||||||||||||||||||||||||||
| 250 | { | ||||||||||||||||||||||||||||
| 251 | 'value' => $d_value_unit[0], | ||||||||||||||||||||||||||||
| 252 | 'unit' => $d_value_unit[1]}); | ||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | 29 | return \@duration_distribution; | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | sub _duration_array { | ||||||||||||||||||||||||||||
| 258 | 61 | ▶ | my ($self, $precision) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_duration_array: called CC=11 SCAR=24.0
| |||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | 61 | ▶ | $precision ||= 10; | ||||||||||||||||||||||||||
|
Condition100%2/2
$precision ||= 10
MC/DC100%1/1
$precision ||= 10
$precision
Truth table100%2/2
A
A= $precision
| |||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||
| 262 | return $self->{"_duration_array_$precision"} | ||||||||||||||||||||||||||||
| 263 | 61 | ▶ | if $self->{"_duration_array_$precision"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 51 | my $pretty_format = $self->duration->{'time'}->pretty; | |||||||||||||||||||||||||||
| 266 | 51 | $pretty_format =~ s/minus /-/ig; | |||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 51 | my @time_frame; | |||||||||||||||||||||||||||
| 269 | 51 | my $precision_counter = 1; | |||||||||||||||||||||||||||
| 270 | 51 | foreach my $frame (split(',', $pretty_format)) { | |||||||||||||||||||||||||||
| 271 | 175 | ▶ | next if $precision_counter > $precision; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 272 | 144 | chomp $frame; | |||||||||||||||||||||||||||
| 273 | 144 | $frame =~ s/^\s+|\s+$//g; | |||||||||||||||||||||||||||
| 274 | 144 | $frame =~ s/s$//ig; | |||||||||||||||||||||||||||
| 275 | 144 | $frame =~ /^([-|\+]?\d+\s)/ig; | |||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | # Make sure we gets the number | ||||||||||||||||||||||||||||
| 278 | # to avoid Use of uninitialized warning | ||||||||||||||||||||||||||||
| 279 | 144 | my $value = $1; | |||||||||||||||||||||||||||
| 280 | 144 | ▶ | if (defined $value && $value) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $value and $value
MC/DC0%0/2
defined $value and $value
defined $value
$value
Truth table33%1/3
A and B
A= defined $valueB= $value
| |||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||
| 282 | 144 | $value =~ s/\s+//ig; | |||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||
| 284 | 144 | ▶ | $frame = '' if $value == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 285 | 144 | ▶ | $frame .= 's' if $value > 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 144 | ▶ | if ($frame) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 288 | 94 | push(@time_frame, $frame); | |||||||||||||||||||||||||||
| 289 | 94 | $precision_counter++; | |||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||
| 291 | } | ||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||
| 293 | 51 | ▶ | if (!scalar @time_frame) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 294 | 1 | push(@time_frame, '0 second'); | |||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||
| 296 | 51 | $self->{"_duration_array_$precision"} = \@time_frame; | |||||||||||||||||||||||||||
| 297 | 51 | return \@time_frame; | |||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||
| 300 | =head2 multiple_units_of | ||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||
| 302 | Shorthand to call time methods | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | =cut | ||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||
| 306 | sub multiple_units_of { | ||||||||||||||||||||||||||||
| 307 | 7 | ▶ | my ($self, $unit) = @_; | ||||||||||||||||||||||||||
|
Subroutine
multiple_units_of: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 308 | # two is multiple! | ||||||||||||||||||||||||||||
| 309 | 7 | ▶ | return ($self->_minimum_number_of($unit) >= 2) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | =head2 minimum_number_of | ||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||
| 314 | Returns the minimum number of the given period. | ||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||
| 316 | =cut | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | sub minimum_number_of { | ||||||||||||||||||||||||||||
| 319 | 7 | ▶ | my ($self, $unit) = @_; | ||||||||||||||||||||||||||
|
Subroutine
minimum_number_of: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 320 | 7 | return ceil($self->_minimum_number_of($unit)); | |||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | sub _minimum_number_of { | ||||||||||||||||||||||||||||
| 324 | 14 | ▶ | my ($self, $unit) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_minimum_number_of: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 325 | 14 | my $orig_unit = $unit; | |||||||||||||||||||||||||||
| 326 | 14 | ▶ | $unit =~ s/s$// if (length($unit) > 1); # Chop plurals, but not 's' itself | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | 14 | $unit = substr($unit, 0, 1); | |||||||||||||||||||||||||||
| 328 | 14 | ▶ | $unit = 'mo' if $orig_unit =~ /^months|^mo/ig; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 14 | my %unit_maps = ( | |||||||||||||||||||||||||||
| 331 | 'mo' => 'months', | ||||||||||||||||||||||||||||
| 332 | 'w' => 'weeks', | ||||||||||||||||||||||||||||
| 333 | 'd' => 'days', | ||||||||||||||||||||||||||||
| 334 | 'h' => 'hours', | ||||||||||||||||||||||||||||
| 335 | 'm' => 'minutes', | ||||||||||||||||||||||||||||
| 336 | 's' => 'seconds', | ||||||||||||||||||||||||||||
| 337 | ); | ||||||||||||||||||||||||||||
| 338 | 14 | my $method = $unit_maps{$unit}; | |||||||||||||||||||||||||||
| 339 | 14 | ▶ | confess "Cannot determine period for $orig_unit" unless ($method); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||
| 341 | 13 | return $self->$method; | |||||||||||||||||||||||||||
| 342 | } | ||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||
| 344 | =head2 duration | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | Returns HASH of duration with the following keys | ||||||||||||||||||||||||||||
| 347 | |||||||||||||||||||||||||||||
| 348 | 'time' # Time::Seconds object | ||||||||||||||||||||||||||||
| 349 | 'years' | ||||||||||||||||||||||||||||
| 350 | 'months' | ||||||||||||||||||||||||||||
| 351 | 'weeks' | ||||||||||||||||||||||||||||
| 352 | 'days' | ||||||||||||||||||||||||||||
| 353 | 'hours' | ||||||||||||||||||||||||||||
| 354 | 'minutes' | ||||||||||||||||||||||||||||
| 355 | 'seconds' | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | =cut | ||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | sub duration { | ||||||||||||||||||||||||||||
| 360 | 120 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
duration: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 361 | 120 | ▶ | return $self->{'_duration'} if $self->{'_duration'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 362 | 34 | my $time_ = Time::Seconds->new($self->seconds); | |||||||||||||||||||||||||||
| 363 | 34 | my $duration = { | |||||||||||||||||||||||||||
| 364 | 'time' => $time_, | ||||||||||||||||||||||||||||
| 365 | 'years' => $time_->years, | ||||||||||||||||||||||||||||
| 366 | 'months' => $time_->months, | ||||||||||||||||||||||||||||
| 367 | 'weeks' => $time_->weeks, | ||||||||||||||||||||||||||||
| 368 | 'days' => $time_->days, | ||||||||||||||||||||||||||||
| 369 | 'hours' => $time_->hours, | ||||||||||||||||||||||||||||
| 370 | 'minutes' => $time_->minutes, | ||||||||||||||||||||||||||||
| 371 | 'seconds' => $time_->seconds | ||||||||||||||||||||||||||||
| 372 | }; | ||||||||||||||||||||||||||||
| 373 | 34 | $self->{'_duration'} = $duration; | |||||||||||||||||||||||||||
| 374 | 34 | return $duration; | |||||||||||||||||||||||||||
| 375 | } | ||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||
| 377 | =head2 get_time_layout | ||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||
| 379 | Return the duration hash with regards to precision | ||||||||||||||||||||||||||||
| 380 | |||||||||||||||||||||||||||||
| 381 | =cut | ||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||
| 383 | sub get_time_layout { | ||||||||||||||||||||||||||||
| 384 | 1 | ▶ | my ($self, $precision) = @_; | ||||||||||||||||||||||||||
|
Subroutine
get_time_layout: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 385 | 1 | my $duration = $self->duration; | |||||||||||||||||||||||||||
| 386 | my $time_layout = { | ||||||||||||||||||||||||||||
| 387 | duration => $self->seconds, | ||||||||||||||||||||||||||||
| 388 | day => $duration->{'day'}, | ||||||||||||||||||||||||||||
| 389 | hour => $duration->{'hour'}, | ||||||||||||||||||||||||||||
| 390 | 1 | second => $duration->{'seconds'}, | |||||||||||||||||||||||||||
| 391 | display_string => $self->as_string($precision)}; | ||||||||||||||||||||||||||||
| 392 | 1 | return $time_layout; | |||||||||||||||||||||||||||
| 393 | } | ||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||
| 395 | =head2 new | ||||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | Object constructor | ||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | =cut | ||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||
| 401 | sub new { ## no critic (RequireArgUnpacking) | ||||||||||||||||||||||||||||
| 402 | 43 | ▶ | my $class = shift; | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 403 | 43 | ▶ | my %params_ref = ref($_[0]) ? %{$_[0]} : @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | |||||||||||||||||||||||||||||
| 405 | 43 | my $interval = $params_ref{'interval'}; | |||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | 43 | ▶ | confess "Missing required arguments" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 408 | unless defined $interval; | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | 42 | ▶ | if ($popular{$interval}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 411 | ## Helps in multiple calling, it would really save the time | ||||||||||||||||||||||||||||
| 412 | 4 | return $popular{$interval}; | |||||||||||||||||||||||||||
| 413 | } | ||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||
| 415 | 38 | ▶ | if (defined $interval) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 416 | 38 | ▶ | Carp::croak("Invalid time interval") if $interval eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 417 | } | ||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||
| 419 | # Try our best to make it parseable. | ||||||||||||||||||||||||||||
| 420 | 37 | $interval =~ s/\s//g; | |||||||||||||||||||||||||||
| 421 | 37 | $interval = lc $interval; | |||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | # All numbers implies a number of seconds. | ||||||||||||||||||||||||||||
| 424 | 37 | ▶ | if ($interval !~ /[A-Za-z]/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 425 | 2 | $interval .= 's'; | |||||||||||||||||||||||||||
| 426 | } | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | 37 | my $self = { | |||||||||||||||||||||||||||
| 429 | _interval => $interval, | ||||||||||||||||||||||||||||
| 430 | }; | ||||||||||||||||||||||||||||
| 431 | 37 | my $obj = bless $self, $class; | |||||||||||||||||||||||||||
| 432 | 37 | $popular{$interval} = $obj; | |||||||||||||||||||||||||||
| 433 | 37 | return $obj; | |||||||||||||||||||||||||||
| 434 | } | ||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||
| 438 | Binary.com, C<< <perl at binary.com> >> | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | =head1 BUGS | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | Please report any bugs or feature requests to C<bug-time-duration-concise-localize at rt.cpan.org>, or through | ||||||||||||||||||||||||||||
| 443 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Time-Duration-Concise-Localize>. I will be notified, and then you'll | ||||||||||||||||||||||||||||
| 444 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||
| 446 | =head1 SUPPORT | ||||||||||||||||||||||||||||
| 447 | |||||||||||||||||||||||||||||
| 448 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||
| 450 | perldoc Time::Duration::Concise | ||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | You can also look for information at: | ||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | =over 4 | ||||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | =item * RT: CPAN's request tracker (report bugs here) | ||||||||||||||||||||||||||||
| 458 | |||||||||||||||||||||||||||||
| 459 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Time-Duration-Concise-Localize> | ||||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||
| 462 | |||||||||||||||||||||||||||||
| 463 | L<http://annocpan.org/dist/Time-Duration-Concise-Localize> | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | =item * CPAN Ratings | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | L<http://cpanratings.perl.org/d/Time-Duration-Concise-Localize> | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | =item * Search CPAN | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | L<http://search.cpan.org/dist/Time-Duration-Concise-Localize/> | ||||||||||||||||||||||||||||
| 472 | |||||||||||||||||||||||||||||
| 473 | =back | ||||||||||||||||||||||||||||
| 474 | |||||||||||||||||||||||||||||
| 475 | =cut | ||||||||||||||||||||||||||||
| 476 | |||||||||||||||||||||||||||||
| 477 | 9 | 1; # End of Time::Duration::Concise | |||||||||||||||||||||||||||