-
making a program expire
Hi all ... i was just wondering is there is a function that could be used in conjunction with a TM structure to get the system date and time? I am trying to make a program expire by using the below code in the constructor of a class. However i see no function that can fill this structure.
Code:
struct tm today;
//getdate(&today); /* need a getdate function here */
if(today.tm_year > 1900)
{
if(today.tm_mon > 6)
{
cout << "Program Error ... Aborting!";
exit(1);
}
}
Im using MSVC++ 6.0 ... thanks for any help :)
-
Create a struct tm for the time you want the program to expire
use time() and localtime() to get a struct tm for now
use difftime() to work out what the difference is. When the sign of the result changes, then the time is past.
-
Right on ... thanks for the reply, ill give that a shot!
:)
-
Hi again ... just had another question about making a program expire. After i have a TM for the time i want the program to expire, and a TM from the time() and localtime() functions. How would i convert these TM structures into a time_t type, so i could use the difftime() function on them ? Or would it be better to just compare the two TM structures to see if the one from the computer is past the expire TM ?
Code:
void CTest::programExpiration(void)
{
double difference = 0;
time_t systime;
struct tm expire;
expire.tm_mday = 10;
expire.tm_mon = 6;
expire.tm_year = 2004;
time(&systime);
struct tm *today = localtime(&systime);
//difference = difftime(today, expire);
}
Any help would be appreciated ... thanks again :)
-
>How would i convert these TM structures into a time_t type, so i could use the difftime() function on them ?
The mktime function makes a time_t out of a struct tm *.
-
yes i did try the mktime() function before, and it worked for the "struct tm *today" structure, however does nother for the "struct tm expire" structure. Is there a function that can convert a "struct tm" into a time_t type ?
:P
-
>Is there a function that can convert a "struct tm" into a time_t type ?
No, but you could always pass the address of expire to make it a pointer.
-
I tried that also, however the program crashes, and the difftime() return value is always the same no matter how i change the expire TM struct :(
Code:
void CMk3dpdek::programExpiration(void)
{
double difference = 0;
time_t systime, today_t, expire_t;
struct tm expire;
expire.tm_mday = 9;
expire.tm_mon = 6;
expire.tm_year = 2004;
time(&systime);
struct tm *today = localtime(&systime);
today_t = mktime(today);
expire_t = mktime(&expire);
difference = difftime(today_t, expire_t);
cout << endl << "THE DIFF IS : " << difference << endl;
cout << endl << "TODAY TIME : " << ctime(&today_t);
cout << endl << "EXPIRE TIME : " << ctime(&expire_t);
}
-
You didn't normalize the values in expire before calling mktime. This is a good reason to check for return values so that you can check for errors.
Code:
void CMk3dpdek::programExpiration(void)
{
double difference = 0;
time_t systime, today_t, expire_t;
time(&systime);
struct tm *today = localtime(&systime);
struct tm expire = *today;
expire.tm_mday = 14;
expire.tm_mon = 6;
expire.tm_year = 2004 - 1900;
today_t = mktime(today);
expire_t = mktime(&expire);
difference = difftime(today_t, expire_t);
cout << endl << "THE DIFF IS : " << difference << endl;
cout << endl << "TODAY TIME : " << ctime(&today_t);
cout << endl << "EXPIRE TIME : " << ctime(&expire_t);
}
-
I think i see what you mean. So by getting the localtime() first then doing ...
Code:
struct tm expire = *today;
I guess that would be a "copy constructor" from one structure to another and make all the values the same.
But ya seems to work now thank you for all the help, guess i just have to add a if statement after the difftime() function and i can test it out.
Thanks again :)