File Coverage

blib/lib/DateTime/TimeZone/Alias.pm
Criterion Covered Total %
statement 69 72 95.8
branch 34 40 85.0
condition 8 10 80.0
subroutine 14 14 100.0
pod 9 9 100.0
total 134 145 92.4


line stmt bran cond sub pod time code
1             package DateTime::TimeZone::Alias;
2              
3 10     10   1535530 use strict;
  10         30  
  10         479  
4              
5 10     10   59 use Carp qw( croak );
  10         22  
  10         1075  
6 10     10   8188 use DateTime::TimeZone::Catalog;
  10         21603  
  10         616  
7              
8 10     10   95 use vars qw( $VERSION );
  10         26  
  10         10449  
9             $VERSION = 0.06;
10              
11             sub import {
12 13     13   116 my $class = shift;
13              
14 13 100       23354 return unless @_;
15              
16 5         15 $class->set( @_ );
17             }
18              
19             sub set {
20 144     144 1 172066 my $class = shift;
21              
22 144 100       2926 croak "Can't be called without any parameters" unless @_;
23              
24 143         817 my %p = @_;
25              
26 143         837 foreach my $key ( keys %p ) {
27 147 100 33     602 if ( $class->is_alias( $p{ $key } ) ) {
    100          
    100          
    100          
    50          
    100          
28 3         2512 $DateTime::TimeZone::Catalog::LINKS{ $key } =
29             $DateTime::TimeZone::Catalog::LINKS{ $p{ $key } };
30             } elsif ( $class->is_timezone( $p{ $key } ) ) {
31 8         3346 $DateTime::TimeZone::Catalog::LINKS{ $key } = $p{ $key };
32             } elsif ( $p{ $key } eq 'floating' ) {
33 1         5 $DateTime::TimeZone::Catalog::LINKS{ $key } = 'floating';
34             } elsif ( $p{ $key } eq 'local' ) {
35 1         6 $DateTime::TimeZone::Catalog::LINKS{ $key } = 'local';
36             } elsif ( $p{ $key } eq 'UTC' || $p{ $key } eq 'Z' ) {
37 0         0 $DateTime::TimeZone::Catalog::LINKS{ $key } = 'UTC';
38             } elsif ( $p{ $key } =~ /^ ([\+\-])? (\d\d?) :? (\d\d) (?::?(\d\d))? $/x ) {
39 25   100     193 my $sign = $1 || '+';
40 25         52 my $hours = $2;
41 25         37 my $minutes = $3;
42 25   100     173 my $seconds = sprintf( "%02d", $4 || 0 );
43              
44 25         172 $DateTime::TimeZone::Catalog::LINKS{ $key } = "$sign$hours:$minutes:$seconds";
45             } else {
46 109         23834 croak "Aliases must point to a valid timezone or offset";
47             }
48             }
49             }
50              
51             sub add {
52 4     4 1 56268 my $class = shift;
53              
54 4 100       224 croak "Can't be called without any parameters" unless @_;
55              
56 3         14 my %p = @_;
57              
58 3         12 foreach my $key ( keys %p ) {
59 3 100       16 if ( ! $class->is_defined( $key )) {
60 1         6 $class->set( %p );
61             } else {
62 2         1267 croak "Attempt to redefine an alias or timezone";
63             }
64             }
65             }
66              
67             sub remove {
68 2     2 1 19209 my $class = shift;
69              
70 2 50       14 return unless @_;
71              
72 2         6 foreach my $key ( @_ ) {
73 2 100       9 if ( ! $class->is_alias( $key ) ) {
74 1         189 croak "Attempt to delete a nonexistant alias";
75             }
76              
77 1         5 delete $DateTime::TimeZone::Catalog::LINKS{ $key };
78             }
79             }
80              
81             sub value {
82 579     579 1 307528 my( $class, $key ) = @_;
83              
84 579 100       1505 if ( $class->is_alias( $key ) ) {
85 203         934 return $DateTime::TimeZone::Catalog::LINKS{ $key };
86             } else {
87 376         1231 return undef;
88             }
89             }
90              
91             sub is_defined {
92 557     557 1 538301 my( $class, $def_candidate ) = @_;
93              
94 557 100 100     1716 if (
95             $class->is_timezone( $def_candidate )
96             || $class->is_alias( $def_candidate )
97             ) {
98 556         5621 return 1;
99             } else {
100 1         5 return undef;
101             }
102             }
103              
104             sub is_alias {
105 1462     1462 1 604098 my( $class, $alias_candidate ) = @_;
106              
107 1462 100       5556 if ( exists $DateTime::TimeZone::Catalog::LINKS{ $alias_candidate } ) {
108 564         2644 return 1;
109             } else {
110 898         4853 return undef;
111             }
112             }
113              
114             sub is_timezone {
115 1255     1255 1 994970 my( $class, $tz_candidate ) = @_;
116              
117 1255 100       1010803 if ( grep( /^\Q${tz_candidate}\E$/, @DateTime::TimeZone::Catalog::ALL ) ) {
118 761         4035 return 1;
119             } else {
120 494         5264 return undef;
121             }
122             }
123              
124             sub aliases {
125 1     1 1 6 my $class = shift;
126              
127 1 50       6 return unless defined wantarray;
128              
129 1 50       4 if ( wantarray ) {
130 0         0 return %DateTime::TimeZone::Catalog::LINKS;
131             } else {
132 1         161 my %dttz_links_copy = %DateTime::TimeZone::Catalog::LINKS;
133 1         11 return \%dttz_links_copy;
134             }
135             }
136              
137             sub timezones {
138 1     1 1 16 my $class = shift;
139              
140 1 50       4 return unless defined wantarray;
141              
142 1 50       7 if ( wantarray ) {
143 0         0 return @DateTime::TimeZone::Catalog::ALL;
144             } else {
145 1         67 my @dttz_all_copy = @DateTime::TimeZone::Catalog::ALL;
146 1         4 return \@dttz_all_copy;
147             }
148             }
149              
150             1;
151              
152             __END__