blib/lib/Date/Period/Human.pm

filter: Statementstmt 70.4% 62 / 88 Branchbran 63.5% 47 / 74 Conditioncond 63.0% 29 / 46 MC/DCmcdc 41.6% 10 / 24 Subroutinesub 100% 10 / 10 Podpod 100% 2 / 2 totaltotal 65.5% 160 / 244 CC 54 SCAR 52.2
CC
54
Cov
66%
human_readable
246.6
_parse_mysql_date
4.0
_get_date_parts
3.0
CRAP 184.6
↑ Summary
1 package Date::Period::Human;
2  
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 ▶use Date::Calc qw/Delta_DHMS Today_and_Now N_Delta_YMDHMS/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 our $VERSION='0.4.8';
10  
11 4 ▶use utf8; # umlauts in translations
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 4 our %translation = (
14 de => {
15 time_months_ago => 'vor %d Monaten',
16 time_a_month_ago => 'vor einem Monat',
17 time_years_ago => 'vor %d Jahren',
18 time_a_year_ago => 'vor einem Jahr',
19 time_weeks_ago => 'vor %d Wochen',
20 time_a_week_ago => 'vor einer Woche',
21 time_num_days_ago => 'vor %d Tagen',
22 time_yesterday_at => 'Gestern um %02d:%02d',
23 time_hour_min_ago => 'vor %d Stunden %d Minuten',
24 time_minute_ago => 'vor %d Minute',
25 time_minutes_ago => 'vor %d Minuten',
26 time_less_than_minute_ago => 'vor weniger als einer Minute',
27 time_just_now => 'gerade eben',
28  
29 time_in_less_than_minute => 'in %d Sekunden',
30 time_in_minute => 'in %d Minute',
31 time_in_minutes => 'in %d Minuten',
32 time_in_hour_minutes => 'in %d Stunde %d Minuten',
33 time_in_hours_minutes => 'in %d Stunde %d Minuten',
34 time_tomorrow_at => 'Morgen um %02d:%02d',
35 time_after_tomorrow_at => 'Übermorgen um %02d:%02d',
36 time_in_num_days => 'in %d Tagen',
37 time_in_week => 'in %d Woche',
38 time_in_weeks => 'in %d Wochen',
39 time_in_month => 'in %d Monat',
40 time_in_months => 'in %d Monaten',
41 time_in_year => 'in %d Jahr',
42 time_in_over_year => 'in über einem Jahr',
43 time_in_years => 'in %d Jahren',
44 time_in_over_years => 'in über %d Jahren',
45 },
46 nl => {
47 time_months_ago => '%d maanden geleden',
48 time_a_month_ago => 'een maand geleden',
49 time_years_ago => '%d jaar geleden',
50 time_a_year_ago => 'een jaar geleden',
51 time_a_week_ago => 'een week geleden',
52 time_weeks_ago => '%d weken geleden',
53 time_num_days_ago => '%d dagen geleden',
54 time_yesterday_at => 'gisteren om %02d:%02d',
55 time_hour_min_ago => '%d uur %d minuten geleden',
56 time_minute_ago => '%d minuut geleden',
57 time_minutes_ago => '%d minuten geleden',
58 time_less_than_minute_ago => 'minder dan een minuut geleden',
59 time_just_now => 'net precies',
60  
61 time_in_less_than_minute => 'in %d seconden',
62 time_in_minute => 'in %d minute',
63 time_in_minutes => 'in %d minuten',
64 time_in_hour_minutes => 'in %d uur %d minuten',
65 time_in_hours_minutes => 'in %d uur %d minuten',
66 time_tomorrow_at => 'morgen een %02d:%02d',
67 time_after_tomorrow_at => 'de dag nar morgen een %02d:%02d',
68 time_in_num_days => 'in %d fagen',
69 time_in_week => 'in %d week',
70 time_in_weeks => 'in %d weken',
71 time_in_month => 'in %d maand',
72 time_in_months => 'in %d maanden',
73 time_in_year => 'in %d jaar',
74 time_in_over_year => 'in mer dan een jaar',
75 time_in_years => 'in %d jaar',
76 time_in_over_years => 'in mer dan %d jaar',
77 },
78 en => {
79 time_months_ago => '%d months ago',
80 time_a_month_ago => 'a month ago',
81 time_years_ago => '%d years ago',
82 time_a_year_ago => 'a year ago',
83 time_a_week_ago => 'a week ago',
84 time_weeks_ago => '%d weeks ago',
85 time_num_days_ago => '%d days ago',
86 time_yesterday_at => 'yesterday at %02d:%02d',
87 time_hour_min_ago => '%d hour %d minutes ago',
88 time_minute_ago => '%d minute ago',
89 time_minutes_ago => '%d minutes ago',
90 time_less_than_minute_ago => 'less than a minute ago',
91 time_just_now => 'just now',
92  
93 time_in_less_than_minute => 'in %d seconds',
94 time_in_minute => 'in %d minute',
95 time_in_minutes => 'in %d minutes',
96 time_in_hour_minutes => 'in %d hour %d minutes',
97 time_in_hours_minutes => 'in %d hours %d minutes',
98 time_tomorrow_at => 'tomorrow at %02d:%02d',
99 time_after_tomorrow_at => 'the day after tomorrow at %02d:%02d',
100 time_in_num_days => 'in %d days',
101 time_in_week => 'in %d week',
102 time_in_weeks => 'in %d weeks',
103 time_in_month => 'in %d month',
104 time_in_months => 'in %d months',
105 time_in_year => 'in %d year',
106 time_in_over_year => 'in over a year',
107 time_in_years => 'in %d years',
108 time_in_over_years => 'in over %d years',
109 },
110 en_short => {
111 time_months_ago => '%dm ago',
112 time_a_month_ago => '1m ago',
113 time_years_ago => '%dy ago',
114 time_a_year_ago => '1y ago',
115 time_a_week_ago => '1w ago',
116 time_weeks_ago => '%dw ago',
117 time_num_days_ago => '%dd ago',
118 time_yesterday_at => '%02d:%02d yesterday',
119 time_hour_min_ago => '%dh %dm ago',
120 time_minute_ago => '%dm ago',
121 time_minutes_ago => '%dm ago',
122 time_less_than_minute_ago => '< 1m ago',
123 time_just_now => 'just now',
124  
125 time_in_less_than_minute => 'in %ds',
126 time_in_minute => 'in %dm',
127 time_in_minutes => 'in %dm',
128 time_in_hour_minutes => 'in %dh %dm',
129 time_in_hours_minutes => 'in %dh %dm',
130 time_tomorrow_at => 'tomorrow at %02d:%02d',
131 time_after_tomorrow_at => 'the day after tomorrow at %02d:%02d',
132 time_in_num_days => 'in %dd',
133 time_in_week => 'in %dw',
134 time_in_weeks => 'in %dw',
135 time_in_month => 'in %dm',
136 time_in_months => 'in %dm',
137 time_in_year => 'in %dy',
138 time_in_over_year => 'in > 1y',
139 time_in_years => 'in %dy',
140 time_in_over_years => 'in > %dy',
141 },
142 );
143  
144 10 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
145 10 my ($klass, $args) = @_;
146 my $self = {
147 today_and_now => $args->{today_and_now},
148 10 ▶ lang => $args->{lang} || 'nl',
Condition100%2/2
$args->{'lang'} || 'nl'
l!l
91
MC/DC100%1/1
$args->{'lang'} || 'nl'
$args->{'lang'}
Truth table100%2/2
A
A= $args->{'lang'}
Aresult
11
00
149 };
150 10 return bless $self, $klass;
151 }
152  
153 69 ▶sub _parse_mysql_date {
Subroutine _parse_mysql_date: called CC=4 SCAR=13.9
154 69 my ($mysql_date) = @_;
155  
156 69 ▶ if ($mysql_date && $mysql_date =~ m/^(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mysql_date and $mysql_date =~ /^(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})$/) 63 6 69
Condition100%3/3
$mysql_date and $mysql_date =~ /^(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})$/
!ll&&!rl&&r
2463
MC/DC100%2/2
$mysql_date and $mysql_date =~ /^(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})$/
$mysql_date $mysql_date =~ /^(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})$/
Truth table100%3/3
A and B
A= $mysql_dateB= $mysql_date =~ /^(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):(\d{2})$/
ABresult
0X0
100
111
157 63 return $1, $2, $3, $4, $5, $6;
158 }
159 6 ▶ $mysql_date ||= '';
Condition100%2/2
$mysql_date ||= ""
l!l
42
MC/DC100%1/1
$mysql_date ||= ""
$mysql_date
Truth table100%2/2
A
A= $mysql_date
Aresult
11
00
160 6 croak "Not a MySQL date: [$mysql_date]";
161 }
162  
163 111 ▶sub _get_date_parts {
Subroutine _get_date_parts: called CC=3 SCAR=11.0
164 111 my ($self, $date) = @_;
165  
166 111 ▶ if (ref($date)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $date) 48 63 111
167 48 return ($date->year, $date->month, $date->day, $date->hour, $date->minute, $date->second);
168 }
169  
170 63 ▶ if ($date =~ /^\d+$/o){
Branch: 1 branch
BranchTrueFalseTotal
if ($date =~ /^\d+$/o) 1 62 63
171 1 my @x = gmtime($date);
172 return (
173 1 $x[5]+1900,
174 $x[4]+1,
175 $x[3],
176 $x[2],
177 $x[1],
178 $x[0]
179 );
180 }
181  
182 62 return _parse_mysql_date($date);
183 }
184  
185 111 ▶sub human_readable {
Subroutine human_readable: called CC=47 SCAR=55.1
Pod documented
186 111 my ($self, $date) = @_;
187  
188 111 my @date = $self->_get_date_parts($date);
189  
190 111 ▶ my @now = ref($self->{today_and_now}) eq 'ARRAY' ? @{$self->{today_and_now}} : ();
Branch: 1 branch
BranchTrueFalseTotal
ref $self->{'today_and_now'} eq 'ARRAY' ? : 110 1 111
191  
192 111 ▶ if (!@now) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@now) 1 110 111
193 1 @now = Today_and_Now(0);
194 }
195 111 my ($Dy, $DM, $Dd,$Dh,$Dm,$Ds) = N_Delta_YMDHMS(@date,@now);
196  
197 ## the past
198 111 ▶ if ($Dy == 1) {
Branch: 18 branches
BranchTrueFalseTotal
if ($Dy == 1) { } 3 108 111
elsif ($Dy > 1) { } 1 107 108
elsif ($DM == 1) { } 4 103 107
elsif ($DM > 1) { } 0 103 103
elsif ($Dd >= 7) { } 10 93 103
elsif ($Dd > 1) { } 7 86 93
elsif ($Dd == 1) { } 7 79 86
elsif ($Dd == 0 and $Dh >= 1) { } 21 58 79
elsif ($Dd == 0 and $Dh == 0 and $Dm > 0) { } 35 23 58
elsif ($Dd == 0 and $Dh == 0 and $Dm == 0 and $Ds > 5) { } 7 16 23
elsif ($Dy == 0 and $DM < -12) { } 0 16 16
elsif ($Dy < 0) { } 0 16 16
elsif ($DM < 0) { } 0 16 16
elsif ($Dd <= -2) { } 2 14 16
elsif ($Dd == -1) { } 0 14 14
elsif ($Dd == 0 and $Dh <= -1) { } 0 14 14
elsif ($Dd == 0 and $Dh == 0 and $Dm < 0) { } 0 14 14
elsif ($Dd == 0 and $Dh == 0 and $Dm == 0 and $Ds < -5) { } 0 14 14
199 3 return $self->_translate('time_a_year_ago');
200 }
201 elsif ($Dy > 1) {
202 1 return $self->_translate('time_years_ago', $Dy);
203 }
204 elsif ($DM == 1) {
205 4 return $self->_translate('time_a_month_ago');
206 }
207 elsif ($DM > 1) {
208 0 return $self->_translate('time_months_ago', $DM);
209 }
210 elsif ($Dd >= 7) {
211 10 ▶ if (int($Dd / 7) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (int $Dd / 7 == 1) 7 3 10
212 7 return $self->_translate('time_a_week_ago', 1);
213 }
214 3 ▶ if (int($Dd / 7) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (int $Dd / 7 > 1) 3 0 3
215 3 return $self->_translate('time_weeks_ago', int($Dd / 7));
216 }
217 }
218 elsif ($Dd > 1) {
219 7 return $self->_translate('time_num_days_ago', $Dd);
220 }
221 elsif ($Dd == 1) {
222 7 return $self->_translate('time_yesterday_at', $date[3], $date[4]);
223 }
224 ▶ elsif ($Dd == 0 && $Dh >= 1) {
Condition100%3/3
$Dd == 0 and $Dh >= 1
!ll&&!rl&&r
25621
MC/DC100%2/2
$Dd == 0 and $Dh >= 1
$Dd == 0 $Dh >= 1
Truth table100%3/3
A and B
A= $Dd == 0B= $Dh >= 1
ABresult
0X0
100
111
225 21 return $self->_translate('time_hour_min_ago', $Dh, $Dm);
226 }
227 ▶ elsif ($Dd == 0 && $Dh == 0 && $Dm > 0) {
Condition83%5/6
$Dd == 0 and $Dh == 0 and $Dm > 0
!ll&&!rl&&r
22135
$Dd == 0 and $Dh == 0
!ll&&!rl&&r
2056
MC/DC66%2/3
$Dd == 0 and $Dh == 0 and $Dm > 0
$Dd == 0 $Dh == 0 $Dm > 0
Truth table75%3/4
A and B and C
A= $Dd == 0B= $Dh == 0C= $Dm > 0
ABCresult
0XX0
10X0
1100
1111
228 35 ▶ if ($Dm == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Dm == 1) 14 21 35
229 14 return $self->_translate('time_minute_ago', $Dm);
230 }
231 21 return $self->_translate('time_minutes_ago', $Dm);
232 }
233 ▶ elsif ($Dd == 0 && $Dh == 0 && $Dm == 0 && $Ds > 5) {
Condition77%7/9
$Dd == 0 and $Dh == 0 and $Dm == 0 and $Ds > 5
!ll&&!rl&&r
2147
$Dd == 0 and $Dh == 0 and $Dm == 0
!ll&&!rl&&r
2021
$Dd == 0 and $Dh == 0
!ll&&!rl&&r
2021
MC/DC50%2/4
$Dd == 0 and $Dh == 0 and $Dm == 0 and $Ds > 5
$Dd == 0 $Dh == 0 $Dm == 0 $Ds > 5
Truth table60%3/5
A and B and C and D
A= $Dd == 0B= $Dh == 0C= $Dm == 0D= $Ds > 5
ABCDresult
0XXX0
10XX0
110X0
11100
11111
234 7 return $self->_translate('time_less_than_minute_ago');
235 }
236  
237 ## the future
238 ▶ elsif ($Dy == 0 && $DM < -12) { # ?? N_Delta_YMDHMS counts 1 year + 3 month as DM = 15 while Dy = 0
Condition33%1/3
$Dy == 0 and $DM < -12
!ll&&!rl&&r
0160
MC/DC0%0/2
$Dy == 0 and $DM < -12
$Dy == 0 $DM < -12
Truth table33%1/3
A and B
A= $Dy == 0B= $DM < -12
ABresult
0X0
100
111
239 0 ▶ if (abs(int($DM / 12)) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (abs int $DM / 12 == 1) 0 0 0
240 0 return $self->_translate('time_in_over_year');
241 }
242 }
243 elsif ($Dy < 0) {
244 0 ▶ if ($Dy == -1){
Branch: 1 branch
BranchTrueFalseTotal
if ($Dy == -1) { } 0 0 0
245 0 ▶ if ($DM < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DM < 0) 0 0 0
246 0 return $self->_translate('time_in_over_year', abs(int($DM / 12)) );
247 }
248 0 return $self->_translate('time_in_year', $Dy * -1);
249 }else{
250 0 ▶ if ($DM < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DM < 0) 0 0 0
251 0 return $self->_translate('time_in_over_years', $Dy * -1 );
252 }
253 0 return $self->_translate('time_in_years', $Dy * -1);
254 }
255 }
256 elsif ($DM < 0) {
257 0 ▶ if ($DM == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($DM == -1) 0 0 0
258 0 return $self->_translate('time_in_month', $DM * -1 );
259 }
260 0 return $self->_translate('time_in_months',$DM * -1);
261 }
262 elsif ($Dd <= -2) {
263 2 ▶ if (abs($Dd / 7) == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (abs $Dd / 7 == 1) { } 0 2 2
elsif ($Dd % 7 == 0) { } 0 2 2
264 0 return $self->_translate('time_in_week', 1);
265 }
266 elsif ($Dd % 7 == 0) {
267 0 return $self->_translate('time_in_weeks', abs(int($Dd / 7)));
268 }
269 2 return $self->_translate('time_in_num_days', ($Dd * -1) );
270 }
271 elsif ($Dd == -1) {
272 0 ▶ if ($Dh < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Dh < 0) 0 0 0
273 0 return $self->_translate('time_after_tomorrow_at', $date[3], $date[4]);
274 }
275 0 return $self->_translate('time_tomorrow_at', $date[3], $date[4]);
276 }
277 ▶ elsif ($Dd == 0 && $Dh <= -1) {
Condition33%1/3
$Dd == 0 and $Dh <= -1
!ll&&!rl&&r
0140
MC/DC0%0/2
$Dd == 0 and $Dh <= -1
$Dd == 0 $Dh <= -1
Truth table33%1/3
A and B
A= $Dd == 0B= $Dh <= -1
ABresult
0X0
100
111
278 0 ▶ if ($Dh == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Dh == -1) 0 0 0
279 0 return $self->_translate('time_in_hour_minutes', $Dh * -1, $Dm * -1);
280 }
281 0 return $self->_translate('time_in_hours_minutes', $Dh * -1, $Dm * -1);
282 }
283 ▶ elsif ($Dd == 0 && $Dh == 0 && $Dm < 0) {
Condition33%2/6
$Dd == 0 and $Dh == 0 and $Dm < 0
!ll&&!rl&&r
0140
$Dd == 0 and $Dh == 0
!ll&&!rl&&r
0014
MC/DC0%0/3
$Dd == 0 and $Dh == 0 and $Dm < 0
$Dd == 0 $Dh == 0 $Dm < 0
Truth table25%1/4
A and B and C
A= $Dd == 0B= $Dh == 0C= $Dm < 0
ABCresult
0XX0
10X0
1100
1111
284 0 ▶ if ($Dm == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Dm == -1) 0 0 0
285 0 return $self->_translate('time_in_minute', $Dm * -1);
286 }
287 0 return $self->_translate('time_in_minutes', $Dm * -1);
288 }
289 ▶ elsif ($Dd == 0 && $Dh == 0 && $Dm == 0 && $Ds < -5) {
Condition33%3/9
$Dd == 0 and $Dh == 0 and $Dm == 0 and $Ds < -5
!ll&&!rl&&r
0140
$Dd == 0 and $Dh == 0 and $Dm == 0
!ll&&!rl&&r
0014
$Dd == 0 and $Dh == 0
!ll&&!rl&&r
0014
MC/DC0%0/4
$Dd == 0 and $Dh == 0 and $Dm == 0 and $Ds < -5
$Dd == 0 $Dh == 0 $Dm == 0 $Ds < -5
Truth table20%1/5
A and B and C and D
A= $Dd == 0B= $Dh == 0C= $Dm == 0D= $Ds < -5
ABCDresult
0XXX0
10XX0
110X0
11100
11111
290 0 return $self->_translate('time_in_less_than_minute', $Ds * -1);
291 }
292  
293  
294 else {
295 14 return $self->_translate('time_just_now');
296 }
297 0 return;
298 }
299  
300 111 ▶sub _translate {
Subroutine _translate: called CC=2 SCAR=6.9
301 111 my ($self, $key, @values) = @_;
302 111 my $phrase = $translation{$self->{lang}}{$key};
303 111 ▶ return sprintf $phrase, @values if $phrase =~ /%/;
Branch: 1 branch
BranchTrueFalseTotal
if $phrase =~ /%/ 76 35 111
304 35 return $phrase;
305 }
306  
307 4 1;
308  
309 =head1 NAME
310  
311 Date::Period::Human - Human readable date periods
312  
313 =head1 SYNOPSYS
314  
315 # Create the Date::Period::Human object
316 my $d = Date::Period::Human->new();
317  
318 # Get a relative human readable date string
319 my $s = $d->human_readable('2010-01-01 02:30:42');
320  
321 # Now $s contains the relative date in human readable form, e.g. "1 year
322 # ago", "3 weeks ago", etc.
323  
324 # Use English instead of the default Dutch
325 # my $d = Date::Period::Human->new({ lang => 'en' });
326  
327 =head1 DESCRIPTION
328  
329 Creates a string of relative time. This is useful when you're showing user
330 created content, where it's nicer to show how long ago the item was posted
331 (e.g. "3 weeks ago") instead of the date and time.
332  
333 This also solves the problem where you don't know the timezone of the user who
334 is viewing the item. This is solved because you show relative time instead of
335 absolute time in most cases.
336  
337 There is one case that isn't relative.
338  
339 =head1 CLASS METHODS
340  
341 This class contains one public class method.
342  
343 =head2 new [options]
344  
345 =over 4
346  
347 =item lang
348  
349 The language you want to use. Default 'nl', can be 'en' for English.
350  
351 =item today_and_now
352  
353 An arrayref containing [ $year, $month, $day, $hour, $min, $sec ].
354  
355 Will be used as the fixed point from which the relative time will be calculated.
356  
357 =back
358  
359 =head1 METHODS
360  
361 This class contains one public method.
362  
363 =head2 $self->human_readable($mysql_date|$datetime|$epoch)
364  
365 Parses the $mysql_date and returns a human readable time string.
366  
367 Or, $datetime (a DateTime object) and returns a human readable time string.
368  
369 Or, $epoch (identified by regex /^\d+$/) and passed through gmtime().
370  
371 =head1 HOMEPAGE
372  
373 http://github.com/pstuifzand/date-period-human
374  
375 =head1 LICENSE
376  
377 This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
378  
379 =head1 AUTHOR
380  
381 Peter Stuifzand <peter@stuifzand.eu>
382  
383 =head1 COPYRIGHT
384  
385 Copyright 2010 Peter Stuifzand
386  
387 =cut
388  
↑ Summary