File Coverage

blib/lib/Time/Duration/es.pm
Criterion Covered Total %
statement 53 56 94.6
branch 19 22 86.3
condition 5 6 83.3
subroutine 15 18 83.3
pod 0 13 0.0
total 92 115 80.0


line stmt bran cond sub pod time code
1             package Time::Duration::es;
2 3     3   106601 use strict;
  3         8  
  3         119  
3 3     3   16 use warnings;
  3         6  
  3         206  
4              
5             our $VERSION = '0.01';
6              
7 3     3   21 use base qw(Exporter);
  3         12  
  3         615  
8             our @EXPORT = qw( later later_exact earlier earlier_exact
9             ago ago_exact from_now from_now_exact
10             duration duration_exact concise );
11             our @EXPORT_OK = ('interval', @EXPORT);
12              
13 3     3   1979 use Time::Duration qw();
  3         7273  
  3         3507  
14              
15             our $MILLISECOND = 0;
16              
17             sub concise ($) {
18 29     29 0 37 my $string = $_[0];
19 29         127 $string =~ tr/,//d;
20 29         109 $string =~ s/\by\b//;
21 29         206 $string =~ s/\b(año|día|hora|minuto|segundo)s?\b/substr($1,0,1)/eg;
  44         248  
22 29         47 $string =~ s/\b(milisegundo)s?\b/ms/g;
23 29         217 $string =~ s/\s*(\d+)\s*/$1/g;
24              
25             # dirty hack to restore prefixed intervals
26 29         57 $string =~ s/en([0-9])/en $1/; # en matches momento
27 29         42 $string =~ s/hace/hace /;
28              
29 29         178 return $string;
30             }
31              
32             sub later { # ' earlier', ' later', 'right then'
33 68     68 0 434 interval( $_[0], $_[1], '%s antes', '%s después', 'al momento'); }
34             sub later_exact { # ' earlier', ' later', 'right then'
35 31     31 0 138 interval_exact($_[0], $_[1], '%s antes', '%s después', 'al momento'); }
36             sub earlier { # ' later', ' earlier', 'right then'
37 6     6 0 148 interval( $_[0], $_[1], '%s después', '%s antes', 'al momento'); }
38             sub earlier_exact { # ' later', ' earlier', 'right then'
39 0     0 0 0 interval_exact($_[0], $_[1], '%s después', '%s antes', 'al momento'); }
40             sub ago { # ' from now', ' ago', 'right now'
41 11     11 0 51 interval( $_[0], $_[1], 'en %s', 'hace %s', 'ahora'); }
42             sub ago_exact { # ' from now', ' ago', 'right now'
43 0     0 0 0 interval_exact($_[0], $_[1], 'en %s', 'hace %s', 'ahora'); }
44             sub from_now { # ' ago', ' from now', 'right now'
45 6     6 0 31 interval( $_[0], $_[1], 'hace %s', 'en %s', 'ahora'); }
46             sub from_now_exact { # ' ago', ' from now', 'right now'
47 0     0 0 0 interval_exact($_[0], $_[1], 'hace %s', 'en %s', 'ahora'); }
48              
49             sub duration_exact {
50 2     2 0 4 my $span = $_[0]; # interval in seconds
51 2   50     21 my $precision = int($_[1] || 0) || 2; # precision (default: 2)
52 2 50       7 return '0 segundos' unless $span;
53 2         7 _render('%s',
54             Time::Duration::_separate(abs $span));
55             }
56              
57             sub duration {
58 16     16 0 52 my $span = $_[0]; # interval in seconds
59 16   100     114 my $precision = int($_[1] || 0) || 2; # precision (default: 2)
60 16 100       52 return '0 segundos' unless $span;
61 14         51 _render('%s',
62             Time::Duration::_approximate($precision,
63             Time::Duration::_separate(abs $span)));
64             }
65              
66             sub interval_exact {
67 31     31 0 40 my $span = $_[0]; # interval, in seconds
68             # precision is ignored
69 31 100       109 my $direction = ($span <= -1) ? $_[2] # what a neg number gets
    50          
70             : ($span >= 1) ? $_[3] # what a pos number gets
71             : return $_[4]; # what zero gets
72 30         85 _render($direction,
73             Time::Duration::_separate($span));
74             }
75              
76             sub interval {
77 91     91 0 117 my $span = $_[0]; # interval, in seconds
78 91   100     1715 my $precision = int($_[1] || 0) || 2; # precision (default: 2)
79 91 100       416 my $direction = ($span <= -1) ? $_[2] # what a neg number gets
    100          
80             : ($span >= 1) ? $_[3] # what a pos number gets
81             : return $_[4]; # what zero gets
82 81         234 _render($direction,
83             Time::Duration::_approximate($precision,
84             Time::Duration::_separate($span)));
85             }
86              
87             my %en2es = (
88             second => ['segundo', 'segundos'],
89             minute => ['minuto' , 'minutos' ],
90             hour => ['hora' , 'horas' ],
91             day => ['día' , 'días' ],
92             year => ['año' , 'años' ],
93             );
94              
95             sub _render {
96             # Make it into Spanish
97 127     127   7038 my $direction = shift @_;
98             my @wheel = map
99             {
100 127         226 ( $_->[1] == 0) ? () # zero wheels
101 635 100       2150 : $_->[1] . ' ' . $en2es{ $_->[0] }[ $_->[1] == 1 ? 0 : 1 ]
    100          
102             }
103              
104             @_;
105              
106 127 50       437 return 'ahora' unless @wheel; # sanity
107 127         129 my $result;
108 127 100       838 if(@wheel == 1) {
    100          
109 51         62 $result = $wheel[0];
110             } elsif(@wheel == 2) {
111 37         1228 $result = "$wheel[0] y $wheel[1]";
112             } else {
113 39         81 $wheel[-1] = "y $wheel[-1]";
114 39         100 $result = join q{, }, @wheel;
115             }
116 127         1304 return sprintf($direction, $result);
117             }
118              
119             1;
120             __END__