Hi People,
i am trying to convert 2 different time_t variables i have to do some basic date comparision.However when i try to convert, the function seem to return the date of the earlier converted of the 2 time_t i passed in. Could anyone help me out here ? I been at it all morning and can't figure out the walk around.
struct tm * startTime;
startTime = localtime(&rawStartTime); // assume rawStartTime is 1140428037
struct tm * currentTime;
time(&rawCurrentTime); //assume rawCurrentTime is 1139909637
currentTime = localtime(&rawCurrentTime);
cout<<"startTime->tm_year:"<<startTime->tm_year<<endl;
cout<<"startTime->tm_mon:"<<startTime->tm_mon<<endl;
cout<<"startTime->tm_mday:"<<startTime->tm_mday<<endl;
cout<<"currentTime->tm_year:"<<currentTime->tm_year<<endl;
cout<<"currentTimee->tm_mon:"<<currentTime->tm_mon<<endl;
cout<<"currentTime->tm_mday:"<<currentTime->tm_mday<<endl;
I did this:
struct tm * startTime;
time_t rawStartTime=1140428037;
time_t rawCurrentTime = 1139909637;
struct tm * currentTime;
startTime = localtime(&rawStartTime); // assume rawStartTime is 1140428037
cout<<"startTime->tm_year:"<<startTime->tm_year<<endl;
cout<<"startTime->tm_mon:"<<startTime->tm_mon<<endl;
cout<<"startTime->tm_mday:"<<startTime->tm_mday<<endl;
currentTime = localtime(&rawCurrentTime);
cout<<"currentTime->tm_year:"<<currentTime->tm_year<<endl;
cout<<"currentTimee->tm_mon:"<<currentTime->tm_mon<<endl;
cout<<"currentTime->tm_mday:"<<currentTime->tm_mday<<endl;
I got this:
startTime->tm_year:107
startTime->tm_mon:8
startTime->tm_mday:21
currentTime->tm_year:106
currentTimee->tm_mon:1
currentTime->tm_mday:14
Your assumptions about the values of the struct tm * ptr is wrong. localtime() uses just one "hidden" struct tm to do it's work. You have to print the first struct pointer, then call localtime(), then print the second struct tm pointer. Your pointers reference the same place in memory, so they see the last change to that hidden struct