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