localtime and mktime

Discussion in 'Perl Misc' started by mhearne808[insert-at-sign-here]gmail[insert-dot-he, Jun 28, 2007.

  1. I think have a fundamental misunderstanding of one of either
    localtime() or mktime(). Here's a snippet of code that illustrates
    what I find confusing:

    #############################
    use Time::Local;
    use POSIX;

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

    print STDERR "Time4: $unixtime4\n";
    print STDERR "Time5: $unixtime5\n";
    #############################

    I would expect that $unixtime4 and $unixtime5 would be identical -
    however, they are different by exactly 1 hour (on Mac OS X and Linux
    RHEL).

    Can anyone explain to me why this is?

    --Mike
    mhearne808[insert-at-sign-here]gmail[insert-dot-he, Jun 28, 2007
    #1
    1. Advertising

  2. mhearne808[insert-at-sign-here]gmail[insert-dot-he

    chanio Guest

    mhearne808[insert-at-sign-here]gmail[insert-dot-here]com ha escrito:
    > I think have a fundamental misunderstanding of one of either
    > localtime() or mktime(). Here's a snippet of code that illustrates
    > what I find confusing:
    >
    > #############################
    > use Time::Local;
    > use POSIX;
    >
    > $unixtime4 = time();
    > ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
    > localtime($unixtime4);
    > $unixtime5 = mktime($sec,$min,$hour,$mday,$mon,$year);
    >
    > print STDERR "Time4: $unixtime4\n";
    > print STDERR "Time5: $unixtime5\n";
    > #############################
    >
    > I would expect that $unixtime4 and $unixtime5 would be identical -
    > however, they are different by exactly 1 hour (on Mac OS X and Linux
    > RHEL).
    >
    > Can anyone explain to me why this is?
    >
    > --Mike

    Right!
    But please, check your system time configuration.
    Your script works well.
    I get the following result...
    Time4: 1183067516
    Time5: 1183067516
    See?
    Find out your locale with POSIX
    here, everything Ok (and I live in Argentina, TZ -03)
    alberto
    chanio, Jun 28, 2007
    #2
    1. Advertising

  3. On 2007-06-28 21:57, chanio <> wrote:
    >
    > mhearne808[insert-at-sign-here]gmail[insert-dot-here]com ha escrito:
    >> I think have a fundamental misunderstanding of one of either
    >> localtime() or mktime(). Here's a snippet of code that illustrates
    >> what I find confusing:
    >>
    >> $unixtime4 = time();
    >> ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
    >> localtime($unixtime4);
    >> $unixtime5 = mktime($sec,$min,$hour,$mday,$mon,$year);

    [...]
    >> I would expect that $unixtime4 and $unixtime5 would be identical -
    >> however, they are different by exactly 1 hour (on Mac OS X and Linux
    >> RHEL).
    >>
    >> Can anyone explain to me why this is?
    >>
    >> --Mike

    > Right!
    > But please, check your system time configuration.
    > Your script works well.

    [...]
    > here, everything Ok (and I live in Argentina, TZ -03)


    So you currently don't have DST - try it again in six months.
    I assume that Mike lives on the northern hemisphere so he currently has
    DST.

    The problem is that mktime takes 9 arguments, not 6:

    mktime(sec, min, hour, mday, mon, year, wday = 0, yday = 0, isdst = 0)

    The last 3 are optional, but the default value of 0 for isdst is not
    particularly useful. Either you know whether you want DST or not, then
    you should explicitely specify it. Or you want mktime to guess, then you
    have to specify isdst as -1:

    unixtime5 = mktime($sec, $min, $hour, $mday, $mon, $year, 0, 0, -1);

    hp


    --
    _ | Peter J. Holzer | I know I'd be respectful of a pirate
    |_|_) | Sysadmin WSR | with an emu on his shoulder.
    | | | |
    __/ | http://www.hjp.at/ | -- Sam in "Freefall"
    Peter J. Holzer, Jul 1, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gore

    mktime and DST.

    Gore, Oct 7, 2003, in forum: C Programming
    Replies:
    1
    Views:
    4,263
    Richard Bos
    Oct 7, 2003
  2. KW
    Replies:
    1
    Views:
    1,825
    Eric Sosman
    Dec 8, 2004
  3. Eric Hodel

    Ruby/DL and libc's localtime

    Eric Hodel, Mar 18, 2005, in forum: Ruby
    Replies:
    2
    Views:
    102
    Eric Hodel
    Mar 18, 2005
  4. Leendert Bottelberghs

    time zone offset calc with localtime and gmtime

    Leendert Bottelberghs, Apr 4, 2005, in forum: Perl Misc
    Replies:
    11
    Views:
    771
    Mothra
    Apr 5, 2005
  5. localtime and Date::Manip

    , May 13, 2005, in forum: Perl Misc
    Replies:
    1
    Views:
    119
    Gunnar Hjalmarsson
    May 13, 2005
Loading...

Share This Page