inconsistencies with mktime()

Discussion in 'C Programming' started by John Hanley, Sep 8, 2007.

  1. John Hanley

    John Hanley Guest

    I am getting some inconsistencies with mktime().

    I allocate memory for my struct tm early in my program, and assign only
    *some*
    of the member variables.

    t->tm_sec=s;
    t->tm_min=m;
    t->tm_hour=h;
    t->tm_mday=d;
    t->tm_mon=mo-1;
    t->tm_year=y-1900;

    I leave out tm_wday, tm_yday, & tm_isdst.

    after this I free(t).

    I examine the values in my debugger. When I allocate the memory, all the
    values are 0. When I call mktime(t), the function changes my tm_hour
    (adds an hour) and I get a certain value returned.

    Why is it changing the hour?

    In another part of my program (a totally different function), I allocate
    memory for my struct tm (a different local struct tm), I read in data
    (exactly the same values as in my other function), again I don't fill in
    tm_wday, tm_yday, & tm_isdst. There seems to be some residual values
    (not 0 this time)from when I allocated the memory and didn't initialize
    the 3 fields.This time when I call mktime(t), it doesn't change my
    tm_hour and I get a different value returned (the correct value).

    Same initial values, two different calls, two different return values.

    It appears mktime is trying to guess the correct date based on what I
    provide.
    I am thinking that perhaps whatever values happen to be in the memory
    location when I allocate the memory are what are changing the values?

    Should I be initializing the tm_wday, tm_yday, & tm_isdst to something?

    Why is mktime() changing the hour in my struct tm?

    Any suggestions?
    Thanks!
    John
    John Hanley, Sep 8, 2007
    #1
    1. Advertising

  2. John Hanley

    Ian Collins Guest

    John Hanley wrote:
    > I am getting some inconsistencies with mktime().
    >
    > I allocate memory for my struct tm early in my program, and assign only
    > *some*
    > of the member variables.
    >
    > t->tm_sec=s;
    > t->tm_min=m;
    > t->tm_hour=h;
    > t->tm_mday=d;
    > t->tm_mon=mo-1;
    > t->tm_year=y-1900;
    >
    > I leave out tm_wday, tm_yday, & tm_isdst.
    >
    > after this I free(t).
    >
    > I examine the values in my debugger. When I allocate the memory, all the
    > values are 0. When I call mktime(t), the function changes my tm_hour
    > (adds an hour) and I get a certain value returned.
    >
    > Why is it changing the hour?
    >

    The tame you have set is an daylight saving for your timezone, so it has
    been corrected. You have to set tm_isdst.

    from the Solaris man page which expands on the standard:

    If tm_isdst is positive, the original values are assumed to
    be in the alternate timezone. If it turns out that the
    alternate timezone is not valid for the computed calendar
    time, then the components are adjusted to the main timezone.
    Likewise, if tm_isdst is zero, the original values are
    assumed to be in the main timezone and are converted to the
    alternate timezone if the main timezone is not valid. If
    tm_isdst is negative, mktime() attempts to determine whether
    the alternate timezone is in effect for the specified time.


    --
    Ian Collins.
    Ian Collins, Sep 8, 2007
    #2
    1. Advertising

  3. John Hanley

    John Hanley Guest

    Ian Collins wrote:
    > John Hanley wrote:
    >> I am getting some inconsistencies with mktime().
    >>
    >> I allocate memory for my struct tm early in my program, and assign only
    >> *some*
    >> of the member variables.
    >>
    >> t->tm_sec=s;
    >> t->tm_min=m;
    >> t->tm_hour=h;
    >> t->tm_mday=d;
    >> t->tm_mon=mo-1;
    >> t->tm_year=y-1900;
    >>
    >> I leave out tm_wday, tm_yday, & tm_isdst.
    >>
    >> after this I free(t).
    >>
    >> I examine the values in my debugger. When I allocate the memory, all the
    >> values are 0. When I call mktime(t), the function changes my tm_hour
    >> (adds an hour) and I get a certain value returned.
    >>
    >> Why is it changing the hour?
    >>

    > The tame you have set is an daylight saving for your timezone, so it has
    > been corrected. You have to set tm_isdst.
    >
    > from the Solaris man page which expands on the standard:
    >
    > If tm_isdst is positive, the original values are assumed to
    > be in the alternate timezone. If it turns out that the
    > alternate timezone is not valid for the computed calendar
    > time, then the components are adjusted to the main timezone.
    > Likewise, if tm_isdst is zero, the original values are
    > assumed to be in the main timezone and are converted to the
    > alternate timezone if the main timezone is not valid. If
    > tm_isdst is negative, mktime() attempts to determine whether
    > the alternate timezone is in effect for the specified time.
    >
    >

    That did the trick. Thanks so much!

    John
    John Hanley, Sep 8, 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. =?Utf-8?B?QUxQTw==?=

    Datagrid bind inconsistencies.......

    =?Utf-8?B?QUxQTw==?=, Nov 14, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    334
    Eliyahu Goldin
    Nov 15, 2005
  2. =?Utf-8?B?QUxQTw==?=

    Datagrid binding inconsistencies...............

    =?Utf-8?B?QUxQTw==?=, Nov 15, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    379
    =?Utf-8?B?UGhpbGxpcCBXaWxsaWFtcw==?=
    Nov 15, 2005
  3. Florian Quetting

    mktime segfault

    Florian Quetting, Dec 31, 2004, in forum: C++
    Replies:
    2
    Views:
    504
    Florian Quetting
    Dec 31, 2004
  4. Rajmohan

    GMT equivalent of mktime

    Rajmohan, Jul 14, 2003, in forum: C Programming
    Replies:
    1
    Views:
    862
    Mark McIntyre
    Jul 14, 2003
  5. Gore

    mktime and DST.

    Gore, Oct 7, 2003, in forum: C Programming
    Replies:
    1
    Views:
    4,241
    Richard Bos
    Oct 7, 2003
Loading...

Share This Page