suaveant has asked for the wisdom of the Perl Monks concerning the following question:

I am looking for a way to get the actual start and end of daylight savings in a timezone from DateTime... I have been looking and there don't seem to be any existing calls, just is_dst.

Right now I am looking to add my own, but I thought maybe I'd missed something.

                - Ant
                - Some of my best work - (1 2 3)

  • Comment on Is there a way to pull dst start/end from DateTime.pm?

Replies are listed 'Best First'.
Re: Is there a way to pull dst start/end from DateTime.pm?
by Fletch (Bishop) on Mar 02, 2007 at 19:11 UTC
      Yeah.. that's basically what I am looking through... I was hoping there might already be a public method I had missed somewhere, though.

                      - Ant
                      - Some of my best work - (1 2 3)

Re: Is there a way to pull dst start/end from DateTime.pm?
by jasonk (Parson) on Mar 06, 2007 at 15:17 UTC

    Here is one approach,although it depends on a lot of undocumented pieces of DateTime::TimeZone, it was the only way I could find at the time...

    #!/usr/local/bin/perl -w ################## use DateTime::TimeZone; use Data::Dumper; my $tz = shift || 'America/New_York'; my $utc_start = DateTime::TimeZone::UTC_START(); my $utc_end = DateTime::TimeZone::UTC_END(); my $short_name = DateTime::TimeZone::SHORT_NAME(); my $tzobj = DateTime::TimeZone->new( name => $tz ); foreach my $span ( @{ $tzobj->{ 'spans' } } ) { my $start = RD->new( $span->[ $utc_start ], $tzobj, )->as_datetime; my $end = RD->new( $span->[ $utc_end ], $tzobj, )->as_datetime; print "$span->[ $short_name ]: $start - $end\n"; } package RD; use constant SECONDS_PER_DAY => 86400; sub new { my $class = shift; my $seconds = shift; my $timezone = shift; my $self = bless( [ $seconds, $timezone ], $class, ); return $self; } sub time_zone { shift->[1] } sub utc_rd_values { my ( $self ) = @_; my $days = int( $self->[0] / SECONDS_PER_DAY ); my $seconds = int( $self->[0] % SECONDS_PER_DAY ); return ( $days, $seconds, 0 ); } sub as_datetime { my ( $self ) = @_; if ( $self->[0] eq '-inf' ) { return DateTime::Infinite::Past->new; } if ( $self->[0] eq 'inf' ) { return DateTime::Infinite::Future->new; } return DateTime->from_object( object => $self ); }

    We're not surrounded, we're in a target-rich environment!