Link to home
Start Free TrialLog in
Avatar of hilltop
hilltopFlag for United States of America

asked on

Looking for DateDiff Equivalent. Must make app time limited trial..

Hi I am looking for ways to make my C++ app a time limited trial. Any suggestion would be great. I generally use the VB DateDiff, but I see no equivalent in C++. It would be nice to have sample code.
ASKER CERTIFIED SOLUTION
Avatar of brettmjohnson
brettmjohnson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jkr
Since you are on Windows, the Windows API should also be mentioned. The functions presented in the article at http://www.codeproject.com/datetime/winapi_datetime_ops.asp ("WinAPI - simple operations with datetime") e.g have something similar to 'DateDiff', e.g.

 SYSTEMTIME s1={2003,11,0,28,12/*hours*/,0,0,0},s2={2003,12,0,1,0,0,0,0};
 printf("%f\n",DT_PeriodsBetween(nano100SecInDay,&s1,&s2));

would retrieve the days elapsed between both time stamps.

For more general information, see http://www.codeproject.com/datetime/datetimedisc.asp ("Date and Time in C++")
Avatar of hilltop

ASKER

Back to difftime. How could I structure a date in the format 2/13/2006 to a time_t and then do the datediff..
Use mktime() to convert struct tm to time_t

Avatar of hilltop

ASKER

Could you explain why this doesnt work?


#define TIME_LIMIT ((double)86400 * 30)
struct tm  timeinfo;


//'memset(&stm,0,sizeof(stm));

timeinfo.tm_year = 2006 - 1900; // subtract 1900
timeinfo.tm_mon = 2 - 1; // January == 0
timeinfo.tm_mday = 10;

//long * crap =1139813215;
time_t start_time = mktime(&timeinfo);
// Some time later...
time_t curr_time =  time(NULL);

// Calculate the time difference (in seconds)
double crap = difftime(curr_time, start_time);
if (crap > TIME_LIMIT) {
    printf("%d > %d\n",difftime, TIME_LIMIT);
return 0;
}



      printf("%d Huh %d\n",start_time, curr_time);
      return 0;
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>Could you explain why this doesnt work?

Your format strings are a bit off, that should be

    printf("%ud > %ud\n",crap, TIME_LIMIT);
    printf("%ud Huh %ud\n",start_time, curr_time);
Avatar of hilltop

ASKER

Thanks I used an INI class and the following code

int main(int argc, char* argv[])
{
#define TIME_LIMIT ((double)86400 * 30)
time_t curr_time =  time(NULL);
CIniReader iniReader("MintDNS.ini");
CIniWriter iniWriter("MintDNS.ini");
time_t start_time;
int gettm = iniReader.ReadInteger("Encoding", "Style", 25);
 start_time = gettm;
    if (gettm == 777){
         start_time =  curr_time;
      }
if (gettm == 111){
        start_time = time(NULL);
        iniWriter.WriteInteger("Encoding", "Style", (DWORD)start_time);
        start_time = iniReader.ReadInteger("Encoding", "Style", 25);
      }

double crap = difftime(curr_time, start_time);
if (crap > TIME_LIMIT) {
    printf("Expired\n");
return 0;
}



      printf("%d seconds past\n",crap);
      return 0;
}