| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Time::Duration::Abbreviated; | 
| 2 | 5 |  |  | 5 |  | 9110 | use 5.008005; | 
|  | 5 |  |  |  |  | 16 |  | 
|  | 5 |  |  |  |  | 201 |  | 
| 3 | 5 |  |  | 5 |  | 26 | use strict; | 
|  | 5 |  |  |  |  | 10 |  | 
|  | 5 |  |  |  |  | 151 |  | 
| 4 | 5 |  |  | 5 |  | 38 | use warnings; | 
|  | 5 |  |  |  |  | 9 |  | 
|  | 5 |  |  |  |  | 138 |  | 
| 5 | 5 |  |  | 5 |  | 6102 | use Time::Duration qw(); | 
|  | 5 |  |  |  |  | 18380 |  | 
|  | 5 |  |  |  |  | 116 |  | 
| 6 | 5 |  |  | 5 |  | 4501 | use parent qw(Exporter); | 
|  | 5 |  |  |  |  | 1549 |  | 
|  | 5 |  |  |  |  | 26 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | our $VERSION = "0.01"; | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | our @EXPORT = qw(later later_exact earlier earlier_exact | 
| 11 |  |  |  |  |  |  | ago ago_exact from_now from_now_exact | 
| 12 |  |  |  |  |  |  | duration duration_exact concise); | 
| 13 |  |  |  |  |  |  | our @EXPORT_OK = ('interval', @EXPORT); | 
| 14 |  |  |  |  |  |  |  | 
| 15 |  |  |  |  |  |  | sub concise { | 
| 16 | 3 |  |  | 3 | 0 | 12 | Time::Duration::concise($_[0]); | 
| 17 |  |  |  |  |  |  | } | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | sub later { | 
| 20 | 12 |  |  | 12 | 0 | 12189 | interval($_[0], $_[1], '%s ago', '%s later'); | 
| 21 |  |  |  |  |  |  | } | 
| 22 |  |  |  |  |  |  |  | 
| 23 |  |  |  |  |  |  | sub later_exact { | 
| 24 | 9 |  |  | 9 | 0 | 2533 | interval_exact($_[0], '%s ago', '%s later'); | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  |  | 
| 27 |  |  |  |  |  |  | sub earlier { | 
| 28 | 12 |  |  | 12 | 0 | 3993 | interval($_[0], $_[1], '%s later', '%s ago'); | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  |  | 
| 31 |  |  |  |  |  |  | sub earlier_exact { | 
| 32 | 9 |  |  | 9 | 0 | 1451 | interval_exact($_[0], '%s later', '%s ago'); | 
| 33 |  |  |  |  |  |  | } | 
| 34 |  |  |  |  |  |  |  | 
| 35 |  |  |  |  |  |  | sub ago { | 
| 36 | 4 |  |  | 4 | 0 | 1329 | &earlier | 
| 37 |  |  |  |  |  |  | } | 
| 38 |  |  |  |  |  |  |  | 
| 39 |  |  |  |  |  |  | sub ago_exact { | 
| 40 | 4 |  |  | 4 | 0 | 375 | &earlier_exact | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | sub from_now { | 
| 44 | 4 |  |  | 4 | 0 | 1733 | &later | 
| 45 |  |  |  |  |  |  | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | sub from_now_exact { | 
| 48 | 4 |  |  | 4 | 0 | 603 | &later_exact | 
| 49 |  |  |  |  |  |  | } | 
| 50 |  |  |  |  |  |  |  | 
| 51 |  |  |  |  |  |  | sub duration_exact { | 
| 52 | 5 | 100 |  | 5 | 0 | 2459 | (my $span = shift) || return '0 sec'; | 
| 53 | 3 |  |  |  |  | 17 | _render('%s', Time::Duration::_separate(abs $span)); | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | sub duration { | 
| 57 | 6 | 100 |  | 6 | 0 | 6431 | (my $span = shift) || return '0 sec'; | 
| 58 | 4 |  | 100 |  |  | 23 | my $precision = int(shift || 0) || 2;  # precision (default: 2) | 
| 59 | 4 |  |  |  |  | 13 | _render( | 
| 60 |  |  |  |  |  |  | '%s', | 
| 61 |  |  |  |  |  |  | Time::Duration::_approximate($precision, Time::Duration::_separate(abs $span)) | 
| 62 |  |  |  |  |  |  | ); | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | sub interval_exact { | 
| 66 | 18 |  |  | 18 | 0 | 34 | my ($span, $neg_direction, $pos_direction) = @_; | 
| 67 |  |  |  |  |  |  |  | 
| 68 | 18 |  |  |  |  | 42 | _render( | 
| 69 |  |  |  |  |  |  | _determine_direction($span, $neg_direction, $pos_direction), | 
| 70 |  |  |  |  |  |  | Time::Duration::_separate($span) | 
| 71 |  |  |  |  |  |  | ); | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  |  | 
| 74 |  |  |  |  |  |  | sub interval { | 
| 75 | 24 |  |  | 24 | 0 | 672 | my ($span, $precision, $neg_direction, $pos_direction) = @_; | 
| 76 |  |  |  |  |  |  |  | 
| 77 | 24 |  | 100 |  |  | 694 | $precision = int($precision || 0) || 2; | 
| 78 | 24 |  |  |  |  | 48 | _render( | 
| 79 |  |  |  |  |  |  | _determine_direction($span, $neg_direction, $pos_direction), | 
| 80 |  |  |  |  |  |  | Time::Duration::_approximate($precision, Time::Duration::_separate($span)) | 
| 81 |  |  |  |  |  |  | ); | 
| 82 |  |  |  |  |  |  | } | 
| 83 |  |  |  |  |  |  |  | 
| 84 |  |  |  |  |  |  | sub _determine_direction { | 
| 85 | 42 |  |  | 42 |  | 63 | my ($span, $neg_direction, $pos_direction) = @_; | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 5 |  |  | 5 |  | 2784 | no warnings qw(numeric uninitialized); | 
|  | 5 |  |  |  |  | 11 |  | 
|  | 5 |  |  |  |  | 357 |  | 
| 88 | 42 | 100 |  |  |  | 119 | my $direction = ($span <= -1) ? $neg_direction | 
|  |  | 100 |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | : ($span >=  1) ? $pos_direction | 
| 90 |  |  |  |  |  |  | : 'now'; | 
| 91 | 5 |  |  | 5 |  | 27 | use warnings; | 
|  | 5 |  |  |  |  | 21 |  | 
|  | 5 |  |  |  |  | 1197 |  | 
| 92 |  |  |  |  |  |  |  | 
| 93 | 42 |  |  |  |  | 140 | return $direction; | 
| 94 |  |  |  |  |  |  | } | 
| 95 |  |  |  |  |  |  |  | 
| 96 |  |  |  |  |  |  | my %units = ( | 
| 97 |  |  |  |  |  |  | second => 'sec', | 
| 98 |  |  |  |  |  |  | minute => 'min', | 
| 99 |  |  |  |  |  |  | hour   => 'hr', | 
| 100 |  |  |  |  |  |  | day    => 'day', | 
| 101 |  |  |  |  |  |  | year   => 'yr', | 
| 102 |  |  |  |  |  |  | ); | 
| 103 |  |  |  |  |  |  |  | 
| 104 |  |  |  |  |  |  | sub _render { | 
| 105 | 49 |  |  | 49 |  | 1921 | my ($direction, @pieces) = @_; | 
| 106 |  |  |  |  |  |  |  | 
| 107 | 49 |  |  |  |  | 58 | my @wheel; | 
| 108 | 49 |  |  |  |  | 82 | for my $piece (@pieces) { | 
| 109 | 245 | 100 |  |  |  | 512 | next if $piece->[1] == 0; | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 112 |  |  |  |  | 130 | my $val  = $piece->[1]; | 
| 112 | 112 |  |  |  |  | 174 | my $unit = $units{$piece->[0]}; | 
| 113 | 112 | 100 |  |  |  | 394 | if ($unit =~ /\A(?:hr|day|yr)\Z/) { | 
| 114 | 85 | 100 |  |  |  | 178 | $unit .= 's' if $val > 1; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 | 112 |  |  |  |  | 262 | push @wheel, "$val $unit"; | 
| 118 |  |  |  |  |  |  | } | 
| 119 |  |  |  |  |  |  |  | 
| 120 | 49 | 100 |  |  |  | 172 | return "now" unless @wheel; | 
| 121 | 33 |  |  |  |  | 256 | return sprintf($direction, join ' ', @wheel); | 
| 122 |  |  |  |  |  |  | } | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | 1; | 
| 125 |  |  |  |  |  |  | __END__ |