blib/lib/DateTimeX/Format/Ago.pm

filter: Statementstmt 93.4% 57 / 61 Branchbran 66.6% 12 / 18 Conditioncond 42.8% 3 / 7 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 92.8% 13 / 14 Podpod 100% 3 / 3 totaltotal 83.9% 89 / 106 CC 14 SCAR 14.0
CC
14
Cov
84%
format_datetime
8.1
_strings
4.7
new
3.5
CRAP 4.1
↑ Summary
1 6 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 package DateTimeX::Format::Ago;
7  
8 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
9 6 $DateTimeX::Format::Ago::AUTHORITY = 'cpan:TOBYINK';
10 6 $DateTimeX::Format::Ago::VERSION = '0.005';
11 }
12  
13 6 ▶use Carp qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use DateTime;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 6 our %__;
18 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
19 $__{ENG} = $__{EN} = {
20 6 future => "in the future",
21 recent => "just now",
22 years => ["%d years ago", "a year ago"],
23 months => ["%d months ago", "a month ago"],
24 weeks => ["%d weeks ago", "a week ago"],
25 days => ["%d days ago", "a day ago"],
26 hours => ["%d hours ago", "an hour ago"],
27 minutes => ["%d minutes ago", "a minute ago"],
28 };
29 $__{GER} = $__{DEU} = $__{DE} = {
30 6 future => "in der Zukunft",
31 recent => "gerade jetzt",
32 years => ["vor %d Jahren", "vor einem Jahr"],
33 months => ["vor %d Monaten", "vor einem Monat"],
34 weeks => ["vor %d Wochen", "vor einer Woche"],
35 days => ["vor %d Tagen", "vor einem Tag"],
36 hours => ["vor %d Stunden", "vor einer Stunde"],
37 minutes => ["vor %d Minuten", "vor einer Minute"],
38 };
39 $__{FRE} = $__{FRA} = $__{FR} = {
40 6 future => "à l'avenir",
41 recent => "récemment",
42 years => ["il y a %d ans", "il y a un an"],
43 months => ["il y a %d mois", "il y a un mois"],
44 weeks => ["il y a %d semaines", "il y a une semaine"],
45 days => ["il y a %d jours", "il y a un jour"],
46 hours => ["il y a %d heures", "il y a une heure"],
47 minutes => ["il y a %d minutes", "il y a une minute"],
48 };
49 $__{KOR} = $__{KO} = {
50 6 future => "잠시 후",
51 recent => "방금 전",
52 years => ["%d년 전", "작년"],
53 months => ["%d개월 전", "지난달"],
54 weeks => ["%d주 전", "지난주"],
55 days => ["%d일 전", "어제"],
56 hours => ["%d시간 전", "1시간 전"],
57 minutes => ["%d분 전", "1분 전"]
58 }; #"# fix for Scite syntax highlighter
59 $__{SPA} = $__{ES} = {
60 6 future => "en el futuro",
61 recent => "ahora mismo",
62 years => ["hace %d años", "hace un año"],
63 months => ["hace %d meses", "hace un mes"],
64 weeks => ["hace %d semanas", "hace una semana"],
65 days => ["hace %d días", "hace un día"],
66 hours => ["hace %d horas", "hace una hora"],
67 minutes => ["hace %d minutos", "hace un minuto"],
68 };
69 $__{POR} = $__{PT} = {
70 6 future => "no futuro",
71 recent => "agora",
72 years => ["%d anos atrás", "há um ano"],
73 months => ["%d meses atrás", "há um mês"],
74 weeks => ["%d semanas atrás", "há uma semana"],
75 days => ["%d dias atrás", "há um dia"],
76 hours => ["%d horas atrás", "há uma hora"],
77 minutes => ["%d minutos atrás", "há um minuto"],
78 };
79 $__{IND} = $__{ID} = {
80 6 future => "yang akan datang",
81 recent => "baru saja",
82 years => ["%d tahun yang lalu", "setahun yang lalu"],
83 months => ["%d bulan yang lalu", "sebulan yang lalu"],
84 weeks => ["%d minggu yang lalu", "seminggu yang lalu"],
85 days => ["%d hari yang lalu", "sehari yang lalu"],
86 hours => ["%d jam yang lalu", "sejam yang lalu"],
87 minutes => ["%d menit yang lalu", "semenit yang lalu"],
88 };
89 }
90  
91 sub new
92 4 ▶{
Subroutine new: called CC=3 SCAR=12.5
Pod documented
93 4 my ($class, %options) = @_;
94 4 ▶ $options{'language'} //= ($ENV{LANG} // 'en');
Condition25%1/4
$options{"language"} //= $ENV{"LANG"} // "en"
l!l
40
$ENV{"LANG"} // "en"
l!l
00
MC/DC0%0/1
$options{"language"} //= $ENV{"LANG"} // "en"
$options{"language"}
Truth table50%1/2
A
A= $options{"language"}
Aresult
11
00
95 4 $options{'language'} =~ s/\..*$//;
96 4 bless \%options, $class;
97 }
98  
99 sub parse_datetime
100 0 ▶{
Subroutine parse_datetime: not called CC=1 SCAR=6.9
Pod documented
101 0 Carp::croak(sprintf("%s doesn't do parsing", __PACKAGE__));
102 }
103  
104 sub _now
105 601 ▶{
Subroutine _now: called CC=2 SCAR=8.1
106 601 ▶ if ($INC{'Time/HiRes.pm'})
Branch: 1 branch
BranchTrueFalseTotal
if ($INC{"Time/HiRes.pm"}) 601 0 601
107 {
108 601 return 'DateTime'->from_epoch(epoch => Time::HiRes::time());
109 }
110 0 return 'DateTime'->now;
111 }
112  
113 sub format_datetime
114 601 ▶{
Subroutine format_datetime: called CC=8 SCAR=20.9
Pod documented
115 601 my ($self, $datetime) = @_;
116 601 ▶ $self = $self->new unless blessed($self);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 601 601
117
118 601 my $now = $self->_now;
119 601 my $delta = $now - $datetime;
120 601 my %strings = $self->_strings;
121
122 601 ▶ return $strings{future} if $delta->is_negative;
Branch: 1 branch
BranchTrueFalseTotal
if $delta->is_negative 21 580 601
123
124 580 foreach my $unit (qw/years months weeks days hours minutes/)
125 {
126 $strings{$unit}[0] = uc "%d $unit ago"
127 2844 ▶ unless defined $strings{$unit}[0];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $strings{$unit}[0] 0 2844 2844
128
129 2844 my $n = $delta->in_units($unit);
130
131 2844 ▶ if ($n > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($n > 0) 405 2439 2844
132 {
133 405 ▶ if (exists $strings{$unit}[$n]
Branch: 1 branch
BranchTrueFalseTotal
if (exists $strings{$unit}[$n] and defined $strings{$unit}[$n]) 45 360 405
Condition66%2/3
exists $strings{$unit}[$n] and defined $strings{$unit}[$n]
!ll&&!rl&&r
360045
MC/DC50%1/2
exists $strings{$unit}[$n] and defined $strings{$unit}[$n]
exists $strings{$unit}[$n] defined $strings{$unit}[$n]
Truth table66%2/3
A and B
A= exists $strings{$unit}[$n]B= defined $strings{$unit}[$n]
ABresult
0X0
100
111
134 and defined $strings{$unit}[$n])
135 {
136 45 return sprintf($strings{$unit}[$n], $n);
137 }
138
139 360 return sprintf($strings{$unit}[0], $n);
140 }
141 }
142
143 175 return $strings{recent};
144 }
145  
146 sub _strings
147 601 ▶{
Subroutine _strings: called CC=4 SCAR=15.5
148 601 my ($self) = @_;
149 601 ▶ $self = $self->new unless blessed($self);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($self) 0 601 601
150
151 601 my $language = uc $self->{language};
152 601 ▶ while (length $language)
Branch: 1 branch
BranchTrueFalseTotal
if (length $language) 601 0 601
153 {
154 601 ▶ return %{$__{$language}} if defined $__{$language};
Branch: 1 branch
BranchTrueFalseTotal
if defined $DateTimeX::Format::Ago::__{$language} 601 0 601
155 0 $language =~ s/(^|[_-])([^_-]*)$//;
156 }
157
158 0 Carp::croak(sprintf("%s doesn't know about language '%s'", __PACKAGE__, $self->{language}));
159 }
160  
161 6 __PACKAGE__
162 __END__
↑ Summary