timediff

L

Larry

Hi,

I am experiencing strange behavior with this code: It basicaly says 1
hours and 2 seconds (01:00:02) elapsed between the first block and the
second:

#include <windows.h>
#include <ctime>
#include <cstdlib>
#include <iostream>

int main()
{
time_t start = time(NULL);
{
struct tm timeinfo;
memset(&timeinfo, 0, sizeof(timeinfo));

localtime_s(&timeinfo, &start);

char szTime[30] = {0};
strftime(szTime, 30, "%X", &timeinfo);
std::cout << "Start: " << szTime << std::endl;
}

Sleep(2000);

time_t end = time(NULL);
{
struct tm timeinfo;
memset(&timeinfo, 0, sizeof(timeinfo));

time_t dif = (time_t)difftime(end, start);

localtime_s(&timeinfo, &dif);

char szTime[30] = {0};
strftime(szTime, 30, "%X", &timeinfo);
std::cout << "End, time elapsed is: " << szTime << std::endl;
}

system("pause");
return EXIT_SUCCESS;
}

what am I missing?

thanks
 
M

Michael Doubez

  I am experiencing strange behavior with this code: It basicaly says 1
hours and 2 seconds (01:00:02) elapsed between the first block and the
second: [snip]
  time_t dif = (time_t)difftime(end, start);

  localtime_s(&timeinfo, &dif); [snip]

what am I missing?

Your local timezone, use gmtime() instead of localtime.
 
L

Larry

That cast should have been a big red flag. difftime does not return a
time_t, and pretending that it does will give you unpredictable results.

Ok, I am tring to do that mysefl, like this:

#include <windows.h>

#include <ctime>

#include <cstdlib>

#include <iostream>

int main()

{

time_t start = 1266412200;

{

struct tm timeinfo;

memset(&timeinfo, 0, sizeof(timeinfo));

gmtime_s(&timeinfo, &start);

char szTime[30] = {0};

strftime(szTime, 30, "%X", &timeinfo);

std::cout << "Start: " << szTime << " time: " << start << std::endl;

}

time_t end = time(NULL);

{

struct tm timeinfo;

memset(&timeinfo, 0, sizeof(timeinfo));

double dif = difftime(end, start);

int seconds = (int)dif % 60;

int minutes = ( ((int)dif - seconds) % (60*60) ) / 60;

int hours = (seconds - (minutes * 60) - (int)dif) / (60*60);

char szElapsedTime[30] = {0};

sprintf_s(szElapsedTime, 30, "%02d:%02d:%02d", hours, minutes, seconds);

std::cout << szElapsedTime << std::endl;

}

system("pause");

return EXIT_SUCCESS;

}



this returns me -1:35:56 ... there's still something wrong...
 
H

Helge Kruse

Larry said:
time_t start = 1266412200;
....

time_t end = time(NULL);
....

double dif = difftime(end, start);
....

this returns me -1:35:56 ... there's still something wrong...

Probably you should consider how you predict the number assign to the
variable start. Without testing your code I would predict that you get never
the same result.

Helge
 
V

Victor Bazarov

Larry said:
[..]
double dif = difftime(end, start);

int seconds = (int)dif % 60;

int minutes = ( ((int)dif - seconds) % (60*60) ) / 60;

int hours = (seconds - (minutes * 60) - (int)dif) / (60*60);

I think you're calculating this particular one incorrectly.

Let's just take another, slower look at your calculations. 'dif' *is*
in seconds, only it has fractional part, yes? So, you need to get rid
of the fraction first. Use 'floor' function. Then you can cast it into
an 'int'. That would be total seconds, which you then want to convert
into hours, minutes, and seconds.

int seconds = int(floor(dif)); // total number of complete seconds

Now, how many hours are in N seconds? N/3600, yes? So, start with that

int hours = seconds / 3600; // full hours

How many seconds *leftover* after you remove all hours?

seconds -= hours * 3600; // remainder of seconds

(or you can do 'seconds %= 3600;' if it's easier to understand)
Now, in the leftover seconds (N'), how many minutes? N'/60. Say it:

int minutes = seconds / 60;

And how many seconds are still left over after you remove all the minutes?

seconds -= minutes * 60;

(again, you can simply do 'seconds %= 60;' if you like that)

So, semantically, your program should be

int seconds = int(floor(difftime(end, start)));
int hours = seconds / 3600;
seconds -= hours * 3600;
int minutes = seconds / 60;
seconds -= minutes * 60;
// here you have 'hours', 'minutes' and 'seconds' split

There is no need to dance around using 'dif' all the time.

V
 
J

James Kanze

Larry wrote:
That cast should have been a big red flag. difftime does not
return a time_t, and pretending that it does will give you
unpredictable results.

If he's included the proper header, the compiler knows the
return type of difftime, and will either reject the cast, or do
the right thing (supposing that the difference is representable
in a time_t). (I don't have access here to my copy of the C
standard, so I'm not sure what the return type of difftime is; I
vaguely remember that it returns seconds, in a double, but don't
quote me on it.)
 

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,770
Messages
2,569,584
Members
45,076
Latest member
OrderKetoBeez

Latest Threads

Top