time_t help -> mktime() returns -1

I'm trying to understand how time, and the comparison of two dates, is handled in c++. I'm trying to see if one date, is before another date. I've done a lot of research on the net, but there doesn't seem to be any real definitive source as I still have a few questions. I'm hoping someone here will be nice enough to help.

I've been doing some reading and I see that time_t is the best way to do a comparison. time_t holds the amount of seconds that have passed since 1970-Jan-1 and my current date. If I have two time_t variables I can just see if one is less-than then other.

To set a date value, I can use struct tm, set the individual values (year,month,day), and then use mktime(&tm) to not only normalize my tm struct, but to also get a time_t value. I'm having trouble with this as mktime always sets my time_t to -1. Why? but first, a few questions...

Is this a good way to compare two dates? (struct tm -- convert to time_t)
If not, how else can I do it? Should I just write my own routine that compares the year, month, and day of two tm structs?

If I was writing software for a decent company, which would my boss probably expect to see? The time_t comparison right?

Is there a function that can accept a date and return a time_t without the need to create struct tm? I've seen some boost stuff, but I don't want to get into boost yet... not this early in my learning curve.

If I have the right idea, and this is the 'professional' way to do it, why does mktime return -1 in this code example?

(this is a simple console app)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include <iostream>
#include <string>
#include <time.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
     struct tm timeInfo={0,0,0,1,1,9,0,0};
     timeInfo.tm_isdst=0;
     
     time_t retval=0;
     retval=mktime(&timeInfo);

     cout << "retval: " << retval << endl;
     return 0;
}


Thanks guys, I really appreciate the help.
OK, so I've been playing around... I got my time_t to be something other then -1 when I increased the value of my year in timeInfo to 109.

So now my code is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include "stdafx.h"
#include <iostream>
#include <string>
#include <time.h>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
     struct tm timeInfo={0,0,0,1,1,109,0,0};
     timeInfo.tm_isdst=0;
     
     time_t retval=0;
     retval=mktime(&timeInfo);

     cout << "retval: " << retval << endl;
     return 0;
}


What is 109? Is that the year 109? Is that 1970 + 109 (or 2079)?
cout << timeInfo.tm_Year << endl; prints 109. What does that mean?

Sorry if these are common questions... I've done a lot of reading on it but it's still unclear... probably because I'm still new to the ways of C++
Are you sure you have the correct number of arguments for initializing tm? It may well be system-dependent, but my tm struct contains a long int, 9 ints, and a char * (for time zone).
Topic archived. No new replies allowed.