Someone, possibly Sam Vilain wrote:
> > > > against the module.  I really would like to remove my dependancy on
> > > > Date::Manip, but, really - 4.5MB?

The generated TimeZone modules are a bit larger than they could be. With
the attached patch, their total size is reduced by almost 50%.

(There's an "undefined variable" warning for zones without a dst rule,
because the is_dst hash element is neveer set. It's harmless.)

The modules get somewhat harder to read, so perhaps there should be an
option in parse_olson to get the old format for the tz changes.

Eugene



diff -r modules/DateTime-TimeZone/lib/DateTime/TimeZone.pm 
modules/DateTime-TimeZone-2/lib/DateTime/TimeZone.pm
272a273,325
> sub _generate_spans_until_match
> {
>     my $self = shift;
>     my $generate_until_year = shift;
>     my $seconds = shift;
>     my $type = shift;
> 
>     my @changes;
>     foreach my $rule (@{$self->_rules})
>     {
>         foreach my $year ( $self->_max_year .. $generate_until_year )
>         {
>             my $next = $rule->date_for_year( $year, $self->_last_offset );
> 
>             # don't bother with changes we've seen already
>             next if $next->{utc}->utc_rd_as_seconds < $self->max_span->{utc_end};
> 
>             push @changes,
>                 DateTime::TimeZone::OlsonDB::Change->new
>                     ( start_date => $next->{local},
>                       short_name =>
>                       sprintf( $self->_last_observance->format, $rule->letter ),
>                       observance => $self->_last_observance,
>                       rule       => $rule,
>                     );
>         }
>     }
> 
>     $self->_max_year($generate_until_year);
>     my @sorted = sort { $a->start_date <=> $b->start_date } @changes;
> 
>     my $start_key = $type . '_start';
>     my $end_key   = $type . '_end';
> 
>     my $match;
>     for ( my $x = 1; $x < @sorted; $x++ )
>     {
>         my $last_offset =
>             $x == 1 ? $self->max_span->{offset} : $sorted[ $x - 2 ]->offset;
> 
>         my $span =
>             DateTime::TimeZone::OlsonDB::Change::two_changes_as_span
>                 ( @sorted[ $x - 1, $x ], $last_offset );
> 
>         push @{ $self->{spans} }, $span;
> 
>         $match = $span
>             if $seconds >= $span->{$start_key} && $seconds < $span->{$end_key};
>     }
> 
>     return $match;
> }
> 
diff -r modules/DateTime-TimeZone/tools/parse_olson 
modules/DateTime-TimeZone-2/tools/parse_olson
129c129
<         my $spans = Dumper zone_as_data($zone);
---
>         my $spans = stringify_zones(zone_as_data($zone));
327a328,346
> sub stringify_zones
> {
>     my $data = shift;
>     my @keys = keys %{$data->[0]};
> 
>     return <<EOF
> [
>     map { my \%hash; [EMAIL PROTECTED]/@keys/} = [EMAIL PROTECTED]; \\\%hash }
> EOF
>         . join("\n", map {stringify_zone($_, [EMAIL PROTECTED])} @$data) . "\n]";
> }
> 
> sub stringify_zone
> {
>     my ($zone, $keys) = @_;
> 
>     return "\t[" . join(',', map "'$_'", @[EMAIL PROTECTED]) . '],';
> }
> 
338,388c357,363
< sub _generate_spans_until_match
< {
<     my $self = shift;
<     my $generate_until_year = shift;
<     my $seconds = shift;
<     my $type = shift;
< 
<     my @changes;
<     foreach my $rule (@$rules)
<     {
<         foreach my $year ( $max_year .. $generate_until_year )
<         {
<             my $next = $rule->date_for_year( $year, !OFFSET );
< 
<             # don't bother with changes we've seen already
<             next if $next->{utc}->utc_rd_as_seconds < $self->max_span->{utc_end};
< 
<             push @changes,
<                 DateTime::TimeZone::OlsonDB::Change->new
<                     ( start_date => $next->{local},
<                       short_name =>
<                       sprintf( $last_observance->format, $rule->letter ),
<                       observance => $last_observance,
<                       rule       => $rule,
<                     );
<         }
<     }
< 
<     $max_year = $generate_until_year;
<     my @sorted = sort { $a->start_date <=> $b->start_date } @changes;
< 
<     my $start_key = $type . '_start';
<     my $end_key   = $type . '_end';
< 
<     my $match;
<     for ( my $x = 1; $x < @sorted; $x++ )
<     {
<         my $last_offset =
<             $x == 1 ? $self->max_span->{offset} : $sorted[ $x - 2 ]->offset;
< 
<         my $span =
<             DateTime::TimeZone::OlsonDB::Change::two_changes_as_span
<                 ( @sorted[ $x - 1, $x ], $last_offset );
< 
<         push @{ $self->{spans} }, $span;
< 
<         $match = $span
<             if $seconds >= $span->{$start_key} && $seconds < $span->{$end_key};
<     }
< 
<     return $match;
---
> sub _last_offset { !OFFSET }
> sub _last_observance { $last_observance }
> sub _rules { $rules }
> sub _max_year {
>     my ($self, $m) = @_;
>     $max_year = $m if defined $m;
>     return $max_year;
393a369,370
>     delete $last_observance->{start}{language};
> 

Reply via email to