Getting Time Difference [modified]
-
hello guys...I made this small console app inwhich I can get the difference (duration) between two times. I then store them into database as string. But the problem is that sometimes it shows some weird difference which I am unable to trace. Here is what I have
SYSTEMTIME t,t2,t3; GetSystemTime(&t); //gets current time on button click GetSystemTime(&t2); //gets current time on second button click after some time //Get the difference between two times t3.wHour = t2.wHour - t.wHour; t3.wMinute = t2.wMinute - t.wMinute; t3.wSecond = t2.wSecond - t.wSecond; //function to store time in SqlServer Database, inwhich I do some concatination and store time in DB //omitting those to keep things simple GetMyDateTime2(t,t2,t3);
But here is one of the durations that I got from this calculation
2 3/3/2011 21:57:57 3/3/2011 21:58:3 0:1:65482_(duration)_ D:\Recordings\SecondRec.wav
what can be the problem?? thnx
modified on Friday, March 4, 2011 5:08 AM
-
hello guys...I made this small console app inwhich I can get the difference (duration) between two times. I then store them into database as string. But the problem is that sometimes it shows some weird difference which I am unable to trace. Here is what I have
SYSTEMTIME t,t2,t3; GetSystemTime(&t); //gets current time on button click GetSystemTime(&t2); //gets current time on second button click after some time //Get the difference between two times t3.wHour = t2.wHour - t.wHour; t3.wMinute = t2.wMinute - t.wMinute; t3.wSecond = t2.wSecond - t.wSecond; //function to store time in SqlServer Database, inwhich I do some concatination and store time in DB //omitting those to keep things simple GetMyDateTime2(t,t2,t3);
But here is one of the durations that I got from this calculation
2 3/3/2011 21:57:57 3/3/2011 21:58:3 0:1:65482_(duration)_ D:\Recordings\SecondRec.wav
what can be the problem?? thnx
modified on Friday, March 4, 2011 5:08 AM
The problem is your calculations are wrong (do you remember 'carry'?) and the
SYSTEMTIME
fields are16
bitWORDS
. TryWORD wCarry;
if (t2.wSecond >= t1.wSecond )
{
wCarry = 0;
t3.wSecond = t2.wSecond - t1.wSecond;
}
else
{
wCarry = 1;
t3.wSecond = 60 + t2.wSecond - t1.wSecond;
}
if (t2.wMinute >= t1.wMinute + wCarry)
{
wCarry = 0;
t3.wMinute = t2.wMinute - t1.wMinute;
}
else
{
wCarry = 1;
t3.wMinute = 60 + t2.wMinute - t1.wMinute;
}
if (t2.wHour >= t1.wHour + wCarry)
{
t3.wHour = t2.wHour - t1.wHour;
}
else
{
// handle error.
}Please note the above code would have troubles on button clicks crossing the date boundary... :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
hello guys...I made this small console app inwhich I can get the difference (duration) between two times. I then store them into database as string. But the problem is that sometimes it shows some weird difference which I am unable to trace. Here is what I have
SYSTEMTIME t,t2,t3; GetSystemTime(&t); //gets current time on button click GetSystemTime(&t2); //gets current time on second button click after some time //Get the difference between two times t3.wHour = t2.wHour - t.wHour; t3.wMinute = t2.wMinute - t.wMinute; t3.wSecond = t2.wSecond - t.wSecond; //function to store time in SqlServer Database, inwhich I do some concatination and store time in DB //omitting those to keep things simple GetMyDateTime2(t,t2,t3);
But here is one of the durations that I got from this calculation
2 3/3/2011 21:57:57 3/3/2011 21:58:3 0:1:65482_(duration)_ D:\Recordings\SecondRec.wav
what can be the problem?? thnx
modified on Friday, March 4, 2011 5:08 AM
-
hello guys...I made this small console app inwhich I can get the difference (duration) between two times. I then store them into database as string. But the problem is that sometimes it shows some weird difference which I am unable to trace. Here is what I have
SYSTEMTIME t,t2,t3; GetSystemTime(&t); //gets current time on button click GetSystemTime(&t2); //gets current time on second button click after some time //Get the difference between two times t3.wHour = t2.wHour - t.wHour; t3.wMinute = t2.wMinute - t.wMinute; t3.wSecond = t2.wSecond - t.wSecond; //function to store time in SqlServer Database, inwhich I do some concatination and store time in DB //omitting those to keep things simple GetMyDateTime2(t,t2,t3);
But here is one of the durations that I got from this calculation
2 3/3/2011 21:57:57 3/3/2011 21:58:3 0:1:65482_(duration)_ D:\Recordings\SecondRec.wav
what can be the problem?? thnx
modified on Friday, March 4, 2011 5:08 AM
overloaded Name wrote:
hello guys...I made this small console app inwhich I can get the difference (duration) between two times.
You might also check out
difftime()
."One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Some people are making such thorough preparation for rainy days that they aren't enjoying today's sunshine." - William Feather
-
hello guys...I made this small console app inwhich I can get the difference (duration) between two times. I then store them into database as string. But the problem is that sometimes it shows some weird difference which I am unable to trace. Here is what I have
SYSTEMTIME t,t2,t3; GetSystemTime(&t); //gets current time on button click GetSystemTime(&t2); //gets current time on second button click after some time //Get the difference between two times t3.wHour = t2.wHour - t.wHour; t3.wMinute = t2.wMinute - t.wMinute; t3.wSecond = t2.wSecond - t.wSecond; //function to store time in SqlServer Database, inwhich I do some concatination and store time in DB //omitting those to keep things simple GetMyDateTime2(t,t2,t3);
But here is one of the durations that I got from this calculation
2 3/3/2011 21:57:57 3/3/2011 21:58:3 0:1:65482_(duration)_ D:\Recordings\SecondRec.wav
what can be the problem?? thnx
modified on Friday, March 4, 2011 5:08 AM
By far, the simplest way is GetSystemTimeAsFileTime[^]. This returns a FILETIME[^] struct which contians 2 DWORDs which you can then drop directly into a ULARGE_INTEGER[^].
FILETIME ftStart, ftEnd; //Increments every 100 nanoseconds
GetSystemTimeAsFileTime(&ftStart);
//do something
GetSystemTimeAsFileTime(&ftEnd);
ULARGE_INTEGER liStart = { ftStart.dwLowDateTime, ftStart.dwHighDateTime };
ULARGE_INTEGER liEnd = { ftEnd.dwLowDateTime, ftStart.dwEndDateTime };printf("Duration: %I64u ms\n", (liEnd - liStart) / (10 * 1000));
or, if you dont mind using casts since ULARGE_INTEGER and FILETIME have the same structure:
ULARGE_INTEGER liStart, liEnd;
GetSystemTimeAsFileTime((FILETIME *)&liStart);
//do something
GetSystemTimeAsFileTime((FILETIME *)&liEnd);printf("Duration: %I64u ms\n", (liEnd - liStart) / (10 * 1000));