File Coverage

blib/lib/DateTime/Locale/FromData.pm
Criterion Covered Total %
statement 113 118 95.7
branch 6 8 75.0
condition 5 10 50.0
subroutine 33 38 86.8
pod 8 24 33.3
total 165 198 83.3


line stmt bran cond sub pod time code
1              
2             use strict;
3 15     15   94 use warnings;
  15         29  
  15         424  
4 15     15   73 use namespace::autoclean;
  15         29  
  15         416  
5 15     15   69  
  15         26  
  15         99  
6             use DateTime::Locale::Util qw( parse_locale_code );
7 15     15   6844 use Params::ValidationCompiler 0.13 qw( validation_for );
  15         31  
  15         1155  
8 15     15   7211 use Specio::Declare;
  15         327926  
  15         6963  
9 15     15   15237 use Storable qw( dclone );
  15         554297  
  15         107  
10 15     15   2779  
  15         31  
  15         2150  
11             our $VERSION = '1.37';
12              
13             my @FormatLengths;
14              
15             BEGIN {
16             my @methods = qw(
17 15     15   120 code
18             name
19             language
20             script
21             territory
22             variant
23             native_name
24             native_language
25             native_script
26             native_territory
27             native_variant
28             am_pm_abbreviated
29             date_format_full
30             date_format_long
31             date_format_medium
32             date_format_short
33             time_format_full
34             time_format_long
35             time_format_medium
36             time_format_short
37             day_format_abbreviated
38             day_format_narrow
39             day_format_wide
40             day_stand_alone_abbreviated
41             day_stand_alone_narrow
42             day_stand_alone_wide
43             month_format_abbreviated
44             month_format_narrow
45             month_format_wide
46             month_stand_alone_abbreviated
47             month_stand_alone_narrow
48             month_stand_alone_wide
49             quarter_format_abbreviated
50             quarter_format_narrow
51             quarter_format_wide
52             quarter_stand_alone_abbreviated
53             quarter_stand_alone_narrow
54             quarter_stand_alone_wide
55             era_abbreviated
56             era_narrow
57             era_wide
58             default_date_format_length
59             default_time_format_length
60             first_day_of_week
61             version
62             glibc_datetime_format
63             glibc_date_format
64             glibc_date_1_format
65             glibc_time_format
66             glibc_time_12_format
67             );
68              
69             for my $meth (@methods) {
70 15         35 my $sub = sub { $_[0]->{$meth} };
71 750     28392   2092 ## no critic (TestingAndDebugging::ProhibitNoStrict)
  28392         4239321  
72             no strict 'refs';
73 15     15   103 *{$meth} = $sub;
  15         28  
  15         2019  
74 750         934 }
  750         2370  
75              
76             @FormatLengths = qw( short medium long full );
77 15         46  
78             for my $length (@FormatLengths) {
79 15         30 my $meth = 'datetime_format_' . $length;
80 60         112 my $key = 'computed_' . $meth;
81 60         108  
82             my $sub = sub {
83             my $self = shift;
84 6     6   24  
85             return $self->{$key} if exists $self->{$key};
86 6 100       20  
87             return $self->{$key} = $self->_make_datetime_format($length);
88 5         15 };
89 60         182  
90             ## no critic (TestingAndDebugging::ProhibitNoStrict)
91             no strict 'refs';
92 15     15   91 *{$meth} = $sub;
  15         27  
  15         616  
93 60         78 }
  60         16671  
94             }
95              
96             my $class = shift;
97             my $data = shift;
98 861     861 0 1875  
99 861         1394 return bless {
100             %{$data},
101             default_date_format_length => 'medium',
102 861         1452 default_time_format_length => 'medium',
  861         19877  
103             locale_data => $data
104             }, $class;
105             }
106              
107             return $_[0]->date_format_medium;
108             }
109              
110 2     2 0 8 return $_[0]->time_format_medium;
111             }
112              
113             return $_[0]->{datetime_format_medium};
114 2     2 0 8 }
115              
116             return $_[0]->datetime_format_medium;
117             }
118 0     0 0 0  
119             my $self = shift;
120             my $length = shift;
121              
122 2     2 0 19 my $dt_key = 'datetime_format_' . $length;
123             my $date_meth = 'date_format_' . $length;
124             my $time_meth = 'time_format_' . $length;
125              
126 5     5   8 my $dt_format = $self->{$dt_key};
127 5         8 $dt_format =~ s/\{0\}/$self->$time_meth/eg;
128             $dt_format =~ s/\{1\}/$self->$date_meth/eg;
129 5         12  
130 5         7 return $dt_format;
131 5         8 }
132              
133 5         12 my $length = enum( values => [qw( full long medium short )] );
134 5         19 my $validator = validation_for(
  5         13  
135 5         15 name => '_check_length_parameter',
  5         12  
136             name_is_optional => 1,
137 5         25 params => [ { type => $length } ],
138             );
139              
140             my $self = shift;
141             my ($l) = $validator->(@_);
142              
143             $self->{default_date_format_length} = lc $l;
144             }
145              
146             my $self = shift;
147             my ($l) = $validator->(@_);
148 1     1 0 2  
149 1         26 $self->{default_time_format_length} = lc $l;
150             }
151 1         14  
152             my %formats;
153             for my $length (@FormatLengths) {
154             my $meth = 'date_format_' . $length;
155 1     1 0 31 $formats{$length} = $_[0]->$meth;
156 1         22 }
157             return \%formats;
158 1         10 }
159              
160             my %formats;
161             for my $length (@FormatLengths) {
162 824     824 0 1524 my $meth = 'time_format_' . $length;
163 824         2015 $formats{$length} = $_[0]->$meth;
164 3296         5488 }
165 3296         7229 return \%formats;
166             }
167 824         2934  
168             my $self = shift;
169              
170             $self->{computed_available_formats}
171 824     824 0 1353 ||= [ sort keys %{ $self->_available_formats } ];
172 824         1954  
173 3296         5812 return @{ $self->{computed_available_formats} };
174 3296         7046 }
175              
176 824         2756 my $self = shift;
177             my $for = shift;
178              
179             return $self->_available_formats->{$for};
180 2     2 1 601 }
181              
182              
183 2   50     13 my $self = shift;
  2         7  
184              
185 2         6 return $self->{prefers_24_hour_time}
  2         487  
186             if exists $self->{prefers_24_hour_time};
187              
188             $self->{prefers_24_hour_time} = $self->time_format_short =~ /h|K/ ? 0 : 1;
189 97     97 1 30136 }
190 97         124  
191             my $self = shift;
192 97         144 return ( $self->{parsed_code} ||= { parse_locale_code( $self->code ) } )
193             ->{language};
194             }
195 99     99   404  
196             my $self = shift;
197             return ( $self->{parsed_code} ||= { parse_locale_code( $self->code ) } )
198 1     1 1 7 ->{script};
199             }
200              
201 1 50       5 my $self = shift;
202             return ( $self->{parsed_code} ||= { parse_locale_code( $self->code ) } )
203 1 50       3 ->{territory};
204             }
205              
206             my $self = shift;
207 2     2 1 9 return ( $self->{parsed_code} ||= { parse_locale_code( $self->code ) } )
208             ->{variant};
209 2   50     13 }
210              
211             $_[0]->code;
212             }
213 2     2 1 716  
214             $_[0]->language_code;
215 2   50     15 }
216              
217             $_[0]->script_code;
218             }
219 3     3 1 652  
220             $_[0]->territory_code;
221 3   50     20 }
222              
223             $_[0]->variant_code;
224             }
225 2     2 1 9  
226             return %{ dclone( $_[0]->{locale_data} ) };
227 2   50     13 }
228              
229             my $self = shift;
230             my $cloning = shift;
231 1     1 0 375  
232             return if $cloning;
233              
234             return $self->code;
235 0     0 0 0 }
236              
237             my $self = shift;
238             shift;
239 0     0 0 0 my $serialized = shift;
240              
241             require DateTime::Locale;
242             my $obj = DateTime::Locale->load($serialized);
243 0     0 0 0  
244             %{$self} = %{$obj};
245              
246             return $self;
247 0     0 0 0 }
248              
249             1;
250              
251 3     3 1 101 # ABSTRACT: Class for locale objects instantiated from pre-defined data
  3         414  
252              
253              
254             =pod
255 2     2 0 36  
256 2         4 =encoding UTF-8
257              
258 2 100       230 =head1 NAME
259              
260 1         4 DateTime::Locale::FromData - Class for locale objects instantiated from pre-defined data
261              
262             =head1 VERSION
263              
264 1     1 0 5407 version 1.37
265 1         2  
266 1         2 =head1 SYNOPSIS
267              
268 1         7 my $locale = DateTime::Locale::FromData->new(%lots_of_data)
269 1         6  
270             =head1 DESCRIPTION
271 1         3  
  1         15  
  1         12  
272             This class is used to represent locales instantiated from the data in the
273 1         8 DateTime::Locale::Data module.
274              
275             =head1 METHODS
276              
277             This class provides the following methods:
278              
279             =head2 $locale->code
280              
281             The complete locale id, something like "en-US".
282              
283             =head2 $locale->language_code
284              
285             The language portion of the code, like "en".
286              
287             =head2 $locale->script_code
288              
289             The script portion of the code, like "Hant".
290              
291             =head2 $locale->territory_code
292              
293             The territory portion of the code, like "US".
294              
295             =head2 $locale->variant_code
296              
297             The variant portion of the code, like "POSIX".
298              
299             =head2 $locale->name
300              
301             The locale's complete name, which always includes at least a language
302             component, plus optional territory and variant components. Something like
303             "English United States". The value returned will always be in English.
304              
305             =head2 $locale->language
306              
307             =head2 $locale->script
308              
309             =head2 $locale->territory
310              
311             =head2 $locale->variant
312              
313             The relevant component from the locale's complete name, like "English" or
314             "United States".
315              
316             =head2 $locale->native_name
317              
318             The locale's complete name in localized form as a UTF-8 string.
319              
320             =head2 $locale->native_language
321              
322             =head2 $locale->native_script
323              
324             =head2 $locale->native_territory
325              
326             =head2 $locale->native_variant
327              
328             The relevant component from the locale's complete native name as a UTF-8
329             string.
330              
331             =head2 $locale->month_format_wide
332              
333             =head2 $locale->month_format_abbreviated
334              
335             =head2 $locale->month_format_narrow
336              
337             =head2 $locale->month_stand_alone_wide
338              
339             =head2 $locale->month_stand_alone_abbreviated
340              
341             =head2 $locale->month_stand_alone_narrow
342              
343             =head2 $locale->day_format_wide
344              
345             =head2 $locale->day_format_abbreviated
346              
347             =head2 $locale->day_format_narrow
348              
349             =head2 $locale->day_stand_alone_wide
350              
351             =head2 $locale->day_stand_alone_abbreviated
352              
353             =head2 $locale->day_stand_alone_narrow
354              
355             =head2 $locale->quarter_format_wide
356              
357             =head2 $locale->quarter_format_abbreviated
358              
359             =head2 $locale->quarter_format_narrow
360              
361             =head2 $locale->quarter_stand_alone_wide
362              
363             =head2 $locale->quarter_stand_alone_abbreviated
364              
365             =head2 $locale->quarter_stand_alone_narrow
366              
367             =head2 $locale->am_pm_abbreviated
368              
369             =head2 $locale->era_wide
370              
371             =head2 $locale->era_abbreviated
372              
373             =head2 $locale->era_narrow
374              
375             These methods all return an array reference containing the specified data.
376              
377             The methods with "format" in the name should return strings that can be used a
378             part of a string, like "the month of July". The stand alone values are for use
379             in things like calendars as opposed to a sentence.
380              
381             The narrow forms may not be unique (for example, in the day column heading for
382             a calendar it's okay to have "T" for both Tuesday and Thursday).
383              
384             The wide name should always be the full name of thing in question. The narrow
385             name should be just one or two characters.
386              
387             B<These methods return a reference to the data stored in the locale object. If
388             you change this reference's contents, this will affect the data in the locale
389             object! You should clone the data first if you want to modify it.>
390              
391             =head2 $locale->date_format_full
392              
393             =head2 $locale->date_format_long
394              
395             =head2 $locale->date_format_medium
396              
397             =head2 $locale->date_format_short
398              
399             =head2 $locale->time_format_full
400              
401             =head2 $locale->time_format_long
402              
403             =head2 $locale->time_format_medium
404              
405             =head2 $locale->time_format_short
406              
407             =head2 $locale->datetime_format_full
408              
409             =head2 $locale->datetime_format_long
410              
411             =head2 $locale->datetime_format_medium
412              
413             =head2 $locale->datetime_format_short
414              
415             These methods return strings appropriate for the C<< DateTime->format_cldr >>
416             method.
417              
418             =head2 $locale->format_for($name)
419              
420             These are accessed by passing a name to C<< $locale->format_for(...) >>, where
421             the name is a CLDR-style format specifier.
422              
423             The return value is a string suitable for passing to C<< $dt->format_cldr >>,
424             so you can do something like this:
425              
426             print $dt->format_cldr( $dt->locale->format_for('MMMdd') )
427              
428             which for the "en" locale would print out something like "08 Jul".
429              
430             Note that the localization may also include additional text specific to the
431             locale. For example, the "MMMMd" format for the "zh" locale includes the
432             Chinese characters for "day" (日) and month (月), so you get something like
433             "S<8月23日>".
434              
435             =head2 $locale->available_formats
436              
437             This should return a list of all the format names that could be passed to C<<
438             $locale->format_for >>.
439              
440             See the documentation for individual locales for details and examples of these
441             formats. The format names that are available vary by locale.
442              
443             =head2 $locale->glibc_datetime_format
444              
445             =head2 $locale->glibc_date_format
446              
447             =head2 $locale->glibc_date_1_format
448              
449             =head2 $locale->glibc_time_format
450              
451             =head2 $locale->glibc_time_12_format
452              
453             These methods return strings appropriate for the C<< DateTime->strftime >>
454             method. However, you are strongly encouraged to use the other format methods,
455             which use the CLDR format data. They are primarily included for the benefit for
456             L<DateTime::Format::Strptime>.
457              
458             =head2 $locale->version
459              
460             The CLDR version from which this locale was generated.
461              
462             =head2 $locale->prefers_24_hour_time
463              
464             Returns a boolean indicating whether or not the locale prefers 24-hour time.
465              
466             =head2 $locale->first_day_of_week
467              
468             Returns a number from 1 to 7 indicating the I<local> first day of the week,
469             with Monday being 1 and Sunday being 7.
470              
471             =head2 $locale->locale_data
472              
473             Returns a clone of the original data used to create this locale as a hash. This
474             is here to facilitate creating custom locales via
475             C<DateTime::Locale->register_data_locale>.
476              
477             =head1 SUPPORT
478              
479             Bugs may be submitted at L<https://github.com/houseabsolute/DateTime-Locale/issues>.
480              
481             There is a mailing list available for users of this distribution,
482             L<mailto:datetime@perl.org>.
483              
484             =head1 SOURCE
485              
486             The source code repository for DateTime-Locale can be found at L<https://github.com/houseabsolute/DateTime-Locale>.
487              
488             =head1 AUTHOR
489              
490             Dave Rolsky <autarch@urth.org>
491              
492             =head1 COPYRIGHT AND LICENSE
493              
494             This software is copyright (c) 2003 - 2022 by Dave Rolsky.
495              
496             This is free software; you can redistribute it and/or modify it under
497             the same terms as the Perl 5 programming language system itself.
498              
499             The full text of the license can be found in the
500             F<LICENSE> file included with this distribution.
501              
502             =cut