| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package DateTime::Format::Duration::DurationString; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 1 |  |  | 1 |  | 94637 | use Any::Moose; | 
|  | 1 |  |  |  |  | 192126 |  | 
|  | 1 |  |  |  |  | 8 |  | 
| 4 | 1 |  |  | 1 |  | 873 | use Carp; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 77 |  | 
| 5 | 1 |  |  | 1 |  | 541 | use DateTime::Duration; | 
|  | 0 |  |  |  |  |  |  | 
|  | 0 |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | =head1 NAME | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | DateTime::Format::Duration::DurationString - JIRA style parsing of duration | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | =head1 VERSION | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | Version 0.03 | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | =cut | 
| 16 |  |  |  |  |  |  |  | 
| 17 |  |  |  |  |  |  | our $VERSION = '0.03'; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | package MyApp; | 
| 22 |  |  |  |  |  |  | use DateTime::Format::Duration::DurationString; | 
| 23 |  |  |  |  |  |  |  | 
| 24 |  |  |  |  |  |  | my $seconds  = DateTime::Format::Duration::DurationString->new()->parse('1d 3h')->to_seconds; | 
| 25 |  |  |  |  |  |  | my $duration = DateTime::Format::Duration::DurationString->new()->parse('1d 3h')->to_duration; | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | C parses a string and returns a duration. | 
| 31 |  |  |  |  |  |  |  | 
| 32 |  |  |  |  |  |  | =cut | 
| 33 |  |  |  |  |  |  |  | 
| 34 |  |  |  |  |  |  | # Constants: | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | use constant SECOND => 1; | 
| 37 |  |  |  |  |  |  | use constant MINUTE => 60 * SECOND; | 
| 38 |  |  |  |  |  |  | use constant HOUR   => 60 * MINUTE; | 
| 39 |  |  |  |  |  |  | use constant DAY    => 24 * HOUR; | 
| 40 |  |  |  |  |  |  | use constant WEEK   => 7 * DAY; | 
| 41 |  |  |  |  |  |  | # use constant MONTH  => 31 * DAY; | 
| 42 |  |  |  |  |  |  | # use constant YEAR   => 365 * DAY; | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  | has 'seconds' => (is => 'rw', isa => 'Int', default => 0); | 
| 45 |  |  |  |  |  |  | has 'minutes' => (is => 'rw', isa => 'Int', default => 0); | 
| 46 |  |  |  |  |  |  | has 'hours'   => (is => 'rw', isa => 'Int', default => 0); | 
| 47 |  |  |  |  |  |  | has 'days'    => (is => 'rw', isa => 'Int', default => 0); | 
| 48 |  |  |  |  |  |  | has 'weeks'   => (is => 'rw', isa => 'Int', default => 0); | 
| 49 |  |  |  |  |  |  | # has 'months'  => (is => 'rw', isa => 'Int', default => 0); | 
| 50 |  |  |  |  |  |  | # has 'years'   => (is => 'rw', isa => 'Int', default => 0); | 
| 51 |  |  |  |  |  |  |  | 
| 52 |  |  |  |  |  |  | =head2 to_seconds | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | Return this object as seconds | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | =cut | 
| 57 |  |  |  |  |  |  |  | 
| 58 |  |  |  |  |  |  | sub to_seconds { | 
| 59 |  |  |  |  |  |  | my $self = shift; | 
| 60 |  |  |  |  |  |  |  | 
| 61 |  |  |  |  |  |  | return ($self->seconds * SECOND) | 
| 62 |  |  |  |  |  |  | + ($self->minutes * MINUTE) | 
| 63 |  |  |  |  |  |  | + ($self->hours   * HOUR) | 
| 64 |  |  |  |  |  |  | + ($self->days    * DAY) | 
| 65 |  |  |  |  |  |  | + ($self->weeks   * WEEK); | 
| 66 |  |  |  |  |  |  | #         + ($self->months  * MONTH) | 
| 67 |  |  |  |  |  |  | #         + ($self->years   * YEAR); | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | } | 
| 70 |  |  |  |  |  |  |  | 
| 71 |  |  |  |  |  |  | =head2 to_duration | 
| 72 |  |  |  |  |  |  |  | 
| 73 |  |  |  |  |  |  | Return this object as a DateTime::Duration | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | =cut | 
| 76 |  |  |  |  |  |  |  | 
| 77 |  |  |  |  |  |  | sub to_duration { | 
| 78 |  |  |  |  |  |  | my $self = shift; | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | return DateTime::Duration->new( weeks   => $self->weeks, | 
| 81 |  |  |  |  |  |  | days    => $self->days, | 
| 82 |  |  |  |  |  |  | hours   => $self->hours, | 
| 83 |  |  |  |  |  |  | minutes => $self->minutes, | 
| 84 |  |  |  |  |  |  | seconds => $self->seconds, | 
| 85 |  |  |  |  |  |  | ); | 
| 86 |  |  |  |  |  |  | } | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | =head2 parse | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | Parse a string | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | =cut | 
| 93 |  |  |  |  |  |  |  | 
| 94 |  |  |  |  |  |  | sub parse { | 
| 95 |  |  |  |  |  |  | my $self = shift; | 
| 96 |  |  |  |  |  |  | my ($str) = @_; | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | foreach my $token (split(/\s+/,$str)) { | 
| 99 |  |  |  |  |  |  | $self->_parse_token($token); | 
| 100 |  |  |  |  |  |  | } | 
| 101 |  |  |  |  |  |  |  | 
| 102 |  |  |  |  |  |  | return $self; | 
| 103 |  |  |  |  |  |  | } | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | sub _parse_token { | 
| 106 |  |  |  |  |  |  | my $self = shift; | 
| 107 |  |  |  |  |  |  | my ($token) = @_; | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | if ($token =~ /^(\d+)(\D?)$/) { | 
| 110 |  |  |  |  |  |  | my $num = $1; | 
| 111 |  |  |  |  |  |  | my $typ = $2; | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | if ($typ eq 's') { | 
| 114 |  |  |  |  |  |  | $self->seconds($self->seconds + $num); | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  | elsif (($typ eq 'm')) { | 
| 117 |  |  |  |  |  |  | $self->minutes($self->minutes + $num); | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  | elsif (($typ eq 'h')||($typ eq '')) { | 
| 120 |  |  |  |  |  |  | $self->hours($self->hours + $num); | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  | elsif ($typ eq 'd'){ | 
| 123 |  |  |  |  |  |  | $self->days($self->days + $num); | 
| 124 |  |  |  |  |  |  | } | 
| 125 |  |  |  |  |  |  | elsif ($typ eq 'w'){ | 
| 126 |  |  |  |  |  |  | $self->weeks($self->weeks + $num); | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  | else { | 
| 129 |  |  |  |  |  |  | croak "$token has unknown type $typ"; | 
| 130 |  |  |  |  |  |  | } | 
| 131 |  |  |  |  |  |  | } | 
| 132 |  |  |  |  |  |  | else { | 
| 133 |  |  |  |  |  |  | croak "$token not wellformed. "; | 
| 134 |  |  |  |  |  |  | } | 
| 135 |  |  |  |  |  |  | return $self; | 
| 136 |  |  |  |  |  |  | } | 
| 137 |  |  |  |  |  |  |  | 
| 138 |  |  |  |  |  |  | 1; # Return something nice to the caller | 
| 139 |  |  |  |  |  |  |  | 
| 140 |  |  |  |  |  |  | =head1 TODO | 
| 141 |  |  |  |  |  |  |  | 
| 142 |  |  |  |  |  |  | Parsestring in constructor? | 
| 143 |  |  |  |  |  |  |  | 
| 144 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 145 |  |  |  |  |  |  |  | 
| 146 |  |  |  |  |  |  | L, L and L | 
| 147 |  |  |  |  |  |  |  | 
| 148 |  |  |  |  |  |  | =head1 AUTHOR | 
| 149 |  |  |  |  |  |  |  | 
| 150 |  |  |  |  |  |  | BjErn-Olav Strand Ebo@startsiden.noE | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  | =head1 LICENSE | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | Copyright 2009 by ABC Startsiden AS, BjErn-Olav Strand . | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | =cut | 
| 159 |  |  |  |  |  |  |  |