File Coverage

blib/lib/Time/Duration/fr.pm
Criterion Covered Total %
statement 57 60 95.0
branch 19 22 86.3
condition 5 6 83.3
subroutine 17 20 85.0
pod 11 13 84.6
total 109 121 90.0


line stmt bran cond sub pod time code
1             package Time::Duration::fr;
2 2     2   90624 use utf8;
  2         9  
  2         8  
3 2     2   46 use strict;
  2         2  
  2         31  
4 2     2   6 use warnings;
  2         6  
  2         47  
5              
6 2     2   6 use Exporter qw< import >;
  2         2  
  2         142  
7              
8             our $VERSION = '1.02';
9              
10             our @EXPORT = qw<
11             later later_exact earlier earlier_exact
12             ago ago_exact from_now from_now_exact
13             duration duration_exact concise
14             >;
15             our @EXPORT_OK = ( "interval", @EXPORT );
16              
17 2     2   6 use constant DEBUG => 0;
  2         2  
  2         99  
18 2     2   858 use Time::Duration ();
  2         2564  
  2         1294  
19              
20              
21             my %en2fr = (
22             second => ["seconde", "secondes"],
23             minute => ["minute" , "minutes" ],
24             hour => ["heure" , "heures" ],
25             day => ["jour" , "jours" ],
26             year => ["année" , "années" ],
27             );
28              
29             my %short = map { $_=> substr($_, 0, 1) } map { @$_ } values %en2fr;
30             my $comp_re = join "|", map { $_->[0] } values %en2fr;
31              
32              
33             sub concise ($) {
34 29     29 1 24 my $string = $_[0];
35              
36             #print "in : $string\n";
37 29         42 $string =~ tr/,//d;
38 29         51 $string =~ s/\bet\b//;
39 29         234 $string =~ s/\b($comp_re)s?\b/$short{$1}/g;
40 29         131 $string =~ s/\s*(\d+)\s*/$1/g;
41              
42             # restore prefixed intervals
43 29         27 $string =~ s/dans/dans /;
44 29         27 $string =~ s/il y a/il y a /;
45              
46 29         92 return $string;
47             }
48              
49             sub later {
50 68     68 1 230 interval( $_[0], $_[1], "%s plus tôt", "%s plus tard", "maintenant");
51             }
52              
53             sub later_exact {
54 31     31 1 44 interval_exact($_[0], $_[1], "%s plus tôt", "%s plus tard", "maintenant");
55             }
56              
57             sub earlier {
58 6     6 1 11 interval( $_[0], $_[1], "%s plus tard", "%s plus tôt", "maintenant");
59             }
60              
61             sub earlier_exact {
62 0     0 1 0 interval_exact($_[0], $_[1], "%s plus tard", "%s plus tôt", "maintenant");
63             }
64              
65             sub ago {
66 6     6 1 9 interval( $_[0], $_[1], 'dans %s', 'il y a %s', "maintenant");
67             }
68              
69             sub ago_exact {
70 0     0 1 0 interval_exact($_[0], $_[1], 'dans %s', 'il y a %s', "maintenant");
71             }
72              
73             sub from_now {
74 6     6 1 9 interval( $_[0], $_[1], 'il y a %s', 'dans %s', "maintenant");
75             }
76              
77             sub from_now_exact {
78 0     0 1 0 interval_exact($_[0], $_[1], 'il y a %s', 'dans %s', "maintenant");
79             }
80              
81              
82             sub duration_exact {
83 2     2 1 3 my $span = $_[0]; # interval in seconds
84 2   50     15 my $precision = int($_[1] || 0) || 2; # precision (default: 2)
85 2 50       3 return '0 seconde' unless $span;
86 2         5 _render('%s',
87             Time::Duration::_separate(abs $span));
88             }
89              
90             sub duration {
91 14     14 1 65338 my $span = $_[0]; # interval in seconds
92 14   100     56 my $precision = int($_[1] || 0) || 2; # precision (default: 2)
93 14 100       25 return '0 seconde' unless $span;
94 12         25 _render('%s',
95             Time::Duration::_approximate($precision,
96             Time::Duration::_separate(abs $span)));
97             }
98              
99              
100             sub interval_exact {
101 31     31 0 22 my $span = $_[0]; # interval, in seconds
102             # precision is ignored
103 31 100       51 my $direction = ($span <= -1) ? $_[2] # what a neg number gets
    50          
104             : ($span >= 1) ? $_[3] # what a pos number gets
105             : return $_[4]; # what zero gets
106 30         39 _render($direction,
107             Time::Duration::_separate($span));
108             }
109              
110             sub interval {
111 86     86 0 63 my $span = $_[0]; # interval, in seconds
112 86   100     262 my $precision = int($_[1] || 0) || 2; # precision (default: 2)
113 86 100       147 my $direction = ($span <= -1) ? $_[2] # what a neg number gets
    100          
114             : ($span >= 1) ? $_[3] # what a pos number gets
115             : return $_[4]; # what zero gets
116 76         108 _render($direction,
117             Time::Duration::_approximate($precision,
118             Time::Duration::_separate($span)));
119             }
120              
121              
122             sub _render {
123             # Make it into French
124 120     120   3560 my $direction = shift @_;
125             my @wheel = map {
126 120         115 ( $_->[1] == 0 ) ? () # zero wheels
127 600 100       1075 : $_->[1] . " " . $en2fr{ $_->[0] }[ $_->[1] == 1 ? 0 : 1 ]
    100          
128             } @_;
129              
130 120 50       179 return "maintenant" unless @wheel; # sanity
131              
132 120         85 my $result;
133 120 100       166 if (@wheel == 1) {
    100          
134 48         39 $result = $wheel[0];
135             }
136             elsif (@wheel == 2) {
137 34         62 $result = "$wheel[0] et $wheel[1]";
138             }
139             else {
140 38         37 $wheel[-1] = "et $wheel[-1]";
141 38         52 $result = join q{, }, @wheel;
142             }
143              
144 120         483 return sprintf($direction, $result);
145             }
146              
147              
148             1;
149              
150             __END__