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

This has never come up before because my server happens to be in the same time zone I am. How would I go about reformatting the reported date from a UNIX server to show the correct time / date in a specified time zone? In my programs I generally use
use POSIX qw(strftime); $today_date = strftime "%m/%d/%Y %T %Z", localtime;
I have read through the perl docs and my head is spinning. I really don't understand much about the localtime function I'm hoping there is an easy method other than just subtracting the hours from the hour value and then have to figure out if going back that many hours actually changes the date. Specifically I'm looking to have the time / date displayed for GMT -3 instead of EST Thanks in advance, -- Brian

Replies are listed 'Best First'.
Re: Display the time / date for different time zones
by MeowChow (Vicar) on Mar 16, 2001 at 06:18 UTC
    You can do a couple of things here:
    • Use the time function, which will give you a UTC time in seconds since 1970, based on GMT (0 offset).
    • Add/subtract x*3600 from that time, with x being the number of hours that your time zone is offset from GMT (note that this can get tricky with daylight savings).
    • Feed this number into gmtime (instead of localtime), to get your 8-element date/time list which you feed into POSIX::strftime.

    Or, you can check out some of the more full-featured modules on CPAN, such as Date::Manip, TimeDate, and Time-modules.

    I would opt for the latter approach.

       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: Display the time / date for different time zones
by kschwab (Vicar) on Mar 16, 2001 at 07:18 UTC
    There's a great example here of using perl to convert between different timezones (this one is a cgi front-end done in perl). There's a link to the perl source code off of the "technical info" section.

    You may also find the Time::Zone module helpful.

Re: Display the time / date for different time zones
by stephen (Priest) on Mar 16, 2001 at 06:15 UTC
    You can use the Date::Format module from CPAN.
    use Date::Format; $today_date = strftime "%m/%d/%Y %T %Z", localtime(), -3;
    (Untested, I'm afraid...)

    stephen