File Coverage

blib/lib/DateTime/Locale/Util.pm
Criterion Covered Total %
statement 27 35 77.1
branch 11 18 61.1
condition 1 6 16.6
subroutine 7 7 100.0
pod 0 1 0.0
total 46 67 68.6


line stmt bran cond sub pod time code
1             package DateTime::Locale::Util;
2              
3 15     15   98 use strict;
  15         33  
  15         417  
4 15     15   74 use warnings;
  15         39  
  15         581  
5 15     15   81 use namespace::autoclean 0.19 -except => ['import'];
  15         415  
  15         212  
6              
7 15     15   1135 use DateTime::Locale::Data;
  15         28  
  15         330  
8              
9 15     15   71 use Exporter qw( import );
  15         184  
  15         7013  
10              
11             our $VERSION = '1.39';
12              
13             our @EXPORT_OK = 'parse_locale_code';
14              
15             # This could probably all be done in a cleaner way starting with _just_
16             # checking the known codes first and only then falling back to heuristics. But
17             # for now it's good enough to handle oddballs like be-tarask and en-polyton.
18             sub parse_locale_code {
19 11     11 0 78 my @pieces = split /-/, $_[0];
20              
21 11 50       39 return unless @pieces;
22              
23 11         580 my %codes = ( language => lc shift @pieces );
24 11 100       82 if ( @pieces == 1 ) {
    50          
    100          
25             ## no critic (ControlStructures::ProhibitCascadingIfElse, Variables::ProhibitPackageVars)
26 6 50 0     45 if ( exists $DateTime::Locale::Data::VariantCodes{ uc $pieces[0] } ) {
    100          
    50          
    0          
27 0         0 $codes{variant} = uc shift @pieces;
28             }
29             elsif (
30             exists $DateTime::Locale::Data::TerritoryCodes{ uc $pieces[0] } )
31             {
32 5         17 $codes{territory} = uc shift @pieces;
33             }
34             elsif (
35             exists $DateTime::Locale::Data::ScriptCodes{ _tc( $pieces[0] ) } )
36             {
37 1         4 $codes{script} = _tc( shift @pieces );
38             }
39             elsif ( length $pieces[0] == 2 || $pieces[0] =~ /^\d\d\d$/ ) {
40 0         0 $codes{territory} = uc shift @pieces;
41             }
42             else {
43 0         0 $codes{script} = _tc( shift @pieces );
44             }
45             }
46             elsif ( @pieces == 3 ) {
47 0         0 $codes{script} = _tc( shift @pieces );
48 0         0 $codes{territory} = uc shift @pieces;
49 0         0 $codes{variant} = uc shift @pieces;
50             }
51             elsif ( @pieces == 2 ) {
52              
53             # I don't think it's possible to have a script + variant without also
54             # having a territory.
55 2 50 33     36 if ( length $pieces[1] == 2 || $pieces[1] =~ /^\d\d\d$/ ) {
56 2         8 $codes{script} = _tc( shift @pieces );
57 2         8 $codes{territory} = uc shift @pieces;
58             }
59             else {
60 0         0 $codes{territory} = uc shift @pieces;
61 0         0 $codes{variant} = uc shift @pieces;
62             }
63             }
64              
65 11         80 return %codes;
66             }
67              
68             sub _tc {
69 4     4   22 return ucfirst lc $_[0];
70             }
71              
72             1;
73              
74             # ABSTRACT: Utility code for DateTime::Locale
75              
76             __END__
77              
78             =pod
79              
80             =encoding UTF-8
81              
82             =head1 NAME
83              
84             DateTime::Locale::Util - Utility code for DateTime::Locale
85              
86             =head1 VERSION
87              
88             version 1.39
89              
90             =head1 DESCRIPTION
91              
92             There are no user-facing parts in this module.
93              
94             =head1 SUPPORT
95              
96             Bugs may be submitted at L<https://github.com/houseabsolute/DateTime-Locale/issues>.
97              
98             There is a mailing list available for users of this distribution,
99             L<mailto:datetime@perl.org>.
100              
101             =head1 SOURCE
102              
103             The source code repository for DateTime-Locale can be found at L<https://github.com/houseabsolute/DateTime-Locale>.
104              
105             =head1 AUTHOR
106              
107             Dave Rolsky <autarch@urth.org>
108              
109             =head1 COPYRIGHT AND LICENSE
110              
111             This software is copyright (c) 2003 - 2023 by Dave Rolsky.
112              
113             This is free software; you can redistribute it and/or modify it under
114             the same terms as the Perl 5 programming language system itself.
115              
116             The full text of the license can be found in the
117             F<LICENSE> file included with this distribution.
118              
119             =cut