| 1 | 1 | ▶ | use 5.006; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | package Net::DNS::GuessTZ; | ||||||||||||||||||||
| 5 | { | ||||||||||||||||||||
| 6 | 1 | $Net::DNS::GuessTZ::VERSION = '0.004'; | |||||||||||||||||||
| 7 | } | ||||||||||||||||||||
| 8 | # ABSTRACT: guess the time zone of a host | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 1 | ▶ | use DateTime::TimeZone 0.51; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 1 | ▶ | use List::Util (); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 1 | ▶ | use Sub::Exporter::Util; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 1 | use Sub::Exporter -setup => { | |||||||||||||||||||
| 16 | exports => [ tz_from_host => Sub::Exporter::Util::curry_method ], | ||||||||||||||||||||
| 17 | 1 | ▶ | }; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 1 | my $ICF; | |||||||||||||||||||
| 20 | sub _icf { | ||||||||||||||||||||
| 21 | 0 | ▶ | $ICF ||= do { | ||||||||||||||||||
|
Subroutine
_icf: not called CC=2 SCAR=17.9
Condition0%0/3
$ICF ||= do {
MC/DC0%0/2
$ICF ||= do {
require IP::Country::Fast;
"IP::Country::Fast"->new
}
$ICF
do {
require IP::Country::Fast;
"IP::Country::Fast"->new
}
Truth table0%0/3
A ||= B
A= $ICFB= do {
require IP::Country::Fast;
"IP::Country::Fast"->new
}
| |||||||||||||||||||||
| 22 | 0 | require IP::Country::Fast; | |||||||||||||||||||
| 23 | 0 | IP::Country::Fast->new; | |||||||||||||||||||
| 24 | }; | ||||||||||||||||||||
| 25 | } | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | sub _all_tz_from_cctld { | ||||||||||||||||||||
| 28 | 0 | ▶ | my ($self, $host) = @_; | ||||||||||||||||||
|
Subroutine
_all_tz_from_cctld: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 29 | 0 | ▶ | return unless my ($cctld) = $host =~ /\.(\w{2})\z/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 30 | 0 | DateTime::TimeZone->names_in_country($cctld); | |||||||||||||||||||
| 31 | } | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | sub _all_tz_from_ip { | ||||||||||||||||||||
| 34 | 0 | ▶ | my ($self, $host) = @_; | ||||||||||||||||||
|
Subroutine
_all_tz_from_ip: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 35 | 0 | ▶ | return unless my $cc = $self->_icf->inet_atocc($host); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 36 | 0 | my @names = DateTime::TimeZone->names_in_country($cc); | |||||||||||||||||||
| 37 | } | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | sub tz_from_host { | ||||||||||||||||||||
| 41 | 2 | ▶ | my ($self, $host, $arg) = @_; | ||||||||||||||||||
|
Subroutine
tz_from_host: called CC=10 SCAR=44.4
Pod
documented
| |||||||||||||||||||||
| 42 | 2 | ▶ | return unless $host; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 43 | 0 | ▶ | $arg ||= {}; | ||||||||||||||||||
|
Condition0%0/2
$arg ||= {}
MC/DC0%0/1
$arg ||= {}
$arg
Truth table0%0/2
A
A= $arg
| |||||||||||||||||||||
| 44 | 0 | ▶ | $arg->{ip_country} = 1 unless exists $arg->{ip_country}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | 0 | my %result; | |||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | 0 | my @names = $self->_all_tz_from_cctld($host); | |||||||||||||||||||
| 49 | 0 | ▶ | $result{cc} = $names[0] if @names; # and @names <= 3; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 0 | ▶ | if ($arg->{ip_country}) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 0 | my @names = $self->_all_tz_from_ip($host); | |||||||||||||||||||
| 53 | 0 | ▶ | $result{ip} = $names[0] if @names; # if @names <= 3; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | my @cand = ($arg->{priority}||'ip') eq 'cc' | ||||||||||||||||||||
| 57 | ? @result{qw(cc ip)} | ||||||||||||||||||||
| 58 | 0 | ▶ | : @result{qw(ip cc)}; | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$arg->{'priority'} || 'ip'
MC/DC0%0/1
$arg->{'priority'} || 'ip'
$arg->{'priority'}
Truth table0%0/2
A
A= $arg->{'priority'}
| |||||||||||||||||||||
| 59 | |||||||||||||||||||||
| 60 | 0 | ▶ | if (my $tz = List::Util::first { defined } @cand) { | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||||||||
| 61 | 0 | return $tz; | |||||||||||||||||||
| 62 | } else { | ||||||||||||||||||||
| 63 | 0 | return; | |||||||||||||||||||
| 64 | } | ||||||||||||||||||||
| 65 | } | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | 1 | 1; | |||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | __END__ | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | =pod | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =encoding UTF-8 | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | =head1 NAME | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | Net::DNS::GuessTZ - guess the time zone of a host | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =head1 VERSION | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | version 0.004 | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | =head1 SYNOPSIS | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | use Net::DNS::GuessTZ qw(tz_from_host); | ||||||||||||||||||||
| 86 | |||||||||||||||||||||
| 87 | my $tz = tz_from_host('cr.yp.to'); | ||||||||||||||||||||
| 88 | |||||||||||||||||||||
| 89 | =head1 DESCRIPTION | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | Brazenly stolen from L<Plagger::Plugin::Filter::GuessTimeZoneByDomain>, this | ||||||||||||||||||||
| 92 | module makes an effort to guess an appropriate time zone for a given host. It | ||||||||||||||||||||
| 93 | will look up the location of the IP addresses owner and it will also consider | ||||||||||||||||||||
| 94 | the country code top-level domain, if the host is under one. | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =head1 METHODS | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =head2 tz_from_host | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | my $tz_name = Net::DNS::GuessTZ->tz_from_host($hostname, %arg); | ||||||||||||||||||||
| 101 | |||||||||||||||||||||
| 102 | This routine returns a guess at the given host's time zone, or false if no | ||||||||||||||||||||
| 103 | guess can be made. | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | Valid arguments are: | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | priority - which method to give priority to: "cc" or "ip"; default: ip | ||||||||||||||||||||
| 108 | ip_country - whether to check the IP address's owner with IP::Country; | ||||||||||||||||||||
| 109 | defaults to true | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | Unlike the Plagger plugin, this routine will gladly make a guess when the | ||||||||||||||||||||
| 112 | country it finds has more than three time zones. | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =head1 CAVEATS | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | This is fine if you don't really care too much about being correct. It's | ||||||||||||||||||||
| 117 | probably better than just always assuming local time. | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | Still, if possible, ask the user for his time zone when you can! | ||||||||||||||||||||
| 120 | |||||||||||||||||||||
| 121 | =head1 AUTHOR | ||||||||||||||||||||
| 122 | |||||||||||||||||||||
| 123 | Ricardo SIGNES <rjbs@cpan.org> | ||||||||||||||||||||
| 124 | |||||||||||||||||||||
| 125 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | This software is copyright (c) 2008 by Ricardo SIGNES. | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 130 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||