Re: using mktime()
From: Al Bowers (xabowers_at_rapidsys.com)
Date: 02/12/05
- Next message: Luke Wu: "Re: What is a sequence point?"
- Previous message: infobahn: "Re: Making C better (by borrowing from C++)"
- In reply to: John Hanley: "using mktime()"
- Next in thread: John Hanley: "Re: using mktime()"
- Reply: John Hanley: "Re: using mktime()"
- Reply: John Hanley: "Re: using mktime()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 12 Feb 2005 09:18:55 -0500
John Hanley wrote:
> I created a function that breaks down a date into broken down time, I
> subtract a certain number of seconds from that, then use mktime() to
> recompute the calendar time.
>
> It works basically except every so often, I get the date 060207 (Feb 7,
> 2006) which is obviously not correct. When it does this it always gives me
> this date.
>
> I tracked it down to my mktime() call, when I get to a certain date, mktime
> returns a number in 4000000000, when it should return a number in
> 1000000000. But I am not sure why. The variable in my tm struct all have
> the correct date, but mktime returns this.
>
> It is happening to me when the date I am changing is: 040727 but it has done
> this on others as well.
>
> I am thinking that something goes wrong in mktime() to cause it to return
> some default number. Perhaps something wrong in one of my time_broken
> members and then it gets past to mktime?
>
> Suggestions? Thanks a bunch!
>
> Here's my code:
The code is not complete and it is not easy to follow the logic.
But I see you are making some errors. Apparently, you are
assuming that type time_t is type long representing seconds.
Standard C does not specify this to be fact. The Standard
only specifies that time_t be an arithmetic time capable of
representing time. And, it does not specify anything on
its instrumentality. So, to be portable, the code must not
assume the type to be type long and the values representing
seconds. To get around this, Standard C provides functions
that will allow you to manipulate time. So, to correct
your function adjust_time, you will need to convert the
time_t value to broken down time and the adjust the struct
member tm_sec in the number of seconds. Then call function
mktime to generate a new time_t value.
Another problem: check your return values.
You did not check the return value
of your mktime function. time_t's range of dates is limited.
Function mktime will return (time_t)-1 should it be uncapable
of representing that date. I can't be sure, but the values
you are getting in the range of 4000000000 may be result of
function mktime returning a (time_t)-1 value.
An example:
#include <stdio.h>
#include <time.h>
#include <limits.h>
time_t AdjustTime(time_t tvalue, int secs)
{
struct tm *tp;
time_t ret;
if((ret = (tvalue != (time_t)-1)))
{
tp = localtime(&tvalue);
if(secs > INT_MAX - tp->tm_sec)
ret = (time_t)-1;
else
{
tp->tm_sec+=secs;
tp->tm_isdst = -1;
ret = mktime(tp);
}
}
return ret;
}
int main(void)
{
time_t date;
struct tm t;
/* make a time_t value for 25DEC2005 12:00:00 */
t.tm_year = 2005-1900;
t.tm_mon = 11;
t.tm_mday = 25;
t.tm_hour = 12;
t.tm_min = t.tm_sec = t.tm_isdst = 0;
if((date = mktime(&t)) == (time_t)-1)
puts("Time is not available");
else
{
printf("date represents %s"
"Attemping to subtract 60 secs\n",ctime(&date));
if((date = AdjustTime(date, -60)) != (time_t)-1)
printf("The new date is %s",ctime(&date));
else puts("Time for the new date is unavailable");
}
return 0;
}
-- Al Bowers Tampa, Fl USA mailto: xabowers@myrapidsys.com (remove the x to send email) http://www.geocities.com/abowers822/
- Next message: Luke Wu: "Re: What is a sequence point?"
- Previous message: infobahn: "Re: Making C better (by borrowing from C++)"
- In reply to: John Hanley: "using mktime()"
- Next in thread: John Hanley: "Re: using mktime()"
- Reply: John Hanley: "Re: using mktime()"
- Reply: John Hanley: "Re: using mktime()"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|