time issue (mktime time_t tm)

S

saurabh

Hi all,
I have written a simple program,which takes a time string
and stores it in a struct called event,then it calculates the
diff between given time and current time,if current time is
passed ,it triggers the event.
In our case for simplicity,the event is printing value of a
variable x.I am giving time inputs which differ by 3 or 4
seconds,still the diff returned by difftime is too large,
and if i dont specify tm_year as year-1900(or some other year),
mktime fails. Can somebody help me please?

Also if it matters,
I am compiling this program on GNU/Linux with gcc3.4.6
<code>
#include<time.h>

typedef struct event{
char triggertime[100];
int x;
}event;

/* convert time string stored in event to a time_t object */
time_t convert_to_time_t(char* ttime)
{
time_t retval;
char year[5];
char month[3];
char day[3];
char hour[3];
char min[3];

strncpy(year,ttime,4);
year[4]='\0';

strncpy(month,&ttime[4],2);
month[2]='\0';

strncpy(day,&ttime[6],2);
day[2]='\0';

strncpy(hour,&ttime[8],2);
hour[2]='\0';

strncpy(min,&ttime[10],2);
min[2]='\0';
printf("\n before creating tm struct values are \nyear=%s month=%s day=%s hour=%s min=%s ",year,month,day,hour,min);

/* couldnt find a function to directly convert from string to time_t
* that is why I convert 1)string to tm and then 2)tm to time_t
*/
struct tm time_str;
/* if i dont specify tm_year as atoi(year)-1900 or some other year for that matter, mktime fails */
time_str.tm_year = atoi(year)-1900;

time_str.tm_mon = atoi(month);
time_str.tm_mday = atoi(day);
time_str.tm_hour = atoi(hour);
time_str.tm_min = atoi(min);
time_str.tm_sec = 1;
time_str.tm_isdst = 1;//confused about this
retval=mktime(&time_str);
if(retval==-1)
{
printf("\n convert to time failed \n");
}
return retval;

}
int should_i_trigger(event* c)
{
char* ttime=c->triggertime;
time_t t=convert_to_time_t(ttime);
time_t now1;
time(&now1);
double diff=difftime(t,now1);
printf("\n diff is %f \n",diff);
/* if diff is less than 0, that means we have already passed the time when
* the event had to be triggered, so return 1
* */

if(diff>0)
return 0;
else
return 1;
}


int main(int argc,char* argv[])
{

if(argc<2)
{
printf("\n Usage:./executable time_str \n");
printf("\n example time_str is 200904151055 (for 15 April 2009 10:55 AM) \n");
exit(-1);
}
event c1;
int len=strlen(argv[1]);
sprintf(c1.triggertime,"%s",argv[1]);
c1.x=1;
while(1)
{
int ret=0;
ret=should_i_trigger(&c1);
if(ret<1)
{
usleep(2000);
}
else
{
/* trigger the event,In our case the evnt is ' printing x' */
printf("\n x=%d\n",c1.x);
break;
}
}

return 0;
}
</code>
 
I

Ike Naar

Hi all,
I have written a simple program,which takes a time string
and stores it in a struct called event,then it calculates the
diff between given time and current time,if current time is
passed ,it triggers the event.
In our case for simplicity,the event is printing value of a
variable x.I am giving time inputs which differ by 3 or 4
seconds,still the diff returned by difftime is too large,
and if i dont specify tm_year as year-1900(or some other year),
mktime fails. Can somebody help me please?

Also if it matters,
I am compiling this program on GNU/Linux with gcc3.4.6
[snip code]

There are several problems with your code; but what most
likely causes the excessive difftime, is the fact that
the tm_mon member of a struct tm needs a value from 0 to 11,
(0 for Januari, 1 for Februari, etc.). So, the assignment

time_str.tm_mon = atoi(month);

in your code should be

time_str.tm_mon = atoi(month)-1;

Then, your program needs to #include several headers:
<string.h> (for strncmp), <stdio.h> (for printf), <stdlib.h> (for atoi)
<offtopic>
and <unistd.h> for usleep
</offtopic>.

You're using gcc. It would be a good idea to enable at least the
following compiler flags:

gcc -Wall -std=c99 -pedantic

and pay attention to every compiler warning you'll get.
 
S

saurabh

..SNIP..
time_str.tm_mon = atoi(month);
in your code should be

time_str.tm_mon = atoi(month)-1;
Making this change made it work.
Then, your program needs to #include several headers:
<string.h> (for strncmp), <stdio.h> (for printf), <stdlib.h> (for atoi)
<offtopic>
and <unistd.h> for usleep
</offtopic>.
Yes I did that.
You're using gcc. It would be a good idea to enable at least the
following compiler flags:

gcc -Wall -std=c99 -pedantic
Ok,Will make sure that before posting any code,I compile
it with above mentioned flags.
and pay attention to every compiler warning you'll get.
I do.
Thanks a lot Ike.
 
L

lawrence.jones

saurabh said:
Making this change made it work.

For the moment. You probably need to set time_str.tm_isdst to -1 rather than
1 to get the system to figure it out for you. The way it is now, you're
assuming that DST is always in effect.
 

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,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top