[LA.pm] OT: changing the date timezone for perl use

David Pisoni dpisoni at shopzilla.com
Thu Jun 23 09:21:07 PDT 2005


On Jun 23, 2005, at 9.05, David Heayn wrote:

> I have an ultra simple perl script that requests the current
> date/time from a shared server (not mine) then spits out the html
> dressing.
>
> My problem is the server is set to Pacific time and I need Eastern 
> time.
>
> Anyone have any suggestions/alternative server time mechanisms. I'd
> rather not go in and program a complete time zone remapping function.
>
> Thanks.
>
> David Heayn * http://www.cloudgraphics.com * 213/925.3283
> _______________________________________________
> Losangeles-pm mailing list
> Losangeles-pm at pm.org
> http://mail.pm.org/mailman/listinfo/losangeles-pm

David,

I just ran into this issue recently.  I came up with a solution, though 
it isn't entirely portable, so YMMV.  This works on my Linux system and 
my MacOS X system:

print scalar localtime;	 	# outputs local Pacific time
{ # block to insulate time zone change
	local $ENV{TZ} = 'America/New York'; # Or I could say 'EST5EDT'
	print scalar localtime; # outputs local Eastern time
}

The valid values for the TZ environment variable are the files located 
in '/usr/share/zoneinfo' (on both my systems.)  There are files at the 
top level (e.g., 'EST5EDT') and a directory hierarchy (e.g., 
'America/New_York').  Spaces in the environment variable get mapped to 
underscores on the filesystem.

On my systems, the default state of the environment variable is 
undefined: other factors are determining the home locale.  The 
environment variable seems to serve as an "override", at least from the 
perspective of the perl runtime.

In my exploration of the topic I ran across POSIX::tzset, which seemed 
entirely unnecessary on my system.  Perhaps some systems would require 
this call.  Essentially all it does is set the internal time zone to 
the current value of $ENV{TZ}.

Enjoy,

David Pisoni
dpisoni at shopzilla.com
Director, Web Engineering
Shopzilla, Inc. <http://www.shopzilla.com/>

"One machine can do the work of fifty ordinary men. No machine can do 
the work of one extraordinary man." - Elbert Hubbard, author, editor, 
printer (1856-1915)



More information about the Losangeles-pm mailing list