Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   inconsistencies with mktime() (http://www.velocityreviews.com/forums/t535880-inconsistencies-with-mktime.html)

John Hanley 09-08-2007 08:59 PM

inconsistencies with mktime()
 
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

Ian Collins 09-08-2007 09:20 PM

Re: inconsistencies with mktime()
 
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.

John Hanley 09-08-2007 10:17 PM

Re: inconsistencies with mktime()
 
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


All times are GMT. The time now is 09:19 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.