| 1 | package Time::Ago; | ||||||||||||||||||||
| 2 | # ABSTRACT: Approximate duration in words | ||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | # Port of Rails distance_of_time_in_words and time_ago_in_words | ||||||||||||||||||||
| 5 | # http://apidock.com/rails/v4.2.1/ActionView/Helpers/DateHelper/distance_of_time_in_words | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 1 | ▶ | use utf8; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 1 | ▶ | use Encode; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | ▶ | use Locale::Messages qw/ bind_textdomain_filter /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use Locale::TextDomain 'Time-Ago'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | ▶ | use Scalar::Util qw/ blessed /; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 1 | our $VERSION = '1.00'; | |||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 1 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 19 | 1 | $ENV{OUTPUT_CHARSET} = 'UTF-8'; | |||||||||||||||||||
| 20 | 1 | bind_textdomain_filter 'Time-Ago' => \&Encode::decode_utf8; | |||||||||||||||||||
| 21 | } | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 1 | ▶ | use constant { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | 1 | MINUTES_IN_QUARTER_YEAR => 131400, # 91.25 days | |||||||||||||||||||
| 25 | MINUTES_IN_THREE_QUARTERS_YEAR => 394200, # 273.75 days | ||||||||||||||||||||
| 26 | MINUTES_IN_YEAR => 525600, | ||||||||||||||||||||
| 27 | 1 | }; | |||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 1 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=4 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 31 | 1 | my $class = shift; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 1 | ▶ | $class = ref($class) || $class; | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 34 | 1 | my $self = bless {}, $class; | |||||||||||||||||||
| 35 | |||||||||||||||||||||
| 36 | 1 | ▶ | while (@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 37 | 0 | my ($method, $val) = splice @_, 0, 2; | |||||||||||||||||||
| 38 | 0 | ▶ | $self->$method(ref $val eq 'ARRAY' ? @$val : $val); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 39 | } | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | 1 | return $self; | |||||||||||||||||||
| 42 | } | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | { | ||||||||||||||||||||
| 46 | my %locale = ( | ||||||||||||||||||||
| 47 | 4 | ▶ | about_x_hours => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 48 | 4 | __nx('about {count} hour', 'about {count} hours', $_, count => $_); | |||||||||||||||||||
| 49 | }, | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 4 | ▶ | about_x_months => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 52 | 4 | __nx('about {count} month', 'about {count} months', $_, count => $_); | |||||||||||||||||||
| 53 | }, | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 4 | ▶ | about_x_years => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 56 | 4 | __nx('about {count} year', 'about {count} years', $_, count => $_); | |||||||||||||||||||
| 57 | }, | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 5 | ▶ | almost_x_years => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 60 | 5 | __nx('almost {count} year', 'almost {count} years', $_, count => $_); | |||||||||||||||||||
| 61 | }, | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | 2 | ▶ | half_a_minute => sub { __('half a minute') }, | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 33 | ▶ | less_than_x_minutes => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 66 | 33 | __nx('less than a minute', 'less than {count} minutes', $_, count => $_); | |||||||||||||||||||
| 67 | }, | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | 6 | ▶ | less_than_x_seconds => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 70 | 6 | __nx( | |||||||||||||||||||
| 71 | 'less than {count} second', | ||||||||||||||||||||
| 72 | 'less than {count} seconds', | ||||||||||||||||||||
| 73 | $_, | ||||||||||||||||||||
| 74 | count => $_, | ||||||||||||||||||||
| 75 | ); | ||||||||||||||||||||
| 76 | }, | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 4 | ▶ | over_x_years => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 79 | 4 | __nx('over {count} year', 'over {count} years', $_, count => $_); | |||||||||||||||||||
| 80 | }, | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 4 | ▶ | x_days => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 83 | 4 | __nx('{count} day', '{count} days', $_, count => $_); | |||||||||||||||||||
| 84 | }, | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 66 | ▶ | x_minutes => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 87 | 66 | __nx('{count} minute', '{count} minutes', $_, count => $_); | |||||||||||||||||||
| 88 | }, | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 2 | ▶ | x_months => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 91 | 2 | __nx('{count} month', '{count} months', $_, count => $_); | |||||||||||||||||||
| 92 | }, | ||||||||||||||||||||
| 93 | 1 | ); | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 134 | ▶ | sub _locale { | ||||||||||||||||||
|
Subroutine
_locale: called CC=1 SCAR=0.1
| |||||||||||||||||||||
| 96 | 134 | my $self = shift; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 134 | ▶ | return sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.3
| |||||||||||||||||||||
| 99 | 134 | ▶ | my $string_id = shift or croak 'no string id supplied'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 100 | 134 | my %args = @_; | |||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | 134 | ▶ | my $sub = $locale{ $string_id } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | or croak "unknown locale string_id '$string_id'"; | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 134 | local $_ = $args{count}; | |||||||||||||||||||
| 106 | 134 | return $sub->(); | |||||||||||||||||||
| 107 | 134 | }; | |||||||||||||||||||
| 108 | } | ||||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 1 | ▶ | sub in_words { | ||||||||||||||||||
|
Subroutine
in_words: called CC=23 SCAR=31.4
Pod
documented
| |||||||||||||||||||||
| 113 | 134 | my $self = shift; | |||||||||||||||||||
| 114 | 134 | ▶ | my %args = (@_ % 2 ? (duration => @_) : @_); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 134 | ▶ | defined $args{duration} or croak 'no duration supplied'; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 117 | 134 | my $duration = $args{duration}; | |||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 134 | ▶ | if (blessed $duration) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 120 | 2 | ▶ | if ($duration->can('epoch')) { # DateTime/Time::Piece-like object | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 121 | 1 | $duration = time - $duration->epoch; | |||||||||||||||||||
| 122 | } elsif ($duration->can('delta_months')) { # DateTime::Duration-like | ||||||||||||||||||||
| 123 | # yes, we're treating every month as 30.41 days | ||||||||||||||||||||
| 124 | 1 | $duration = ($duration->delta_months * (86400 * 365 / 12)) + | |||||||||||||||||||
| 125 | ($duration->delta_days * 86400) + | ||||||||||||||||||||
| 126 | ($duration->delta_minutes * 60) + | ||||||||||||||||||||
| 127 | $duration->delta_seconds | ||||||||||||||||||||
| 128 | ; | ||||||||||||||||||||
| 129 | } | ||||||||||||||||||||
| 130 | } | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 134 | ▶ | my $round = sub { int($_[0] + 0.5) }; | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 134 | $duration = abs $duration; | |||||||||||||||||||
| 135 | 134 | my $mins = $round->($duration / 60); | |||||||||||||||||||
| 136 | 134 | my $secs = $round->($duration); | |||||||||||||||||||
| 137 | |||||||||||||||||||||
| 138 | 134 | my $locale = $self->_locale; | |||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | 134 | ▶ | if ($mins <= 1) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | 103 | ▶ | unless ($args{include_seconds}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 142 | 91 | ▶ | return $mins == 0 ? | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 143 | $locale->('less_than_x_minutes', count => 1) : | ||||||||||||||||||||
| 144 | $locale->('x_minutes', count => $mins) | ||||||||||||||||||||
| 145 | ; | ||||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 12 | ▶ | return $locale->('less_than_x_seconds', count => 5) if $secs <= 4; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 149 | 10 | ▶ | return $locale->('less_than_x_seconds', count => 10) if $secs <= 9; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 150 | 8 | ▶ | return $locale->('less_than_x_seconds', count => 20) if $secs <= 19; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 151 | 6 | ▶ | return $locale->('half_a_minute', count => 20) if $secs <= 39; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 152 | 4 | ▶ | return $locale->('less_than_x_minutes', count => 1) if $secs <= 59; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 153 | 2 | return $locale->('x_minutes', count => 1); | |||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 31 | ▶ | return $locale->('x_minutes', count => $mins) if $mins <= 44; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 157 | 27 | ▶ | return $locale->('about_x_hours', count => 1) if $mins <= 89; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | # 90 mins up to 24 hours | ||||||||||||||||||||
| 160 | 25 | ▶ | if ($mins <= 1439) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 161 | 2 | return $locale->('about_x_hours', count => $round->($mins/60)); | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | # 24 hours up to 42 hours | ||||||||||||||||||||
| 165 | 23 | ▶ | return $locale->('x_days', count => 1) if $mins <= 2519; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | # 42 hours up to 30 days | ||||||||||||||||||||
| 168 | 21 | ▶ | return $locale->('x_days', count => $round->($mins / 1440)) if $mins <= 43199; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | # 30 days up to 60 days | ||||||||||||||||||||
| 171 | 19 | ▶ | if ($mins <= 86399) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 172 | 4 | return $locale->('about_x_months', count => $round->($mins / 43200)); | |||||||||||||||||||
| 173 | } | ||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | # 60 days up to 365 days | ||||||||||||||||||||
| 176 | 15 | ▶ | if ($mins <= 525600) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 177 | 2 | return $locale->('x_months', count => $round->($mins / 43200)); | |||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | # XXX does not implement leap year stuff that Rails implementation has | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 13 | my $remainder = $mins % MINUTES_IN_YEAR; | |||||||||||||||||||
| 183 | 13 | my $years = int($mins / MINUTES_IN_YEAR); | |||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | 13 | ▶ | if ($remainder < MINUTES_IN_QUARTER_YEAR) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 186 | 4 | return $locale->('about_x_years', count => $years); | |||||||||||||||||||
| 187 | } | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | 9 | ▶ | if ($remainder < MINUTES_IN_THREE_QUARTERS_YEAR) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 190 | 4 | return $locale->('over_x_years', count => $years); | |||||||||||||||||||
| 191 | } | ||||||||||||||||||||
| 192 | |||||||||||||||||||||
| 193 | 5 | return $locale->('almost_x_years', count => $years + 1); | |||||||||||||||||||
| 194 | } | ||||||||||||||||||||
| 195 | |||||||||||||||||||||
| 196 | |||||||||||||||||||||
| 197 | 1 | 1; | |||||||||||||||||||
| 198 | |||||||||||||||||||||
| 199 | __END__ | ||||||||||||||||||||