[sf-perl] Getting the timezone

Tatsuhiko Miyagawa miyagawa at gmail.com
Fri Nov 17 22:43:05 PST 2006


On 11/17/06, David Alban <extasia at extasia.org> wrote:

>  use Time::Local;
>
>  use constant SECONDS_FIELD => 1;
>  use constant MINUTES_FIELD => 1;
>  use constant HOURS_FIELD   => 2;
>  use constant DAY_FIELD     => 3;
>  use constant MONTH_FIELD   => 4;
>  use constant YEAR_FIELD    => 5;
>
>  sub time_zone {
>    my $time = time;
>
>    my ( $local_year,
>         $local_month,
>         $local_day,
>         $local_hours,
>         $local_minutes,
>         $local_seconds,
>       ) = ( localtime $time )[ YEAR_FIELD,
>                                MONTH_FIELD,
>                                DAY_FIELD,
>                                HOURS_FIELD,
>                                MINUTES_FIELD,
>                                SECONDS_FIELD,
>                              ];
>
>    my ( $gm_year,
>         $gm_month,
>         $gm_day,
>         $gm_hours,
>         $gm_minutes,
>         $gm_seconds,
>       ) = ( gmtime $time )[ YEAR_FIELD,
>                             MONTH_FIELD,
>                             DAY_FIELD,
>                             HOURS_FIELD,
>                             MINUTES_FIELD,
>                             SECONDS_FIELD,
>                           ];
>
>    my $seconds_local = timelocal( $local_seconds,
>                                   $local_minutes,
>                                   $local_hours,
>                                   $local_day,
>                                   $local_month,
>                                   $local_year,
>                                 );
>
>    my $seconds_gm    = timelocal( $gm_seconds,
>                                   $gm_minutes,
>                                   $gm_hours,
>                                   $gm_day,
>                                   $gm_month,
>                                   $gm_year,
>                                 );
>
>    my $difference = $seconds_local - $seconds_gm;

This could've been just as simplified as:

  my $time = time;
  my $difference = timelocal(localtime($time)) - timelocal(gmtime($time));


-- 
Tatsuhiko Miyagawa


More information about the SanFrancisco-pm mailing list