how we compare times
i tried difftime() funtion.it returns the diffrence in time.
but i wan twhich one is early
how we compare times
i tried difftime() funtion.it returns the diffrence in time.
but i wan twhich one is early
What format is the timestamp in? I assume its a UNIX timestamp, which is seconds since the UNIX epoch.
So,
Similar rules apply to struct time_t, just research it!Code:time1 = 600079 time2 = 700960 difference = time2 - time1 print difference " seconds apart"
my time format like this
04:10:46,03:15:45
i ant to compare these to time stamps
Your best bet would be to convert it to your own 'timestamp' (convert it all to seconds for example),
Code:unsigned short int time1 = 0, time2 = 0, h, m, s; sscanf("04:10:46", "%od:%od:%od", &h, &m &s); time1 = (h * pow(60, 2)) + (m * 60) + s; /* and so on */ /* You can now compare the two by, time1 - time2, to get the seconds in-between */
> i tried difftime() funtion.it returns the diffrence in time.
I imagine by looking at whether the result is positive or negative.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
it's not giving any negitive value.
it's only displaying the absolute value.
You could post some actual code, with some actual data, rather than keep posting "it doesn't work".
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
ok
printf("%d \n",difftime("04:10:52","05:14:36"));
it's printing absolute valueee.
one more problem is how we can compare datee.
thank u
Writing random code are we?
Code::6: warning: passing arg 1 of `difftime' makes integer from pointer without a cast :6: warning: passing arg 2 of `difftime' makes integer from pointer without a cast
Not to mention trying to print a float result with %d
> "04:10:52"
The first thing you need to do is parse your time into a "struct tm" variable, then call mktime() on it to return a "time_t" variable.
Then you can call difftime()
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
i tried this
char buffer[20]="04:30:12";
char buffer1[20]="03:34:36";
int h,m,s;
struct tm ctime,ptime;
sscanf(buffer, "%od:%od:%od", &h, &m, &s);
ctime.tm_sec=s;
ctime.tm_min=m;
ctime.tm_hour=h;
sscanf(buffer1, "%od:%od:%od", &h, &m, &s);
ptime.tm_sec=s;
ptime.tm_min=m;
ptime.tm_hour=h;
time_t now1,now2;
now1=mktime(&ctime);
now2=mktime(&ptime);
g_print("***************** %d \n",difftime(now2,now1));
but
again same problem geting "0 " or garbage values
Both your sscanf and printf argument conversions are broken.Code:#include <stdio.h> #include <time.h> int main ( ) { char buffer[20]="04:30:12"; char buffer1[20]="03:34:36"; int h,m,s; struct tm ctime,ptime; sscanf(buffer, "%od:%od:%od", &h, &m, &s); ctime.tm_sec=s; ctime.tm_min=m; ctime.tm_hour=h; sscanf(buffer1, "%od:%od:%od", &h, &m, &s); ptime.tm_sec=s; ptime.tm_min=m; ptime.tm_hour=h; time_t now1,now2; now1=mktime(&ctime); now2=mktime(&ptime); printf("***************** %d \n",difftime(now2,now1)); return 0; } $ gcc -W -Wall -ansi -pedantic -O2 foo.c foo.c: In function `main': foo.c:8: warning: unsigned int format, int arg (arg 3) foo.c:8: warning: unsigned int format, int arg (arg 4) foo.c:8: warning: unsigned int format, int arg (arg 5) foo.c:12: warning: unsigned int format, int arg (arg 3) foo.c:12: warning: unsigned int format, int arg (arg 4) foo.c:12: warning: unsigned int format, int arg (arg 5) foo.c:16: warning: ISO C90 forbids mixed declarations and code foo.c:19: warning: int format, double arg (arg 2)
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
thank u for ur rely
is it necessary to give the value of tm_year,month,day.
i only intialised sec,min,hour,
so that it's taking default year,month,day.that's why iam getting garbage values.
Something basic as making sure your variables are initialised.Code:struct tm ctime = { 0 };
Man.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.