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.
LVL 7
hilltopAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

brettmjohnsonCommented:
Use the ANSI standard functions time() and difftime() as follows:

#include <time.h>

// time limit of 30 days (86400 seconds per day)
#define TIME_LIMIT ((double)86400 * 30)

time_t start_time = time(NULL);
// Some time later...
time_t curr_time =  time(NULL);

// Calculate the time difference (in seconds)
double diff_time = difftime(curr_time, start_time);
if (diff_time > TIME_LIMIT) {
      // time trial has expired.
}
      


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
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++")
hilltopAuthor Commented:
Back to difftime. How could I structure a date in the format 2/13/2006 to a time_t and then do the datediff..
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

brettmjohnsonCommented:
Use mktime() to convert struct tm to time_t

hilltopAuthor Commented:
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;
jkrCommented:
If you want to use 'difftime()', there's no need to do that. Just store the binary time_t values in the registry like

BOOL SetInstallDate ( DWORD dwDate) {

     BOOL bRC = FALSE;

     LPCTSTR pszSubKey = "MyApp\\Settings";
     HKEY hBase = HKEY_CURRENT_USER;

     HKEY hKey = NULL;
     REGSAM sam = KEY_SET_VALUE | KEY_READ;

     if ( ERROR_SUCCESS != RegCreateKey ( hBase, pszSubKey, &hKey)) {

          return FALSE;
     }

     DWORD dwType = REG_DWORD;
     DWORD dwSize = sizeof ( DWORD);

     // set new value

     if ( ERROR_SUCCESS == RegSetValueEx ( hKey, "InstallStamp", 0, dwType, ( LPBYTE) &dwDate, dwSize)) {

          bRC = TRUE;
     }


     RegCloseKey ( hKey);

     return bRC;
}

BOOL GetInstallDate ( DWORD* pdwDate) {

     BOOL bRC = FALSE;

     LPCTSTR pszSubKey = "MyApp\\Settings";
     HKEY hBase = HKEY_CURRENT_USER;

     HKEY hKey = NULL;
     REGSAM sam = KEY_READ;

     if ( ERROR_SUCCESS != RegCreateKey ( hBase, pszSubKey, &hKey)) {

          return FALSE;
     }

     DWORD dwType = REG_DWORD;
     DWORD dwSize = sizeof ( DWORD);

     // get old value

     if ( ERROR_SUCCESS == RegQueryValueEx ( hKey, "IntallStamp", 0, &dwType, ( LPBYTE) pdwDate, &dwSize)) {

          bRC = TRUE;
     }

 
     RegCloseKey ( hKey);

     return bRC;
}

so you could

// upon installation
time_t tInst = time(NULL);
SetInstallDate ( (DWORD) tInst);

// when running
time_t tNow = time(NULL);
time_t tInst;
SetInstallDate ( (DWORD*) &tInst);

double dDiff = difftime(tNow,tInst);

But, if you explicitly what to use that 'manually', 'mktime()' is - as mentioned - the correct way:

struct tm tim;

tim.tm_day = 13;
tim.tm_mon = 2 - 1; // month indices are 0-based
tim.tm_year = 2006 - 1900; // you guess it...

time_t t = mktime(&tim);


jkrCommented:
>>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);
hilltopAuthor Commented:
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;
}
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.