inconsistencies with mktime()

J

John Hanley

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
 
I

Ian Collins

John said:
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.
 
J

John Hanley

Ian said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top