J
John Hanley
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:
int adjust_time()
{
struct tm time_broken; /* time in broken time */
struct tm tb; /* adjusted time */
time_t time_calendar=0; /* calendar time as a long int */
int sec,min,hour,mday,mon,year;
long int dt; /* temp variables */
int tm;
/* if CHG_TIME==0, no conversion necessary */
if(CHG_TIME==0)
{
return 1;
}
/* parse out time & date info */
sec=0;
min=TIME%100;
hour=TIME/100;
mday=DATE%100;
mon=((DATE%10000)/100)-1;
/* year must be represented as years since 1900 */
/* If the year is < 40, we assume it's in 2000's */
/* If the year is >= 40, we assume it's in 1900's */
year=(DATE/10000);
if(year<40)
year=year+100;
/* create the struct tm */
time_broken.tm_sec=sec;
time_broken.tm_min=min;
time_broken.tm_hour=hour;
time_broken.tm_mday=mday;
time_broken.tm_mon=mon;
time_broken.tm_year=year;
/* get the number of seconds since
* Jan 1, 1970 */
time_calendar=mktime(&time_broken);
/* subtract CHGTIME from calendar time_t date */
time_calendar=time_calendar-(CHG_TIME*60);
tb=*(gmtime(&time_calendar));
/* calculate new time */
tm=(tb.tm_hour*100)+tb.tm_min;
/* calculate new date */
dt=( ((tb.tm_year%100)*10000) + ((tb.tm_mon+1)*100) + (tb.tm_mday) );
/* return new time & date */
TIME=tm;
DATE=dt;
return 0;
}
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:
int adjust_time()
{
struct tm time_broken; /* time in broken time */
struct tm tb; /* adjusted time */
time_t time_calendar=0; /* calendar time as a long int */
int sec,min,hour,mday,mon,year;
long int dt; /* temp variables */
int tm;
/* if CHG_TIME==0, no conversion necessary */
if(CHG_TIME==0)
{
return 1;
}
/* parse out time & date info */
sec=0;
min=TIME%100;
hour=TIME/100;
mday=DATE%100;
mon=((DATE%10000)/100)-1;
/* year must be represented as years since 1900 */
/* If the year is < 40, we assume it's in 2000's */
/* If the year is >= 40, we assume it's in 1900's */
year=(DATE/10000);
if(year<40)
year=year+100;
/* create the struct tm */
time_broken.tm_sec=sec;
time_broken.tm_min=min;
time_broken.tm_hour=hour;
time_broken.tm_mday=mday;
time_broken.tm_mon=mon;
time_broken.tm_year=year;
/* get the number of seconds since
* Jan 1, 1970 */
time_calendar=mktime(&time_broken);
/* subtract CHGTIME from calendar time_t date */
time_calendar=time_calendar-(CHG_TIME*60);
tb=*(gmtime(&time_calendar));
/* calculate new time */
tm=(tb.tm_hour*100)+tb.tm_min;
/* calculate new date */
dt=( ((tb.tm_year%100)*10000) + ((tb.tm_mon+1)*100) + (tb.tm_mday) );
/* return new time & date */
TIME=tm;
DATE=dt;
return 0;
}