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

don't know if its been like this since installation, but my perl time function is giving me the wrong time. I'm in EST and its giving me back GMT(I believe) Example: It's 9:30 AM here in Florida, and time gives me back 1094736647 which is Thu Sep 9 13:31:13 2004, 4 hours ahead.... Very strange and I don't know how to fix it. I checked my system local settings, and they all Say EST. My BIOS clock is right too...Strange
This is perl, v5.8.4 built for MSWin32-x86-multi-thread (with 3 registered patches, see perl -V for more detail)
Any help is appreciated, thanks

Replies are listed 'Best First'.
Re: time() off
by gellyfish (Monsignor) on Sep 09, 2004 at 14:20 UTC

    What do you get if do (at the command prompt):

    c:\> SET TZ=EST4 C:\> perl -e"print scalar localtime()"
    ?

    I suspect that your OS has broken timezone handling (you don't say what it is though.)

    /J\

      That fixed it. For whatever reason I had a system variable TZ and it was set to EST. Changing it to EST4 worked and my perl time is back! Thanks guys.
Re: time() off
by bart (Canon) on Sep 09, 2004 at 13:42 UTC
    time() returns epoch time, which would be the same (for your type of OS) all over the world. It has no idea about GMT. It's just a number, the number of seconds passed since a reference date, commonly 1.1.1970 at 0:00:00, GMT. That's a moment in time in history, expressed in GMT.

    You're probably using gmtime() in your conversion to string, while you should be using localtime() instead.

    my $time = 1094736647; print scalar gmtime($time); print "\n"; print scalar localtime($time);
Re: time() off
by edan (Curate) on Sep 09, 2004 at 13:46 UTC

    The perl time function returns epoch time, which is likely to be (except on strange sysmtems) "the number of non-leap seconds since 00:00:00 UTC, January 1, 1970" That means it's in "GMT" (or UTC) time. If you want the local time, you want to feed the value of time to the (conveniently named) function localtime. If you want that pretty string like "Thu Sep 9 09:31:13 2004" then call localtime in scalar context. If you have more specific formatting needs, I would use POSIX::strftime().

    --
    edan

      ok, understood, about time(), but localtime is still off, its not 1:48 in florida right now its 9:48
      my localtime() returns Thu Sep 9 13:48:38 2004, and that's wrong. what will fix that?
        ok, so maybe I'm just not getting it...Does localtime simply return time in a nice format?, and not the actual "Local Time" of your system? if so I get it, I think thats dumb, but I get it...If localtime is actually supposed to return the "Local time" of my stytem then its off. Please clarify.
Re: time() off
by osunderdog (Deacon) on Sep 09, 2004 at 14:25 UTC
    I've seen something similar if the environment variable 'TZ' is set. This is consistent on both *nix and Windows:
    F:\>perl -e "print scalar localtime" Thu Sep 9 08:18:24 2004 F:\>set TZ=MST F:\>perl -e "print scalar localtime" Thu Sep 9 14:19:25 2004
    And on *nix:
    $perl -e "print scalar localtime" Thu Sep 9 08:20:26 2004 $export TZ=mst $perl -e "print scalar localtime" Thu Sep 9 14:20:35 2004
Re: time() off
by mutated (Monk) on Sep 09, 2004 at 13:58 UTC
    I'm assuming you are running on a *nix what does the command date return? How are you setting your system clock? and what is /etc/localtime symbolicaly linked to?


    daN.
      No unfortunately I don't have the luxury here at work to get to use Nix, I'm on MSWin32, using ActiveState perl, v5.8.4, my system clock and BIOS Clock are both correct, and my regional settings are set to United States
Re: time() off
by rupesh (Hermit) on Sep 09, 2004 at 13:38 UTC
    C:\>perl -e "print scalar localtime();" Thu Sep 9 19:07:14 2004
      And the point of that was?...My localtime is off too...
      perl -e "print scalar localtime();" = Thu Sep 9 13:42:34 2004
      and its not 1:42 pm right now...
        The point of that was (I would guess) to point out that time() is supposed to be UTC-based, with gmtime and localtime available to display it according to your whim. It's only now that we hear that localtime() is giving you a problem.

        Maybe your system clock is wrong? Are you on a *nixy system? What do you get when you type "date" at your friendly local shell prompt?

        --
        edan