File Coverage

blib/lib/DateTime/Format/Duration/DurationString.pm
Criterion Covered Total %
statement 50 50 100.0
branch 12 12 100.0
condition 3 3 100.0
subroutine 13 13 100.0
pod 3 3 100.0
total 81 81 100.0


line stmt bran cond sub pod time code
1             package DateTime::Format::Duration::DurationString;
2              
3 4     4   536021 use Moo;
  4         43637  
  4         26  
4 4     4   5211 use Carp;
  4         8  
  4         260  
5 4     4   523 use DateTime::Duration;
  4         171111  
  4         178  
6 4     4   2541 use MooX::Types::MooseLike::Numeric 'PositiveOrZeroInt';
  4         30524  
  4         365  
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             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 parses a string and returns a duration.
32              
33             =cut
34              
35             # Constants:
36              
37 4     4   28 use constant SECOND => 1;
  4         5  
  4         321  
38 4     4   20 use constant MINUTE => 60 * SECOND;
  4         6  
  4         197  
39 4     4   19 use constant HOUR => 60 * MINUTE;
  4         5  
  4         229  
40 4     4   19 use constant DAY => 24 * HOUR;
  4         6  
  4         213  
41 4     4   21 use constant WEEK => 7 * DAY;
  4         10  
  4         1897  
42             # use constant MONTH => 31 * DAY;
43             # use constant YEAR => 365 * DAY;
44              
45             has 'seconds' => (is => 'rw', isa => PositiveOrZeroInt, default => 0);
46             has 'minutes' => (is => 'rw', isa => PositiveOrZeroInt, default => 0);
47             has 'hours' => (is => 'rw', isa => PositiveOrZeroInt, default => 0);
48             has 'days' => (is => 'rw', isa => PositiveOrZeroInt, default => 0);
49             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             sub to_seconds {
60 8     8 1 9 my $self = shift;
61            
62 8         95 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             sub to_duration {
79 1     1 1 2 my $self = shift;
80            
81 1         16 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             sub parse {
96 11     11 1 13605 my $self = shift;
97 11         18 my ($str) = @_;
98            
99 11         41 foreach my $token (split(/\s+/,$str)) {
100 14         28 $self->_parse_token($token);
101             }
102            
103 9         29 return $self;
104             }
105              
106             sub _parse_token {
107 14     14   16 my $self = shift;
108 14         16 my ($token) = @_;
109            
110 14 100       47 if ($token =~ /^(\d+)(\D?)$/) {
111 13         23 my $num = $1;
112 13         19 my $typ = $2;
113            
114 13 100 100     79 if ($typ eq 's') {
    100          
    100          
    100          
    100          
115 2         32 $self->seconds($self->seconds + $num);
116             }
117             elsif (($typ eq 'm')) {
118 1         16 $self->minutes($self->minutes + $num);
119             }
120             elsif (($typ eq 'h')||($typ eq '')) {
121 3         23 $self->hours($self->hours + $num);
122             }
123             elsif ($typ eq 'd'){
124 5         72 $self->days($self->days + $num);
125             }
126             elsif ($typ eq 'w'){
127 1         17 $self->weeks($self->weeks + $num);
128             }
129             else {
130 1         95 croak "$token has unknown type $typ";
131             }
132             }
133             else {
134 1         146 croak "$token not wellformed. ";
135             }
136 12         1860 return $self;
137             }
138              
139             1; # Return something nice to the caller
140              
141             =head1 TODO
142              
143             Parsestring in constructor?
144              
145             =head1 SEE ALSO
146              
147             L, L and L
148              
149             =head1 AUTHOR
150              
151             Bjorn-Olav Strand Ebo@startsiden.noE
152              
153             =head1 LICENSE
154              
155             Copyright 2009 by ABC Startsiden AS, BjErn-Olav Strand .
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