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

I can't seem to get Time::CTime::strftime to print out the timezone for me...

I do the following:

my $time = time; 
print strftime('%l:%M:%S %p, %A %B %o, %Y', localtime($time));

This works wonderfully generating out put such as:
2:40:55 PM, Tuesday October 10th, 2000 however...I want to add in the timezone.

Now I would figure I'd simply modify the template to read as such:

print strftime('%l:%M:%S %p %Z, %A %B %o, %Y', localtime($time));

However, this ends up just putting a blank space where the %Z token (which according to the perldoc is the proper token) is

Executing

$ date +%M
yields the timezone...so I know it's set...

Any ideas, monks?

  • Comment on Time::CTime::strftime won't print Timezones...

Replies are listed 'Best First'.
(jcwren) Re: Time::CTime::strftime won't print Timezones...
by jcwren (Prior) on Oct 10, 2000 at 23:57 UTC
    This relies on the 'TZ' environment variable being set. Type 'set' at a shell prompt, and you'll find that TZ isn't defined.

    Type 'export TZ=EST5' at a *nix shell prompt, or 'set TZ=EST5' under Windows, and then run your program (from the shell, so that the environment is inherited). It will then print the 'EST' portion of the TZ variable.

    I'd like to tell you how to set it automatically, but I'm not sure I'd be giving accurate information. Maybe someone else can provide that piece of the (non-Perl) puzzle.

    --Chris

    e-mail jcwren
      However...I like to stay away from backticks as much as I can. Any other ideas?
        The magic %ENV gives you access to environmental variables:
        use POSIX; $ENV{TZ} = 'KTZ'; # Kinakuta Time Zone print strftime('%l:%M:%S %p %Z, %A %B %o, %Y', localtime($time));
        Easy!
      Well...this is meant to be a CGI...and I want it to determine at runtime what the timezone is... I could feasibly do it as such:
      $timezone = `date +%Z`;
      # blah...get all the information and plop it all into a string and print it
      

      And I might just end up doing that. :P