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

Hi monks,
Is there a way to capture the system time zone in perl? If so how?
Thanks in advance Pops

Replies are listed 'Best First'.
Re: Time zone of the system
by polettix (Vicar) on Sep 28, 2005 at 09:28 UTC
    A look to DateTime::Timezone could be worth your time, in particular the The "local" time zone paragraph.

    Flavio
    perl -ple'$_=reverse' <<<ti.xittelop@oivalf

    Don't fool yourself.
Re: Time zone of the system
by marto (Cardinal) on Sep 28, 2005 at 09:28 UTC
    Hi,

    Check out Time::Timezone:
    "tz2zone() parses the TZ environment variable and returns a timezone string suitable for inclusion in date-like output"

    You may also want to look at The Perl DateTime Project .

    Hope this helps.

    Martin
      The TZ environment variable isn't the same as the system time zone. Most (Unix) systems will have their system clock running on UTC - but have most processes use a different TZ (There aren't that many places that actually have UTC as their timezone - even the UK and Portugal use daylight saving and are hence not on UTC, which doesn't do daylight saving). And TZ can vary from process to process, or even switch during the lifetime of the process.

      I do not know a portable way to determing the system time zone. If /etc/adjtime exists, one might look at its third line, however, there's no garantee that line is actually correct.

      I've no idea whether Windows system typically use UTC or local time.

Re: Time zone of the system
by McDarren (Abbot) on Sep 28, 2005 at 09:58 UTC
    use Tongue::In::Cheek; print `date +%Z`;
    Sorry, couldn't help myself :D
Re: Time zone of the system
by calin (Deacon) on Sep 28, 2005 at 09:59 UTC

    Keep in mind that while there is a system-wide timezone default, this is user overridable. Imagine an Internet host with 3 users logged in via SSH from Australia, Costa Rica and Zimbabwe. Each user is in a different timezone.

Re: Time zone of the system
by tbone1 (Monsignor) on Sep 28, 2005 at 14:12 UTC
      Is there a way to capture the system time zone in perl?

      Yes.

      (SLAMS FIST ON DESK WHILE LAUGHING GIDDILY)

      Oh Lord, do I ever need coffee ...

      If the TZ environment variable is misset, then I'm not sure what you're going to do. Here is what I got off of our Sun server:

      $ env | grep TZ TZ=US/East-Indiana $
      This might be a sysadmin thing to correct rather than a perl script thing.

      --
      tbone1, YAPS (Yet Another Perl Schlub)
      And remember, if he succeeds, so what.
      - Chick McGee

Re: Time zone of the system
by Samy_rio (Vicar) on Sep 28, 2005 at 09:27 UTC

    Try it,

    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time +);

    Regards,
    Velusamy R.

      That neither gives you a timezone (the last field only indicates whether daylight saving time is active or not), nor does it give you system time information. localtime gives you the time for whatever timezone your process has set (or inherited). Which may be the same as your system timezone, but on a typical Unix system, most likely is not.