| 1 |
package DateTime::TimeZone::OlsonDB::Zone; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use namespace::autoclean; |
| 6 |
|
| 7 |
our $VERSION = '2.69'; |
| 8 |
|
| 9 |
use DateTime::TimeZone; |
| 10 |
use DateTime::TimeZone::OlsonDB; |
| 11 |
use DateTime::TimeZone::OlsonDB::Change; |
| 12 |
use DateTime::TimeZone::OlsonDB::Observance; |
| 13 |
use List::Util qw( first max ); |
| 14 |
|
| 15 |
sub new { |
| 16 |
my $class = shift; |
| 17 |
my %p = @_; |
| 18 |
|
| 19 |
my $self = { |
| 20 |
name => $p{name}, |
| 21 |
observances => $p{observances}, |
| 22 |
changes => [], |
| 23 |
infinite_rules => {}, |
| 24 |
}; |
| 25 |
|
| 26 |
return bless $self, $class; |
| 27 |
} |
| 28 |
|
| 29 |
sub name { $_[0]->{name} } |
| 30 |
|
| 31 |
sub last_rules_year { |
| 32 |
my $self = shift; |
| 33 |
my $odb = shift; |
| 34 |
|
| 35 |
my $last_rule = $self->{observances}[-1]{rules}; |
| 36 |
|
| 37 |
return unless $last_rule; |
| 38 |
|
| 39 |
my @rules = $odb->rules_by_name($last_rule); |
| 40 |
|
| 41 |
return $rules[-1]->min_year(); |
| 42 |
} |
| 43 |
|
| 44 |
sub expand_observances { |
| 45 |
my $self = shift; |
| 46 |
my $odb = shift; |
| 47 |
my $max_year = shift; |
| 48 |
|
| 49 |
my $prev_until; |
| 50 |
## no critic (ControlStructures::ProhibitCStyleForLoops) |
| 51 |
for ( my $x = 0; $x < @{ $self->{observances} }; $x++ ) { |
| 52 |
my %p = %{ $self->{observances}[$x] }; |
| 53 |
|
| 54 |
my $rules_name = delete $p{rules}; |
| 55 |
|
| 56 |
my $last_offset_from_std |
| 57 |
= $self->last_change ? $self->last_change->offset_from_std : 0; |
| 58 |
my $last_offset_from_utc |
| 59 |
= $self->last_change ? $self->last_change->offset_from_utc : 0; |
| 60 |
|
| 61 |
my $obs = DateTime::TimeZone::OlsonDB::Observance->new( |
| 62 |
%p, |
| 63 |
utc_start_datetime => $prev_until, |
| 64 |
rules => [ $odb->rules_by_name($rules_name) ], |
| 65 |
last_offset_from_utc => $last_offset_from_utc, |
| 66 |
last_offset_from_std => $last_offset_from_std, |
| 67 |
); |
| 68 |
|
| 69 |
my $rule = $obs->first_rule; |
| 70 |
my $letter = $rule ? $rule->letter : q{}; |
| 71 |
|
| 72 |
my $change = DateTime::TimeZone::OlsonDB::Change->new( |
| 73 |
type => 'observance', |
| 74 |
utc_start_datetime => $obs->utc_start_datetime, |
| 75 |
local_start_datetime => $obs->local_start_datetime, |
| 76 |
short_name => $obs->formatted_short_name( $letter, $rule ), |
| 77 |
observance => $obs, |
| 78 |
$rule ? ( rule => $rule ) : (), |
| 79 |
); |
| 80 |
|
| 81 |
if ($DateTime::TimeZone::OlsonDB::DEBUG) { |
| 82 |
## no critic (InputOutput::RequireCheckedSyscalls) |
| 83 |
print "Adding observance change ...\n"; |
| 84 |
|
| 85 |
$change->_debug_output; |
| 86 |
} |
| 87 |
|
| 88 |
$self->add_change($change); |
| 89 |
|
| 90 |
if ( $obs->rules ) { |
| 91 |
$obs->expand_from_rules( $self, $max_year ); |
| 92 |
} |
| 93 |
|
| 94 |
$prev_until = $obs->until( |
| 95 |
$self->last_change ? $self->last_change->offset_from_std : 0 ); |
| 96 |
|
| 97 |
# last observance |
| 98 |
if ( $x == $#{ $self->{observances} } ) { |
| 99 |
foreach my $rule ( $obs->rules ) { |
| 100 |
if ( $rule->is_infinite ) { |
| 101 |
$self->add_infinite_rule($rule); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
sub add_change { |
| 109 |
my $self = shift; |
| 110 |
my $change = shift; |
| 111 |
|
| 112 |
if ( defined $change->utc_start_datetime ) { |
| 113 |
if ( @{ $self->{changes} } |
| 114 |
&& $self->{changes}[-1]->utc_start_datetime |
| 115 |
&& $self->{changes}[-1]->utc_start_datetime |
| 116 |
== $change->utc_start_datetime ) { |
| 117 |
if ( $self->{changes}[-1]->rule && $change->observance ) { |
| 118 |
## no critic (InputOutput::RequireCheckedSyscalls) |
| 119 |
print |
| 120 |
" Ignoring previous rule change, that starts the same time as current observance change\n\n" |
| 121 |
if $DateTime::TimeZone::OlsonDB::DEBUG; |
| 122 |
|
| 123 |
$self->{changes}[-1] = $change; |
| 124 |
|
| 125 |
return; |
| 126 |
} |
| 127 |
|
| 128 |
die |
| 129 |
"Cannot add two different changes that have the same UTC start datetime!\n"; |
| 130 |
} |
| 131 |
|
| 132 |
my $last_change = $self->last_change; |
| 133 |
|
| 134 |
if ( $last_change->short_name eq $change->short_name |
| 135 |
&& $last_change->total_offset == $change->total_offset |
| 136 |
&& $last_change->is_dst == $change->is_dst ) { |
| 137 |
my $last_rule = $last_change->rule || q{}; |
| 138 |
my $new_rule = $change->rule || q{}; |
| 139 |
|
| 140 |
## no critic (InputOutput::RequireCheckedSyscalls) |
| 141 |
print "Skipping identical change\n" |
| 142 |
if $DateTime::TimeZone::OlsonDB::DEBUG; |
| 143 |
return; |
| 144 |
} |
| 145 |
|
| 146 |
push @{ $self->{changes} }, $change; |
| 147 |
} |
| 148 |
else { |
| 149 |
if ( $self->{earliest} ) { |
| 150 |
die 'There can only be one earliest time zone change!'; |
| 151 |
} |
| 152 |
else { |
| 153 |
$self->{earliest} = $change; |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
sub add_infinite_rule { |
| 159 |
$_[0]->{infinite_rules}{ $_[1] } = $_[1]; |
| 160 |
} |
| 161 |
|
| 162 |
sub last_change { |
| 163 |
return unless @{ $_[0]->{changes} } || $_[0]->{earliest}; |
| 164 |
return ( |
| 165 |
@{ $_[0]->{changes} } |
| 166 |
? $_[0]->{changes}[-1] |
| 167 |
: $_[0]->{earliest} |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
sub sorted_changes { |
| 172 |
( |
| 173 |
( defined $_[0]->{earliest} ? $_[0]->{earliest} : () ), |
| 174 |
sort { $a->utc_start_datetime <=> $b->utc_start_datetime } |
| 175 |
@{ $_[0]->{changes} } |
| 176 |
); |
| 177 |
} |
| 178 |
|
| 179 |
sub infinite_rules { |
| 180 |
my @v = sort { $a->min_year <=> $b->min_year || $a->month cmp $b->month } |
| 181 |
values %{ $_[0]->{infinite_rules} }; |
| 182 |
return @v; |
| 183 |
} |
| 184 |
|
| 185 |
1; |