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 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>