blib/lib/DateTime/Format/Duration/DurationString.pm

filter: Statementstmt 100% 57 / 57 Branchbran 100% 12 / 12 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 13 / 13 Podpod 100% 3 / 3 totaltotal 100% 90 / 90 CC 9 SCAR 13.2
CC
9
Cov
100%
_parse_token
8.0
parse
2.0
BEGIN
1.0
CRAP 3.8
↑ Summary
1 package DateTime::Format::Duration::DurationString;
2  
3 4 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use DateTime::Duration;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use MooX::Types::MooseLike::Numeric 'PositiveOrZeroInt';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 DateTime::Format::Duration::DurationString - JIRA style parsing of duration
11  
12 =head1 VERSION
13  
14 Version 0.04
15  
16 =cut
17  
18 4 our $VERSION = '0.04';
19  
20 =head1 SYNOPSIS
21  
22 package MyApp;
23 use DateTime::Format::Duration::DurationString;
24
25 my $seconds = DateTime::Format::Duration::DurationString->new()->parse('1d 3h')->to_seconds;
26 my $duration = DateTime::Format::Duration::DurationString->new()->parse('1d 3h')->to_duration;
27  
28  
29 =head1 DESCRIPTION
30  
31 C<DateTime::Format::Duration::DurationString> parses a string and returns a duration.
32  
33 =cut
34  
35 # Constants:
36  
37 4 ▶use constant SECOND => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 4 ▶use constant MINUTE => 60 * SECOND;
Subroutine BEGIN: called CC=1 SCAR=0.0
39 4 ▶use constant HOUR => 60 * MINUTE;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 4 ▶use constant DAY => 24 * HOUR;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶use constant WEEK => 7 * DAY;
Subroutine BEGIN: called CC=1 SCAR=0.0
42 # use constant MONTH => 31 * DAY;
43 # use constant YEAR => 365 * DAY;
44  
45 4 has 'seconds' => (is => 'rw', isa => PositiveOrZeroInt, default => 0);
46 4 has 'minutes' => (is => 'rw', isa => PositiveOrZeroInt, default => 0);
47 4 has 'hours' => (is => 'rw', isa => PositiveOrZeroInt, default => 0);
48 4 has 'days' => (is => 'rw', isa => PositiveOrZeroInt, default => 0);
49 4 has 'weeks' => (is => 'rw', isa => PositiveOrZeroInt, default => 0);
50 # has 'months' => (is => 'rw', isa => PositiveOrZeroInt, default => 0);
51 # has 'years' => (is => 'rw', isa => PositiveOrZeroInt, default => 0);
52  
53 =head2 to_seconds
54  
55 Return this object as seconds
56  
57 =cut
58  
59 8 ▶sub to_seconds {
Subroutine to_seconds: called CC=1 SCAR=0.0
Pod documented
60 8 my $self = shift;
61
62 8 return ($self->seconds * SECOND)
63 + ($self->minutes * MINUTE)
64 + ($self->hours * HOUR)
65 + ($self->days * DAY)
66 + ($self->weeks * WEEK);
67 # + ($self->months * MONTH)
68 # + ($self->years * YEAR);
69
70 }
71  
72 =head2 to_duration
73  
74 Return this object as a DateTime::Duration
75  
76 =cut
77  
78 1 ▶sub to_duration {
Subroutine to_duration: called CC=1 SCAR=0.0
Pod documented
79 1 my $self = shift;
80
81 1 return DateTime::Duration->new( weeks => $self->weeks,
82 days => $self->days,
83 hours => $self->hours,
84 minutes => $self->minutes,
85 seconds => $self->seconds,
86 );
87 }
88  
89 =head2 parse
90  
91 Parse a string
92  
93 =cut
94  
95 11 ▶sub parse {
Subroutine parse: called CC=2 SCAR=6.9
Pod documented
96 11 my $self = shift;
97 11 my ($str) = @_;
98
99 11 foreach my $token (split(/\s+/,$str)) {
100 14 $self->_parse_token($token);
101 }
102
103 9 return $self;
104 }
105  
106 14 ▶sub _parse_token {
Subroutine _parse_token: called CC=8 SCAR=20.8
107 14 my $self = shift;
108 14 my ($token) = @_;
109
110 14 ▶ if ($token =~ /^(\d+)(\D?)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /^(\d+)(\D?)$/) { } 13 1 14
111 13 my $num = $1;
112 13 my $typ = $2;
113
114 13 ▶ if ($typ eq 's') {
Branch: 5 branches
BranchTrueFalseTotal
if ($typ eq 's') { } 2 11 13
elsif ($typ eq 'm') { } 1 10 11
elsif ($typ eq 'h' or $typ eq '') { } 3 7 10
elsif ($typ eq 'd') { } 5 2 7
elsif ($typ eq 'w') { } 1 1 2
115 2 $self->seconds($self->seconds + $num);
116 }
117 elsif (($typ eq 'm')) {
118 1 $self->minutes($self->minutes + $num);
119 }
120 ▶ elsif (($typ eq 'h')||($typ eq '')) {
Condition100%3/3
$typ eq "h" or $typ eq ""
l!l&&r!l&&!r
127
MC/DC100%2/2
$typ eq "h" or $typ eq ""
$typ eq "h" $typ eq ""
Truth table100%3/3
A or B
A= $typ eq "h"B= $typ eq ""
ABresult
1X1
011
000
121 3 $self->hours($self->hours + $num);
122 }
123 elsif ($typ eq 'd'){
124 5 $self->days($self->days + $num);
125 }
126 elsif ($typ eq 'w'){
127 1 $self->weeks($self->weeks + $num);
128 }
129 else {
130 1 croak "$token has unknown type $typ";
131 }
132 }
133 else {
134 1 croak "$token not wellformed. <duration><wdhms>";
135 }
136 12 return $self;
137 }
138  
139 4 1; # Return something nice to the caller
140  
141 =head1 TODO
142  
143 Parsestring in constructor?
144  
145 =head1 SEE ALSO
146  
147 L<DateTime>, L<DateTime::Duration> and L<DateTime::Format::Duration>
148  
149 =head1 AUTHOR
150  
151 Bjorn-Olav Strand E<lt>bo@startsiden.noE<gt>
152  
153 =head1 LICENSE
154  
155 Copyright 2009 by ABC Startsiden AS, BjE<oslash>rn-Olav Strand <bo@startsiden.no>.
156  
157 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
158  
159 =cut
160  
↑ Summary