[sf-perl] Getting the timezone

Michael Friedman friedman at highwire.stanford.edu
Fri Nov 17 21:39:28 PST 2006


Well, now that you've got us all curious as to why you can't use  
modules...

You forgot to divide $difference by 60 to get minutes, but other than  
that this should be OK. Not the easiest way, I'm sure, but it should  
work.

You realize that you are assuming that the server already knows what  
timezone it's in. If it didn't, it would (probably) give you the same  
value for gmtime as localtime. And if the server is at all unix-like,  
you can get that timezone as
	$ENV{TZ}

formatted as readable text or
	system('date +%z');

which even formats it in RFC-2822 format like you want it.

(The system call will definitely be slower, but also smaller and more  
readable.)

-- Mike

On Nov 17, 2006, at 6:40 PM, David Alban wrote:

> Greetings,
>
> Assume I want to derive a string representing the local host's
> timezone.  The string is of the form sNNNN where s is a plus or minus
> sign, and the N's are digits.  E.g.:
>
>   -0800
>   +0000
>   +0100
>
> Assume I want to do this in "pure perl".  That is, I don't want to use
> any modules that aren't in the core perl distibution.  Assume you're
> not allowed to ask why.
>
> Can anyone see any reason not to do the following?
>
> Thanks,
> David
>
>   use Time::Local;
>
>   use constant SECONDS => 1;
>   use constant MINUTES => 60 * SECONDS;
>   use constant HOURS   => 60 * MINUTES;
>
>   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;
>
>     $difference > 24 * HOURS and return sprintf "+????";
>
>     my $sign = $difference < 0 ? "-" : "+";
>
>     $difference = abs $difference;
>
>     my $hours_offset   = int ( $difference / HOURS );
>     my $minutes_offset = int ( $difference % HOURS );
>
>     sprintf "%s%02d%02d", $sign, $hours_offset, $minutes_offset;
>   } # time_zone
>
>
> -- 
> Live in a world of your own, but always welcome visitors.
> _______________________________________________
> SanFrancisco-pm mailing list
> SanFrancisco-pm at pm.org
> http://mail.pm.org/mailman/listinfo/sanfrancisco-pm

---------------------------------------------------------------------
Michael Friedman                     HighWire Press
Phone: 650-725-1974                  Stanford University
FAX:   270-721-8034                  <friedman at highwire.stanford.edu>
---------------------------------------------------------------------




More information about the SanFrancisco-pm mailing list