user2308001
user2308001

Reputation: 233

Mktime returns different values

I have a class for operations with dates. When I try to intialize it with the same values,the first instance works fine but all the others return different values every time.I keep the dates in time_t DATE variable.

Its constructor :

CDate (int y,int m,int d)
    {
     tm * datetmp ;
     datetmp = localtime (&DATE);
     datetmp->tm_year = y - 1900;
     datetmp->tm_mon = m - 1;
     datetmp->tm_mday = d;
     DATE=mktime ( datetmp );
    }

Main:

CDate a ( 2000, 1, 30 );
CDate b ( 2000, 2, 27 );
cout <<a.DATE <<endl;
cout << b.DATE << endl;

The results after each run:

949240864 951646384

949240864 951605568

949240864 951632960

949240864 951617456

949240864 951662240

Upvotes: 0

Views: 290

Answers (1)

Iłya Bursov
Iłya Bursov

Reputation: 24229

I'm not very sure how this code should work, but I suppose you want something like this:

CDate (int y,int m,int d)
{
    tm datetmp;
    memset(&datetmp, 0, sizeof(datetmp));
    datetmp.tm_year = y - 1900;
    datetmp.tm_mon = m - 1;
    datetmp.tm_mday = d;
    DATE = mktime(datetmp);
}

Upvotes: 1

Related Questions