File Coverage

blib/lib/DateTime/TimeZone/Olson.pm
Criterion Covered Total %
statement 19 19 100.0
branch 2 2 100.0
condition 1 3 33.3
subroutine 6 6 100.0
pod 1 1 100.0
total 29 31 93.5


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