| 1 |
package DateTime::TimeZone::OlsonDB::Change; |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use namespace::autoclean; |
| 6 |
|
| 7 |
our $VERSION = '2.70'; |
| 8 |
|
| 9 |
sub new { |
| 10 |
my $class = shift; |
| 11 |
my %p = @_; |
| 12 |
|
| 13 |
# These are almost always mutually exclusive, except when adding |
| 14 |
# an observance change and the last rule has no offset, but the |
| 15 |
# new observance has an anonymous rule. In that case, prefer the |
| 16 |
# offset from std defined in the observance to that in the |
| 17 |
# previous rule (what a mess!). |
| 18 |
if ( $p{type} eq 'observance' ) { |
| 19 |
$p{offset_from_std} = $p{rule}->offset_from_std if defined $p{rule}; |
| 20 |
$p{offset_from_std} = $p{observance}->offset_from_std |
| 21 |
if $p{observance}->offset_from_std; |
| 22 |
$p{offset_from_std} ||= 0; |
| 23 |
} |
| 24 |
else { |
| 25 |
$p{offset_from_std} = $p{observance}->offset_from_std; |
| 26 |
$p{offset_from_std} = $p{rule}->offset_from_std if defined $p{rule}; |
| 27 |
} |
| 28 |
|
| 29 |
$p{offset_from_utc} = $p{observance}->offset_from_utc; |
| 30 |
|
| 31 |
$p{is_dst} = 0; |
| 32 |
$p{is_dst} = 1 if $p{rule} && $p{rule}->offset_from_std; |
| 33 |
$p{is_dst} = 1 if $p{observance}->offset_from_std; |
| 34 |
|
| 35 |
if ( $p{short_name} =~ m{([\-\+\w]+)/([\-\+\w]+)} ) { |
| 36 |
$p{short_name} = $p{is_dst} ? $2 : $1; |
| 37 |
} |
| 38 |
|
| 39 |
return bless \%p, $class; |
| 40 |
} |
| 41 |
|
| 42 |
sub utc_start_datetime { $_[0]->{utc_start_datetime} } |
| 43 |
sub local_start_datetime { $_[0]->{local_start_datetime} } |
| 44 |
sub short_name { $_[0]->{short_name} } |
| 45 |
sub is_dst { $_[0]->{is_dst} } |
| 46 |
sub observance { $_[0]->{observance} } |
| 47 |
sub rule { $_[0]->{rule} } |
| 48 |
sub offset_from_utc { $_[0]->{offset_from_utc} } |
| 49 |
sub offset_from_std { $_[0]->{offset_from_std} } |
| 50 |
sub total_offset { $_[0]->offset_from_utc + $_[0]->offset_from_std } |
| 51 |
|
| 52 |
sub two_changes_as_span { |
| 53 |
my ( $c1, $c2 ) = @_; |
| 54 |
|
| 55 |
my ( $utc_start, $local_start ); |
| 56 |
|
| 57 |
if ( defined $c1->utc_start_datetime ) { |
| 58 |
$utc_start = $c1->utc_start_datetime->utc_rd_as_seconds; |
| 59 |
$local_start = $c1->local_start_datetime->utc_rd_as_seconds; |
| 60 |
} |
| 61 |
else { |
| 62 |
$utc_start = $local_start = '-inf'; |
| 63 |
} |
| 64 |
|
| 65 |
my $utc_end = $c2->utc_start_datetime->utc_rd_as_seconds; |
| 66 |
my $local_end = $utc_end + $c1->total_offset; |
| 67 |
|
| 68 |
return { |
| 69 |
utc_start => $utc_start, |
| 70 |
utc_end => $utc_end, |
| 71 |
local_start => $local_start, |
| 72 |
local_end => $local_end, |
| 73 |
short_name => $c1->short_name, |
| 74 |
offset => $c1->total_offset, |
| 75 |
is_dst => $c1->is_dst, |
| 76 |
}; |
| 77 |
} |
| 78 |
|
| 79 |
## no critic (Subroutines::ProhibitUnusedPrivateSubroutines, InputOutput::RequireCheckedSyscalls) |
| 80 |
sub _debug_output { |
| 81 |
my $self = shift; |
| 82 |
|
| 83 |
my $obs = $self->observance; |
| 84 |
|
| 85 |
if ( $self->utc_start_datetime ) { |
| 86 |
print ' UTC: ', $self->utc_start_datetime->datetime, "\n"; |
| 87 |
print ' Local: ', $self->local_start_datetime->datetime, "\n"; |
| 88 |
} |
| 89 |
else { |
| 90 |
print " First change (starts at -inf)\n"; |
| 91 |
} |
| 92 |
|
| 93 |
print ' Short name: ', $self->short_name, "\n"; |
| 94 |
printf " UTC offset: %s (%s)\n", $obs->offset_from_utc, |
| 95 |
$obs->offset_from_utc_as_hm; |
| 96 |
|
| 97 |
if ( $obs->offset_from_std || $self->rule ) { |
| 98 |
if ( $obs->offset_from_std ) { |
| 99 |
printf " Std offset: %s (%s)\n", $obs->offset_from_std, |
| 100 |
$obs->offset_from_std_as_hm; |
| 101 |
} |
| 102 |
|
| 103 |
if ( $self->rule ) { |
| 104 |
printf " Std offset: %s (%s) - %s rule\n", |
| 105 |
$self->rule->offset_from_std, |
| 106 |
$self->rule->offset_from_std_as_hm, |
| 107 |
$self->rule->name; |
| 108 |
} |
| 109 |
} |
| 110 |
else { |
| 111 |
print " Std offset: 0 - no rule\n"; |
| 112 |
} |
| 113 |
|
| 114 |
print "\n"; |
| 115 |
} |
| 116 |
## use critic |
| 117 |
|
| 118 |
1; |