0

I was looking for similar topic here, but sadly I wasn't able to find anything helpful.
Problem with my application in C++ is that I need to compute the difference in seconds between two date times given in string structure.
I already cut the string structures into specific day, month etc values. So I started testing the tm structure given in <ctime> library.

I wrote simple code:

tm dat1, dat2;
time_t t1, t2;

dat1.tm_mday=3;
dat1.tm_year=2012-1990;
dat1.tm_mon=1-1;


dat2.tm_mday=3;
dat2.tm_year=2003-1990;
dat2.tm_mon=3-1;


t1 = mktime(&dat1);
t2 = mktime(&dat2);


double m = difftime(t1, t2);
cout << m << "\n";

which is sadly giving my an error: returning -1 value in mktime() function.
For the first time I also filled fields tm_sec, tm_min, tm_hour but there was also the same error. Right now I am wondering if the fields tm_yday and tm_wday are necessary to use mktime() function?
If yes is there simple way to compute them, because my input looks like "27.07.2012 18:52:00".

Thanks for help, and have a nice day!

Jerry YY Rain
  • 4,134
  • 7
  • 35
  • 52
General_Code
  • 239
  • 1
  • 4
  • 12
  • This is really the type of thing to use a library for. Date/time calculations can be more odd and complex then you'd imagine. –  Jul 11 '14 at 08:25
  • 1
    possible duplicate of [Number of days between two dates C++](http://stackoverflow.com/questions/14218894/number-of-days-between-two-dates-c) – KjMag Jul 11 '14 at 08:29
  • 1
    `struct tm` has more fields than that, doesn't it? Maybe set the whole thing to zero (something like `tm dat1 = {}, dat2 = {};`)? – Mats Petersson Jul 11 '14 at 08:36
  • It's working! Thanks for the answer, i just wondering why this if() is needed, I will check this out later! – General_Code Jul 11 '14 at 08:39
  • Agree with user3564091, sharafjaffri's answer from that question would seem to do exactly what you want so long as you can get your dates into `tm struct` correctly and don't bother converting to days as it's already in seconds. – Component 10 Jul 11 '14 at 08:40

0 Answers0