File Coverage

blib/lib/DateTime/TimeZone/Olson.pm
Criterion Covered Total %
statement 20 20 100.0
branch 2 2 100.0
condition 1 3 33.3
subroutine 6 6 100.0
pod 1 1 100.0
total 30 32 93.7


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             DateTime::TimeZone::Olson - timezones from the Olson database
4              
5             =head1 SYNOPSIS
6              
7             use DateTime::TimeZone::Olson qw(olson_version);
8              
9             $version = olson_version;
10              
11             use DateTime::TimeZone::Olson qw(
12             olson_canonical_names olson_link_names olson_all_names
13             olson_links
14             olson_country_selection
15             );
16              
17             $names = olson_canonical_names;
18             $names = olson_link_names;
19             $names = olson_all_names;
20             $links = olson_links;
21             $countries = olson_country_selection;
22              
23             use DateTime::TimeZone::Olson qw(olson_tz);
24              
25             $tz = olson_tz("America/New_York");
26              
27             =head1 DESCRIPTION
28              
29             This module encapsulates the Olson timezone database, providing
30             L-compatible timezone objects and ancillary data. On each
31             program run this module provides access to a particular version of the
32             timezone database, determined by which version of L
33             is installed.
34              
35             =cut
36              
37             package DateTime::TimeZone::Olson;
38              
39 2     2   58483 { use 5.006; }
  2         8  
  2         85  
40 2     2   10 use warnings;
  2         4  
  2         78  
41 2     2   10 use strict;
  2         7  
  2         98  
42              
43 2         241 use Time::OlsonTZ::Data 0.201012 qw(
44             olson_version
45             olson_canonical_names olson_link_names olson_all_names
46             olson_links
47             olson_country_selection
48             olson_tzfile
49 2     2   2027 );
  2         4966  
50              
51             our $VERSION = "0.006";
52              
53 2     2   13 use parent "Exporter";
  2         3  
  2         8  
54             our @EXPORT_OK = qw(
55             olson_version
56             olson_canonical_names olson_link_names olson_all_names
57             olson_links
58             olson_country_selection
59             olson_tz
60             );
61              
62             =head1 FUNCTIONS
63              
64             =head2 Basic information
65              
66             =over
67              
68             =item olson_version
69              
70             Returns the version number of the database to which this module is
71             providing access. Version numbers for the Olson database currently
72             consist of a year number and a lowercase letter, such as "C<2010k>";
73             they are not guaranteed to retain this format in the future.
74              
75             =back
76              
77             =head2 Zone metadata
78              
79             =over
80              
81             =item olson_canonical_names
82              
83             Returns the set of timezone names that this version of the database
84             defines as canonical. These are the timezone names that are directly
85             associated with a set of observance data. The return value is a reference
86             to a hash, in which the keys are the canonical timezone names and the
87             values are all C.
88              
89             =item olson_link_names
90              
91             Returns the set of timezone names that this version of the database
92             defines as links. These are the timezone names that are aliases for
93             other names. The return value is a reference to a hash, in which the
94             keys are the link timezone names and the values are all C.
95              
96             =item olson_all_names
97              
98             Returns the set of timezone names that this version of the
99             database defines. These are the L and the
100             L. The return value is a reference to a hash, in
101             which the keys are the timezone names and the values are all C.
102              
103             =item olson_links
104              
105             Returns details of the timezone name links in this version of the
106             database. Each link defines one timezone name as an alias for some
107             other timezone name. The return value is a reference to a hash, in
108             which the keys are the aliases and each value is the canonical name of
109             the timezone to which that alias refers. All such canonical names can
110             be found in the L hash.
111              
112             =item olson_country_selection
113              
114             Returns information about how timezones relate to countries, intended
115             to aid humans in selecting a geographical timezone.
116              
117             The return value is a reference to a hash, keyed by (ISO 3166 alpha-2
118             uppercase) country code. The value for each country is a hash containing
119             these values:
120              
121             =over
122              
123             =item B
124              
125             The ISO 3166 alpha-2 uppercase country code.
126              
127             =item B
128              
129             An English name for the country, possibly in a modified form, optimised
130             to help humans find the right entry in alphabetical lists. This is
131             not necessarily identical to the country's standard short or long name.
132             (For other forms of the name, consult a database of countries, keying
133             by the country code.)
134              
135             =item B
136              
137             Information about the regions of the country that use distinct
138             timezones. This is a hash, keyed by English description of the region.
139             The description is empty if there is only one region. The value for
140             each region is a hash containing these values:
141              
142             =over
143              
144             =item B
145              
146             Brief English description of the region, used to distinguish between
147             the regions of a single country. Empty string if the country has only
148             one region for timezone purposes. (This is the same string used as the
149             key in the B hash.)
150              
151             =item B
152              
153             Name of the Olson timezone used in this region. The named timezone is
154             guaranteed to exist in the database, but not necessarily as a canonical
155             name (it may be a link). Typically, where there are aliases or identical
156             canonical zones, a name is chosen that refers to a location in the
157             country of interest.
158              
159             =item B
160              
161             Geographical coordinates of some point within the location referred to in
162             the timezone name. This is a latitude and longitude, in ISO 6709 format.
163              
164             =back
165              
166             =back
167              
168             This data structure is intended to help a human select the appropriate
169             timezone based on political geography, specifically working from a
170             selection of country. It is of essentially no use for any other purpose.
171             It is not strictly guaranteed that every geographical timezone in the
172             database is listed somewhere in this structure, so it is of limited use
173             in providing information about an already-selected timezone. It does
174             not include non-geographic timezones at all. It also does not claim
175             to be a comprehensive list of countries, and does not make any claims
176             regarding the political status of any entity listed: the "country"
177             classification is loose, and used only for identification purposes.
178              
179             =back
180              
181             =head2 Zone data
182              
183             =over
184              
185             =item olson_tz(NAME)
186              
187             Returns a reference to an object that encapsulates the timezone
188             named I in the Olson database and which implements the
189             L interface. Cs if the name does not exist
190             in this version of the database. Currently the object is of class
191             L, but this is not guaranteed.
192              
193             =cut
194              
195             my %cache_tz;
196             sub olson_tz($) {
197 8     8 1 112360 my($tzname) = @_;
198 8   33     53 return $cache_tz{$tzname} ||= do {
199 8         1121 require DateTime::TimeZone::Tzfile;
200 8         34507 DateTime::TimeZone::Tzfile->VERSION(0.010);
201 8 100       60 DateTime::TimeZone::Tzfile->new(
202             filename => olson_tzfile($tzname),
203             name => $tzname,
204             category => ($tzname =~ m#\A([^/]+)/# ? "$1" : undef),
205             is_olson => 1,
206             );
207             };
208             }
209              
210             =back
211              
212             =head1 BUGS
213              
214             Parts of the Olson timezone database are liable to be inaccurate.
215             See L for discussion. Frequent updates of
216             the installation of L is recommended, to keep it
217             accurate for current dates.
218              
219             =head1 SEE ALSO
220              
221             L,
222             L,
223             L,
224             L
225              
226             =head1 AUTHOR
227              
228             Andrew Main (Zefram)
229              
230             =head1 COPYRIGHT
231              
232             Copyright (C) 2010, 2011, 2012, 2013
233             Andrew Main (Zefram)
234              
235             =head1 LICENSE
236              
237             This module is free software; you can redistribute it and/or modify it
238             under the same terms as Perl itself.
239              
240             =cut
241              
242             1;