File Coverage

blib/lib/Net/DNS/GuessTZ.pm
Criterion Covered Total %
statement 20 42 47.6
branch 1 18 5.5
condition 0 7 0.0
subroutine 7 11 63.6
pod 1 1 100.0
total 29 79 36.7


line stmt bran cond sub pod time code
1 1     1   84472 use strict;
  1         10  
  1         26  
2 1     1   5 use warnings;
  1         1  
  1         46  
3             package Net::DNS::GuessTZ 0.005;
4             # ABSTRACT: guess the time zone of a host
5              
6             #pod =head1 SYNOPSIS
7             #pod
8             #pod use Net::DNS::GuessTZ qw(tz_from_host);
9             #pod
10             #pod my $tz = tz_from_host('cr.yp.to');
11             #pod
12             #pod =head1 DESCRIPTION
13             #pod
14             #pod Brazenly stolen from L<Plagger::Plugin::Filter::GuessTimeZoneByDomain>, this
15             #pod module makes an effort to guess an appropriate time zone for a given host. It
16             #pod will look up the location of the IP addresses owner and it will also consider
17             #pod the country code top-level domain, if the host is under one.
18             #pod
19             #pod =head1 CAVEATS
20             #pod
21             #pod This is fine if you don't really care too much about being correct. It's
22             #pod probably better than just always assuming local time.
23             #pod
24             #pod Still, if possible, ask the user for his time zone when you can!
25             #pod
26             #pod =cut
27              
28 1     1   438 use DateTime::TimeZone 0.51;
  1         153914  
  1         27  
29 1     1   7 use List::Util ();
  1         1  
  1         16  
30              
31 1     1   496 use Sub::Exporter::Util;
  1         11198  
  1         7  
32 1         4 use Sub::Exporter -setup => {
33             exports => [ tz_from_host => Sub::Exporter::Util::curry_method ],
34 1     1   172 };
  1         1  
35              
36             my $ICF;
37             sub _icf {
38 0   0 0   0 $ICF ||= do {
39 0         0 require IP::Country::Fast;
40 0         0 IP::Country::Fast->new;
41             };
42             }
43              
44             sub _all_tz_from_cctld {
45 0     0   0 my ($self, $host) = @_;
46 0 0       0 return unless my ($cctld) = $host =~ /\.(\w{2})\z/;
47 0         0 DateTime::TimeZone->names_in_country($cctld);
48             }
49              
50             sub _all_tz_from_ip {
51 0     0   0 my ($self, $host) = @_;
52 0 0       0 return unless my $cc = $self->_icf->inet_atocc($host);
53 0         0 my @names = DateTime::TimeZone->names_in_country($cc);
54             }
55              
56             #pod =method tz_from_host
57             #pod
58             #pod my $tz_name = Net::DNS::GuessTZ->tz_from_host($hostname, %arg);
59             #pod
60             #pod This routine returns a guess at the given host's time zone, or false if no
61             #pod guess can be made.
62             #pod
63             #pod Valid arguments are:
64             #pod
65             #pod priority - which method to give priority to: "cc" or "ip"; default: ip
66             #pod ip_country - whether to check the IP address's owner with IP::Country;
67             #pod defaults to true
68             #pod
69             #pod Unlike the Plagger plugin, this routine will gladly make a guess when the
70             #pod country it finds has more than three time zones.
71             #pod
72             #pod =cut
73              
74             sub tz_from_host {
75 2     2 1 111 my ($self, $host, $arg) = @_;
76 2 50       12 return unless $host;
77 0   0       $arg ||= {};
78 0 0         $arg->{ip_country} = 1 unless exists $arg->{ip_country};
79              
80 0           my %result;
81              
82 0           my @names = $self->_all_tz_from_cctld($host);
83 0 0         $result{cc} = $names[0] if @names; # and @names <= 3;
84              
85 0 0         if ($arg->{ip_country}) {
86 0           my @names = $self->_all_tz_from_ip($host);
87 0 0         $result{ip} = $names[0] if @names; # if @names <= 3;
88             }
89              
90             my @cand = ($arg->{priority}||'ip') eq 'cc'
91             ? @result{qw(cc ip)}
92 0 0 0       : @result{qw(ip cc)};
93              
94 0 0   0     if (my $tz = List::Util::first { defined } @cand) {
  0            
95 0           return $tz;
96             } else {
97 0           return;
98             }
99             }
100              
101             1;
102              
103             __END__
104              
105             =pod
106              
107             =encoding UTF-8
108              
109             =head1 NAME
110              
111             Net::DNS::GuessTZ - guess the time zone of a host
112              
113             =head1 VERSION
114              
115             version 0.005
116              
117             =head1 SYNOPSIS
118              
119             use Net::DNS::GuessTZ qw(tz_from_host);
120              
121             my $tz = tz_from_host('cr.yp.to');
122              
123             =head1 DESCRIPTION
124              
125             Brazenly stolen from L<Plagger::Plugin::Filter::GuessTimeZoneByDomain>, this
126             module makes an effort to guess an appropriate time zone for a given host. It
127             will look up the location of the IP addresses owner and it will also consider
128             the country code top-level domain, if the host is under one.
129              
130             =head1 PERL VERSION
131              
132             This library should run on perls released even a long time ago. It should work
133             on any version of perl released in the last five years.
134              
135             Although it may work on older versions of perl, no guarantee is made that the
136             minimum required version will not be increased. The version may be increased
137             for any reason, and there is no promise that patches will be accepted to lower
138             the minimum required perl.
139              
140             =head1 METHODS
141              
142             =head2 tz_from_host
143              
144             my $tz_name = Net::DNS::GuessTZ->tz_from_host($hostname, %arg);
145              
146             This routine returns a guess at the given host's time zone, or false if no
147             guess can be made.
148              
149             Valid arguments are:
150              
151             priority - which method to give priority to: "cc" or "ip"; default: ip
152             ip_country - whether to check the IP address's owner with IP::Country;
153             defaults to true
154              
155             Unlike the Plagger plugin, this routine will gladly make a guess when the
156             country it finds has more than three time zones.
157              
158             =head1 CAVEATS
159              
160             This is fine if you don't really care too much about being correct. It's
161             probably better than just always assuming local time.
162              
163             Still, if possible, ask the user for his time zone when you can!
164              
165             =head1 AUTHOR
166              
167             Ricardo SIGNES <cpan@semiotic.systems>
168              
169             =head1 CONTRIBUTOR
170              
171             =for stopwords Ricardo Signes
172              
173             Ricardo Signes <rjbs@semiotic.systems>
174              
175             =head1 COPYRIGHT AND LICENSE
176              
177             This software is copyright (c) 2008 by Ricardo SIGNES.
178              
179             This is free software; you can redistribute it and/or modify it under
180             the same terms as the Perl 5 programming language system itself.
181              
182             =cut