tags:

views:

95

answers:

3

I had assumed it would be as simple as $ENV{TZ}, but the TZ environment variable is not set, and yet the date command still knows I am in EDT, so there must be some other way of determining timezone (other than saying chomp(my $tz = qx/date +%Z/);).

+8  A: 
use POSIX;
print strftime("%Z", localtime()), "\n";
Igor Krivokon
I thought about %Z, but I remember something about it not working on some systems.
Chas. Owens
According to man strftime, %Z is a standard specification and should be supported on all platforms.
Igor Krivokon
I think I was remembering this from the POSIX docs: The "Z" specifier is notoriously unportable since the names of timezones are non‐standard. Sticking to the numeric specifiers is the safest route. But I think this is probably safe enough for my purposes.
Chas. Owens
+4  A: 
use POSIX;
localtime();
my ($std, $dst) = POSIX::tzname();

tzname() gives you access to the POSIX global tzname - but you need to have called localtime() for it to be set in the first place.

Beano
+3  A: 

If you want something more portable than POSIX (but probably much slower) you can use DateTime::TimeZone for this:

use DateTime::TimeZone;

print DateTime::TimeZone->new( name => 'local' )->name();
Dave Rolsky