0

In this Book object I created a system to keep track of the time a book has been checked out... there is a tm* called dateCheckedOut to store the date that the book is checked out.

int Book::getHeldTime()
{
time_t now ;
time(&now);
tm* t = localtime(&now);


double difference = difftime(now, mktime(dateCheckedOut))/(60 * 60 * 24);

cout << dateCheckedOut->tm_mon << dateCheckedOut->tm_mday << dateCheckedOut->tm_year << endl;  //prints out 231117, which is correct.
cout << t->tm_mon << t->tm_mday << t->tm_year << endl; //prints out 34117, which is also correct
cout << difftime(now, mktime(dateCheckedOut)) << endl; //prints out 0
cout << difference << endl; //prints out 0;

return (int)(difference); //returns 0
}

I am quite confused because I checked the date when its checked out and the date when it is loaded, both are correct, but the difftime function just returns 0. Is there anything that might cause this code to not work? Thank in advance!

P.S. dateCheckedOut only has tm_mday, tm_mon, and tm_year set to the correct value, the rest are all not set. Is that a problem?

Steven Zhao
  • 301
  • 1
  • 2
  • 8
  • I would set `dateCheckedOut` using *normal* calls (like `localtime`) then change the fields you want to set (day, month, year). That way you can be certain the currently unset fields aren't causing a problem. I seem to recall there's only a couple of fields that are ignored, the rest are used even if they're invalid values. – paxdiablo Apr 02 '17 at 04:02
  • Just letting you know, instead of using C-style casts like (int), use C++-style casts. [More information](http://stackoverflow.com/questions/103512/why-use-static-castintx-instead-of-intx). –  Apr 02 '17 at 04:03

0 Answers0